Expandable table rows in angular 4 with angular material

前端 未结 4 1301
别跟我提以往
别跟我提以往 2020-11-28 02:54

How would you make rows expandable in angular material tables? One requirement is that I need to be using the angular material table. I would also prefer to use the material

4条回答
  •  抹茶落季
    2020-11-28 03:15

    It is not possible out of the box, but you can solve it with a little custom code. Take a look at this discussion and this solution (not from me, but the basis for this answer).

    In short: Use the material table and add a click-method to the rows:

    
    

    Add a component for the expanded area. The row_detail.html contains the html which is in the expanded area.

    @Component({
      selector: 'app-inline-message',
      templateUrl: 'row_detail.html',
      styles: [`
        :host {
          display: block;
          padding: 24px;
          background: rgba(0,0,0,0.03);
        }
      `]
    })
    export class InlineMessageComponent {
      @Input() content1: string;
      @Input() content2: string;
    }
    

    In your component where the table lives you need the method to expand the row. First, add this to your component...

    expandedRow: number;
    @ViewChildren('myRow', { read: ViewContainerRef }) containers;
    

    ... and then add the method:

    /**
       * Shows the detail view of the row
       * @param {number} index
       */
    expandRow(index: number, row: DataFromRowFormat) {
    
        if (this.expandedRow != null) {
          // clear old message
          this.containers.toArray()[this.expandedRow].clear();
        }
    
        if (this.expandedRow === index) {
          this.expandedRow = null;
        } else {
          const container = this.containers.toArray()[index];
          const factory: ComponentFactory = this.resolver.resolveComponentFactory(InlineMessageComponent);
          const messageComponent = container.createComponent(factory);
    
          messageComponent.instance.content1= "some text";
          messageComponent.instance.content2 = "some more text";
        }
    }
    

提交回复
热议问题