Angular 6 PrimeNG - how to add a checkbox with distinct values from one column?

試著忘記壹切 提交于 2019-12-11 15:42:12

问题


I have example Angular PrimeNG code for creating a Html Table:

<h3 class="first">Basic</h3>
<p-table [value]="cars">
    <ng-template pTemplate="header">
        <tr>
            <th>Vin</th>
            <th>Year</th>
            <th>Brand</th>
            <th>Color</th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-car>
        <tr>
            <td>{{car.vin}}</td>
            <td>{{car.year}}</td>
            <td>{{car.brand}}</td>
            <td>{{car.color}}</td>
        </tr>
    </ng-template>
</p-table>

<h3>Dynamic Columns</h3>
<p-table [columns]="cols" [value]="cars">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{col.header}}
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData let-columns="columns">
        <tr>
            <td *ngFor="let col of columns">
                    {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

How to create a dynamic checkbox (e.g. on the left side of a screen next to a table) so that options are generated based on unique values from a column? (something like Amazon has on the left side of the screen):

And based on a click for each checkbox - it would show subset of rows in a table...

Thanks


回答1:


You have to use : <p-tableHeaderCheckbox [value]="car"></p-tableHeaderCheckbox>

Here's an example:

 <p-table [value]="cars" [(selection)]="selectedCars">
        <ng-template pTemplate="header">
            <tr>
                <th style="width: 35px">
                   <p-tableHeaderCheckbox [value]="car"></p-tableHeaderCheckbox>
                </th>
                <th>Make</th>
                <th>Model</th>
                <th>Color</th>
             </tr>
         </ng-template>
         <ng-template pTemplate="body" let-car>
             <tr>
                 <td>
                     <p-tableCheckbox [value]="car"></p-tableCheckbox>
                 </td>
                 <td>{{car.make}}</td>
                 <td>{{car.model}}</td>
                 <td>{{car.color}}</td>                            
             </tr>
          </ng-template>
   </p-table> 

Take a look to the documentation: https://www.primefaces.org/primeng/#/table/selection

UPDATED ANSWER:

<div class="ui-g" style="width:250px;margin-bottom:10px">
    <div class="ui-g-12"><p-checkbox name="group1" value="New York" label="New York" [(ngModel)]="selectedCities" inputId="ny"></p-checkbox></div>
    <div class="ui-g-12"><p-checkbox name="group1" value="San Francisco" label="San Francisco" [(ngModel)]="selectedCities" inputId="sf"></p-checkbox></div>
    <div class="ui-g-12"><p-checkbox name="group1" value="Los Angeles" label="Los Angeles" [(ngModel)]="selectedCities" inputId="la"></p-checkbox></div>
</div>

ts

 selectedCities: string[] = [];

    selectedCategories: string[] = ['Technology', 'Sports'];

    checked: boolean = false;

You can always create the checkboxes with a *ngFor https://www.primefaces.org/primeng/#/checkbox



来源:https://stackoverflow.com/questions/52633982/angular-6-primeng-how-to-add-a-checkbox-with-distinct-values-from-one-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!