Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

javascript - Add nested rows in angular material table

I want to create something like this but I don't want it to be an expansion panel just because I want the nested rows to be added or removed dynamically based on Add and delete button. I have a material data table with multiple rows and want to add nested rows in between 2 rows and these nested rows could be n numbers and I can increase or decrease the value of n. enter image description here

question from:https://stackoverflow.com/questions/65836337/add-nested-rows-in-angular-material-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To my knowledge Angular Material does not cater for nested rows.

I solved this issue by maintaining a "flat" table and changing the style of sub/nested rows to appear as such. To do this, the data needs to be ordered so that each row's sub rows are placed after it.

Ordering ex.

  1. Row 1
  2. Row 1 Nested Row 1
  3. Row 1 Nested Row 2
  4. Row 2
  5. Row 2 Nested Row 1
  6. etc.

Once the data is ordered, you need to identify and apply your class to the nested rows.

CSS - Nested row class example:

  .example-table {
    width: 100%;
   
    .cdk-column-a {
     
    }

    ..

    .sub-level-row {
      td {
        font-size: 11px;
        opacity: 0.85;
        border-bottom: 1px solid;
      }
    }
  }

HTML - Apply class using [ngClass] to nested rows:

  <tr mat-row [ngClass]="{'sub-level-row': isSubLevelRow(rowData)}"
  *matRowDef="let rowData; columns: tableColumns"></tr>

TS - Determine if row is nested:

  isSubLevelRow(rowData) {
    // use your data to determine if nested row
    return rowData.parentId !== null;
  }

Result:

Result

Apply padding, margins, colors etc. for more nested look.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...