我使用Angular 6
我想制作一张表,该表使用角度材料的用户友好性和方便的角度数据表(和一般数据表)。
它工作正常,但我想添加数据表中提供的单个列搜索。 问题是我还没有定义诸如angular-datatbale方式(通过ajax调用)的数据结构,因此我无法使用它们的方式:https://l-lin.github.io/angular-datatables/#/advanced/individual-column-filtering
这是我的工作方式:
我的ts文件:
initDataTable() {
console.log('initDataTables');
const that = this;
this.dtOptions = {
pagingType: 'full_numbers',
// pageLength: 2,
serverSide: false, // si true, execute l'appel ajax à chaque utilisation d'un des filtres
processing: true,
deferRender: true,
lengthMenu: [[5, 10, 25, 50, -1], [5, 10, 25, 50, 'Tous']],
responsive: true,
language: {
lengthMenu: 'Afficher _MENU_ enregistrements par page',
zeroRecords: 'Aucun contact disponible',
info: '_START_ à _END_ sur un total de _TOTAL_ enregistrements',
// info: ' Page _PAGE_ sur _PAGES_',
infoEmpty: 'Aucun contact disponible',
infoFiltered: '(filtré(s) sur _MAX_ enregistrements)',
paginate: {
first: 'Premier',
last: 'dernier',
next: 'Suivant',
previous: 'Precedent'
},
search: '_INPUT_',
searchPlaceholder: 'Recherche',
},
order: [[1, 'desc']],
// Declare the use of the extension in the dom parameter
dom: 'Blfrtip',
stateSave: true,
buttons: [
// 'columnsToggle',
// 'colvis',
// 'copy',
{
extend: 'print',
className: 'btn btn-info btn-round btn-fab btn-fab-mini',
text: '<i class="material-icons" title="Imprimer">print</i>',
exportOptions: {
columns: [1, 2, 3, 4]
}
},
{
extend: 'pdfHtml5',
className: 'btn btn-danger btn-round btn-fab btn-fab-mini',
text: '<i class="material-icons" title="Exporter au format pdf">save</i>',
exportOptions: {
columns: [1, 2, 3, 4]
}
},
{
extend: 'excel',
className: 'btn btn-success btn-round btn-fab btn-fab-mini',
text: '<i class="material-icons" title="Exporter au format xls">save</i>',
exportOptions: {
columns: [1, 2, 3, 4]
}
},
{
text: '<i class="material-icons" title="Supprimer">delete</i>',
className: 'btn btn-danger btn-round btn-fab btn-fab-mini',
action: function (e, dt, node, config) {
that.checkSelect(dt);
}
},
{
text: '<i class="material-icons" title="Actualiser">replay</i>',
className: 'btn btn-primary btn-round btn-fab btn-fab-mini',
action: function (e, dt, node, config) {
that.refresh();
}
},
{
text: '<i class="material-icons" title="Nouveau">add</i>',
className: 'btn btn-warning btn-round btn-fab btn-fab-mini',
action: function (e, dt, node, config) {
that.crudContact(null, 1);
}
}
]
};
}
加载数据:
getContacts() {
const dtr: Contact[] = [];
this.myJarviaServices.getAllContacts()
.pipe(first())
.subscribe(res => {
this.contacts = res.content;
console.log(res.content);
this.contacts.forEach((item, index) => {
// dtr[index] = [];
dtr.push(item);
});
this.dataTable = {
headerRow: ['N°', 'Identifiant', 'Nom', 'Prénom'],
footerRow: ['N°', 'Identifiant', 'Nom', 'Prénom'],
dataRows: dtr
};
this.contactList = res.content;
this.loading = false;
},
error => {
console.log('error subscribe: ' + error);
this.error(error);
});
}
DataTable模型:
import {Contact} from './contact';
export interface DataTable {
headerRow: string[];
footerRow: string[];
dataRows: Contact[];
}
我的HTML文件:
<table id="example" datatable [dtOptions]="dtOptions" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th style="width: 1%">
<mat-checkbox (change)="$event ? masterToggle(dataTable.dataRows) : null"
[checked]="selection.hasValue() && isAllSelected(dataTable.dataRows.length)">
</mat-checkbox>
</th>
<th>{{ dataTable.headerRow[0] }}</th>
<th>{{ dataTable.headerRow[1] }}</th>
<th>{{ dataTable.headerRow[2] }}</th>
<th>{{ dataTable.headerRow[3] }}</th>
<th class="disabled-sorting text-right">Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Sélection</th>
<th>{{ dataTable.footerRow[0] }}</th>
<th>{{ dataTable.footerRow[1] }}</th>
<th>{{ dataTable.footerRow[2] }}</th>
<th>{{ dataTable.footerRow[3] }}</th>
<th class="text-right">Action</th>
</tr>
</tfoot>
<tbody>
<tr *ngFor="let row of dataTable.dataRows">
<td>
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)">
</mat-checkbox>
</td>
<!--<td>{{row[0]}}</td>-->
<td>{{row.idaicontact}}</td>
<td>{{row.identifiant}}</td>
<td>{{row.nom}}</td>
<td>{{row.prenom}}</td>
<td class="text-right">
<a (click)="crudContact(row, 2)" class="btn btn-link btn-success btn-just-icon" matTooltip="Mettre à jour" [matTooltipPosition]="'left'">
<i class="material-icons">create</i>
</a>
<a (click)="crudContact(row, 4)" class="btn btn-link btn-danger btn-just-icon" matTooltip="Supprimer" [matTooltipPosition]="'right'">
<i class="material-icons">delete</i>
</a>
<a (click)="crudContact(row, 5)" class="btn btn-link btn-info btn-just-icon" matTooltip="Visualiser" [matTooltipPosition]="'left'">
<i class="material-icons">visibility</i>
</a>
</td>
</tr>
</tbody>
</table>
我发现这种有角度的物质方式: https://stackblitz.com/edit/angular-hbakxo-xctfqy?file=app%2Ftable-filtering-example.ts
但是我不知道如何使它适应我的“混合”表...
是否可以应用单个列过滤?