最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular Material nested optgroups - Stack Overflow

programmeradmin1浏览0评论

After reading this SO question I found that it isn't possible to have nested optgroups as it's not part of the HTML5 spec. It is possible to apply styles yourself, but I would rather avoid that if it's possible in angular material.

"Under the hood" angular material swaps a lot of elements for divs and applies custom styling. Which makes me think they might handle this situation.

If I have the following code, I would expect the md-optgroup with label="Second level deep" to be indented further.

Plunker

<md-input-container>
    <md-select ng-model="vm.someModel">
        <md-optgroup label="One level deep">
            <md-option>
                One level deep
            </md-option>
        </md-optgroup>
        <md-optgroup label="Another group">
            <md-optgroup label="Second level deep">
                <md-option>Two levels deep</md-option>
            </md-optgroup>
        </md-optgroup>
    </md-select>
</md-input-container>

However, all md-optgroups are not indented and displayed the same.

This is how it currently looks:

I can't see anything in the demos or docs about this (in fact they they don't have any docs for md-optgroup at all), but the docs can sometimes be out of date/inplete.

Is it possible to have nested option groups with different indentation?

If not, what is the best way to achieve this using what is available in angular material? Should I use flex etc.

I'm trying to achieve something like this:

After reading this SO question I found that it isn't possible to have nested optgroups as it's not part of the HTML5 spec. It is possible to apply styles yourself, but I would rather avoid that if it's possible in angular material.

"Under the hood" angular material swaps a lot of elements for divs and applies custom styling. Which makes me think they might handle this situation.

If I have the following code, I would expect the md-optgroup with label="Second level deep" to be indented further.

Plunker

<md-input-container>
    <md-select ng-model="vm.someModel">
        <md-optgroup label="One level deep">
            <md-option>
                One level deep
            </md-option>
        </md-optgroup>
        <md-optgroup label="Another group">
            <md-optgroup label="Second level deep">
                <md-option>Two levels deep</md-option>
            </md-optgroup>
        </md-optgroup>
    </md-select>
</md-input-container>

However, all md-optgroups are not indented and displayed the same.

This is how it currently looks:

I can't see anything in the demos or docs about this (in fact they they don't have any docs for md-optgroup at all), but the docs can sometimes be out of date/inplete.

Is it possible to have nested option groups with different indentation?

If not, what is the best way to achieve this using what is available in angular material? Should I use flex etc.

I'm trying to achieve something like this:

Share Improve this question edited May 23, 2017 at 11:50 CommunityBot 11 silver badge asked Mar 4, 2016 at 11:03 Matt LishmanMatt Lishman 1,8172 gold badges23 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

After a bit of time to think about it, I managed to do it using flex-offset.

You can simply apply flex-offset to the md-optgroup.

I used flex-offset="5" because that looked the most sensible but any offset will work.

I added additional nesting and options to clarify the point a bit better.

Updated plunker

<md-input-container>
    <md-select ng-model="vm.someModel">
      <md-optgroup label="One level deep">
          <md-option>
              One level deep
          </md-option>
      </md-optgroup>
      <md-optgroup label="Another group">
          <md-optgroup flex-offset="5" label="Second level deep">

              <md-optgroup flex-offset="5" label="3 deep now and we want to see what happens with long text">
                <md-option>Some more options</md-option>
              </md-optgroup>

              <md-option>Two levels deep</md-option>
              <md-option>Two levels deep again</md-option>

          </md-optgroup>
      </md-optgroup>
    </md-select>
  </md-input-container>
发布评论

评论列表(0)

  1. 暂无评论