简体   繁体   English

在表格中实现列切换

[英]Implement column toggle in table

I am working on angular app.我正在开发 angular 应用程序。 In that I am using primeng table with column toggle feature.我正在使用带有列切换功能的primeng表。 My code is as follows我的代码如下

HTML - HTML -

<p-table [columns]="selectedColumns" [value]="products">
    <ng-template pTemplate="caption">
        <p-multiSelect [options]="cols" [(ngModel)]="selectedColumns" optionLabel="header"
            selectedItemsLabel="{0} columns selected" [style]="{minWidth: '200px'}" placeholder="Choose Columns"></p-multiSelect>
    </ng-template>
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th>Code</th>
            <th *ngFor="let col of columns">
                {{col.header}}
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-product let-columns="columns">
        <tr>
            <td>{{product.code}}</td>
            <td *ngFor="let col of columns">
                {{product[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

Component -零件 -

import { Component, OnInit, Input } from '@angular/core';
import { Product } from './product';
import { ProductService } from './productservice';

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html'
    })
    export class AppComponent { 
        products: Product[];
        
        cols: any[];
    
        _selectedColumns: any[];
    
        constructor(private productService: ProductService) { }
    
        ngOnInit() {
            this.productService.getProductsSmall().then(data => this.products = data);
    
            this.cols = [
                { field: 'name', header: 'Name' },
                { field: 'category', header: 'Category' },
                { field: 'quantity', header: 'Quantity' }
            ];
    
            this._selectedColumns = this.cols;
        }
    
        @Input() get selectedColumns(): any[] {
            return this._selectedColumns;
        }
    
        set selectedColumns(val: any[]) {
            //restore original order
            this._selectedColumns = this.cols.filter(col => val.includes(col));
        }
    }

Stackblitz -堆栈闪电战 -

https://stackblitz.com/edit/primeng-tablecoltoggle-demo?file=src%2Fapp%2Fapp.component.ts https://stackblitz.com/edit/primeng-tablecoltoggle-demo?file=src%2Fapp%2Fapp.component.ts

Code is working fine but I want reverse functionality in col toggle.代码工作正常,但我想要 col 切换中的反向功能。 By default columns are coming as ticked in drop down and if user want to remove any column he can uncheck it.默认情况下,列会在下拉列表中打勾,如果用户想要删除任何列,他可以取消选中它。 I want to implement exactly reverse, that is by default all columns in dropdown should come as uncheck and if user wish to see data of any column he can tick the column in dropdown and can see the column with data.我想实现完全相反,即默认情况下下拉列表中的所有列都应取消选中,如果用户希望查看任何列的数据,他可以在下拉列表中勾选该列并可以看到包含数据的列。 how can I do that?我怎样才能做到这一点?

Just set the selected columns as an empty array to start out with.只需将选定的列设置为空数组即可。

   ngOnInit() {
            this.productService.getProductsSmall().then(data => this.products = data);
    
            this.cols = [
                { field: 'name', header: 'Name' },
                { field: 'category', header: 'Category' },
                { field: 'quantity', header: 'Quantity' }
            ];
    
            this._selectedColumns = []; // just leave an empty array from the start
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM