Angularjs 当columnDefs将字段作为类型编号时,Angular ui grid将按字符串过滤。为什么?
我正在使用django 1.8、angularjs 1.3.14和jquery 1.11.0 这在控制器/gridOptions/columnDefs中Angularjs 当columnDefs将字段作为类型编号时,Angular ui grid将按字符串过滤。为什么?,angularjs,django,user-interface,grid,angular-ui-grid,Angularjs,Django,User Interface,Grid,Angular Ui Grid,我正在使用django 1.8、angularjs 1.3.14和jquery 1.11.0 这在控制器/gridOptions/columnDefs中 { field: 'credit_amt', displayName: 'Credit Amount', type: 'number', width: '8%', enableFocusedCellEdit: true, visible:true, //Th
{ field: 'credit_amt',
displayName: 'Credit Amount',
type: 'number',
width: '8%',
enableFocusedCellEdit: true,
visible:true,
//This 'filters' is the sort box to do the search.
filters: [{
condition: uiGridConstants.filter.GREATER_THAN,
placeholder: 'greater than'
}
请注意,“type”是一个数字。当我运行此程序时,程序将此字段视为字符串而不是数字。所以排序没有按我需要的方式工作。我试着省去“type”,让它自动检测数据类型-没用 以下是使用前后的排序结果: 如您所见,当所有数据都不小于6时,项目被过滤。
请帮忙。谢谢。您可以使用自己的条件功能
condition: function(term, value, row, column){
if(!term) return true;
return parseFloat(value) > parseFloat(term);
}
您使用的是什么版本的angular ui grid。我刚刚做了一个plnkr,用类似的数据进行排序和过滤,效果很好。所以这可能是你这边的版本问题
var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid']);
app.controller('MainCtrl', ['$scope', '$http', 'uiGridConstants', function ($scope, $http, uiGridConstants) {
$scope.gridOptions1 = {
enableSorting: true,
enableFiltering:true,
columnDefs: [
{ field: 'credit_amount',
displayName: 'Credit Amount',
type: 'number',
enableFocusedCellEdit: true,
visible:true,
//This 'filters' is the sort box to do the search.
filters: [{
condition: uiGridConstants.filter.GREATER_THAN,
placeholder: 'greater than'
}
]
}]
};
$scope.gridOptions1.data = [{
credit_amount:1000.02
},{
credit_amount:1001.0
},{
credit_amount:100.0
},{
credit_amount:500.0
}]
}]);
我正在扩展@Qi Tang的答案,以防有像我这样的人在寻找一个过滤结果仅等于过滤值(而不是更大)的答案 对于其他实现,请注意:
if(!term) return true;
对于任何falsy值,都将返回true,请确保这是您想要的。这不仅包括null和undefined,还包括false和0,,这在数字筛选中可能不可取(我无法筛选项目为0的位置)。问题在于筛选。我设置了“type:”number“并尝试了numberStr。两者都不起作用。该列仍按字符串值而不是数字进行筛选。谢谢。这就是我所实现的,并且成功了。我不需要包括“行”或“列”。无论是否使用“if”语句,它都可以工作。如果您不介意的话,If语句做什么呢?我想如果term为null,parseFloat将抛出一个异常。但似乎并非如此。所以我认为你可以删除if语句。if语句非常重要。如果在筛选器中添加了某些内容,然后将其删除,则搜索词将变为null或未定义。使过滤器不再工作。所以//如果(!term)term='0';//事实证明,这是一个非常重要的字段,因此,如果用户从过滤器输入中添加然后删除数据,请设置该值。
if(!term) return true;