从单元格模板中搜索文本不能在ui-grid中工作

时间:2016-04-20 06:28:33

标签: angularjs angular-ui-grid celltemplate

我为其中一个列定义了条件单元格模板。它正确显示数据,但我无法在单元格模板中搜索文本。 这是我的傻瓜: https://plnkr.co/edit/TDX5jtPord1hkzCVaw3L?p=preview

var template1 = '<div class="">' + 
'<div class="" ng-if="COL_FIELD > 30">Greater </div> ' +
'<div class="" ng-if="COL_FIELD < 30"> Lesser </div> ' +
'</div>';

在模板中我已经把条件设为..如果COL_FIELD&gt; 30然后再写大..或者写小一点。现在,我应该能够搜索更大或更小的数字列。

enter image description here

2 个答案:

答案 0 :(得分:2)

解决方案可能是在您的数据上添加属性,如:

$http.get('data.json').success(function(data) {
    data.map(function(item) {
        item.greaterLesser = item.amount > 30 ? 'Greater' : 'Lesser';
    });
    $scope.gridOptions.data = data;
});

然后不使用带模板的金额,只需绑定此属性即可。

$scope.gridOptions = {
    enableFiltering: true,
    columnDefs: [{
      field: 'name',
      width: 70
    }, {
      field: 'greaterLesser',
      name: 'Number',
      width: 90,
    }, {
      field: 'amount',
      name: 'Currency',
      cellFilter: 'currencyFilter:this',
    }]
};

以下是updated plunker

修改

如果您想使用该模板,您可以自己实现搜索功能。您可以在字段中添加filter选项并实施condition功能。类似的东西:

filter: {
    condition: function(searchTerm, cellValue) {
        var value = cellValue > 30 ? 'greater' : 'lesser';
        var result = value.search(searchTerm.toLowerCase());
        return result > -1;
    }
}

这里我使用了search函数,但你可以使用match或其他一些函数。 这是demo plunker

答案 1 :(得分:0)

我使用下面的代码使搜索有效

field: 'EmpId', 
displayName: 'Employee Type', 
cellTemplate: '<div style="cursor:pointer"  class="ui-grid-cell-contents">{{grid.appScope.GetEmployeeType(row)}}</div>', 
filter: {
            condition: function (searchTerm, cellValue,row,column) {
            var value = $scope.GetEmployeeType(row);//same function that you use to display value in cell Template
            return (value.toLocaleLowerCase().indexOf(searchTerm.toLocaleLowerCase())>-1);
                            }
                        }