使用 ng-table 和 selectize 自定义过滤/排序
Custom filtering / ordering with ng-table and selectize
我的 ng-table 列之一包含一个 selectize 下拉列表。此列的值是另一个 table 的外键。我不想显示此键,而是希望显示此键代表的行的另一个属性(名称)。
我的数据在一个名为 table.
的数组中
我使用另一个数组 fkTable 正确显示了下拉菜单。
<selectize ng-model="row[col.name]"
options='fkTable'
config='fkConfig'">
其中 fkConfig:
$scope.fkConfig = {
maxItems: 1,
valueField: 'id',
labelField: 'name',
searchField: 'name'
};
现在我希望能够根据外部行名称而不是 ID 来过滤和排序此列。
我试图将 ID 映射到他们的名字:
$scope.foreignRowNames = {
0:"not grouped"
1:"Google"
14:"Youtube"
}
并为此特定列创建自定义过滤器:
function filterSelectizeColumn(table, searchTerm) {
for (var i = 0; i < table.length; i++) {
var fkValue = table[i].fk;
var foreignRowName = $scope.foreignRowNames[fkValue]];
if (foreignRowName.indexOf(searchTerm) == -1) {
table = table.splice(i, 1);
}
}
}
但这似乎是一种笨拙且低效的处理方式,由于这两个库的流行,我认为这是一个有点普遍的问题。
我的问题是如何有效地为此外键列创建自定义过滤器。
我采用的解决方案:
- 用相应的名称替换所有外键
- 进行编辑时,将名称转换回它们所代表的键
我的 ng-table 列之一包含一个 selectize 下拉列表。此列的值是另一个 table 的外键。我不想显示此键,而是希望显示此键代表的行的另一个属性(名称)。 我的数据在一个名为 table.
的数组中我使用另一个数组 fkTable 正确显示了下拉菜单。
<selectize ng-model="row[col.name]"
options='fkTable'
config='fkConfig'">
其中 fkConfig:
$scope.fkConfig = {
maxItems: 1,
valueField: 'id',
labelField: 'name',
searchField: 'name'
};
现在我希望能够根据外部行名称而不是 ID 来过滤和排序此列。
我试图将 ID 映射到他们的名字:
$scope.foreignRowNames = {
0:"not grouped"
1:"Google"
14:"Youtube"
}
并为此特定列创建自定义过滤器:
function filterSelectizeColumn(table, searchTerm) {
for (var i = 0; i < table.length; i++) {
var fkValue = table[i].fk;
var foreignRowName = $scope.foreignRowNames[fkValue]];
if (foreignRowName.indexOf(searchTerm) == -1) {
table = table.splice(i, 1);
}
}
}
但这似乎是一种笨拙且低效的处理方式,由于这两个库的流行,我认为这是一个有点普遍的问题。
我的问题是如何有效地为此外键列创建自定义过滤器。
我采用的解决方案:
- 用相应的名称替换所有外键
- 进行编辑时,将名称转换回它们所代表的键