我正在尝试将过滤器应用于我的数据表。
<div class="widget-content table-container" ng-controller="getEmployeesController" style="overflow:auto">
<table ng-table="employeesList" show-filter="true" class="table table-striped table-bordered">
<tr ng-repeat="employee in $data">
<td data-title="'#'">
{{ (itemsPerPage * (pageNumber-1)) + $index+1 }}
</td>
<td data-title="'Name'" sortable="'Name'" filter="{ 'firstName+lastName': 'text' }">
{{employee.firstName +""+employee.lastName}}
</td>
<td data-title="'First Name'" sortable="'firstName'" filter="{ 'firstName': 'text' }">
{{employee.firstName}}
</td>
<td data-title="'Last Name'" sortable="'lastName'" filter="{ 'lastName': 'text' }">
{{employee.lastName}}
</td>
</tr>
</table>
</div>
第2和第3次过滤效果很好。我想要一个TD,其中第一个名称和第二个名称连接在一起。
App.js
//Datatable
myApp.factory('dataTable', ['$filter', 'ngTableParams', '$rootScope', function ($filter, ngTableParams, $rootScope) {
var factoryDefinition = {
render: function($scope, config, componentId, data) {
if (!config) config = { filter: { 'firstName': $rootScope.mysearch } };
var config = angular.extend({}, {page:1, count:10}, config)
$scope[componentId] = new ngTableParams(config, {
total: data.length, // length of data
getData: function ($defer, params) {
// organize filter as $filter understand it (graph object)
var filters = {};
var val = $rootScope.mysearch;
var key = "firstname";
$rootScope.mysearch = "";
angular.forEach(params.filter(), function (val, key) {
var filter = filters;
var parts = key.split('.');
for (var i = 0; i < parts.length; i++) {
if (i != parts.length - 1) {
filter[parts[i]] = {};
filter = filter[parts[i]];
}
else {
filter[parts[i]] = val;
}
}
});
// use build-in angular filter
var filteredData = params.filter() ?
$filter('filter')(data, filters) :
data;
var orderedData = params.sorting() ?
$filter('orderBy')(filteredData, params.orderBy()) :
data;
params.total(orderedData.length); // set total for recalc pagination
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
$scope.pageNumber = params.page();
$scope.itemsPerPage = params.count();
}
});
}
}
return factoryDefinition;
}
]);
myApp.filter('customFilter', ['$filter', function ($filter) {
var filterFilter = $filter('filter');
var standardComparator = function standardComparator(obj, text) {
text = ('' + text).toLowerCase();
return ('' + obj).toLowerCase().indexOf(text) > -1;
};
return function customFilter(array, expression) {
function customComparator(actual, expected) {
var isBeforeActivated = expected.before;
var isAfterActivated = expected.after;
var isLower = expected.lower;
var isHigher = expected.higher;
var higherLimit;
var lowerLimit;
var itemDate;
var queryDate;
if (ng.isObject(expected)) {
//exact match
if (expected.distinct) {
if (!actual || actual.toLowerCase() !== expected.distinct.toLowerCase()) {
return false;
}
return true;
}
//matchAny
if (expected.matchAny) {
if (expected.matchAny.all) {
return true;
}
if (!actual) {
return false;
}
for (var i = 0; i < expected.matchAny.items.length; i++) {
if (actual.toLowerCase() === expected.matchAny.items[i].toLowerCase()) {
return true;
}
}
return false;
}
//date range
if (expected.before || expected.after) {
try {
if (isBeforeActivated) {
higherLimit = expected.before;
itemDate = new Date(actual);
queryDate = new Date(higherLimit);
if (itemDate > queryDate) {
return false;
}
}
if (isAfterActivated) {
lowerLimit = expected.after;
itemDate = new Date(actual);
queryDate = new Date(lowerLimit);
if (itemDate < queryDate) {
return false;
}
}
return true;
} catch (e) {
return false;
}
} else if (isLower || isHigher) {
//number range
if (isLower) {
higherLimit = expected.lower;
if (actual > higherLimit) {
return false;
}
}
if (isHigher) {
lowerLimit = expected.higher;
if (actual < lowerLimit) {
return false;
}
}
return true;
}
//etc
return true;
}
return standardComparator(actual, expected);
}
var output = filterFilter(array, expression, customComparator);
return output;
};
}]);
控制器
myApp.controller('getEmployeesController', ['$scope', 'employeeServices', 'dataTable', '$window', '$timeout', '$filter', '$rootScope', 'ngDialog',
function ($scope, employeeServices, dataTable, $window, $timeout, $filter, $rootScope, ngDialog) {
employeeServices.getEmployees().then(function (result) {
$scope.data = result.data;
});
连锁的TD未正确过滤。我该怎么做才能使它发挥作用?
答案 0 :(得分:0)
找到解决方案,
在集合中创建一个新属性,然后将其设置为连接对象以解决问题。 控制器:
myApp.controller('getEmployeesController', ['$scope', 'employeeServices', 'dataTable', '$window', '$timeout', '$filter', '$rootScope', 'ngDialog',
function ($scope, employeeServices, dataTable, $window, $timeout, $filter, $rootScope, ngDialog) {
employeeServices.getEmployees().then(function (result) {
$scope.data = result.data;
angular.forEach(result.data, function (value, key) {
if (value.lastName == undefined) {
}
var fullName = value.firstName + '' + ((value.lastName != undefined) ? (value.lastName) : (''));
result.data[key].fullName = fullName;
});
});
<强> HTML 强>:
<td data-title="'Name'" sortable="'firstName'" filter="{ 'fullName': 'text' }" style="width:100px">
{{employee.fullName}}
</td>