前端之家收集整理的这篇文章主要介绍了
ngTable自定义/重写过滤器,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<script type="text/ng-template" id="/filter/js/startDateFilter.html"> <input type="text"> </script>
$scope.customFilter = {
start:{
id:'/filter/js/startDateFilter.html',placeholder:'Start'
}
}
$scope.userTable = new NgTableParams({},{
filterDelay: 0,filterOptions: {filterFn: $scope.customFilterFn},dataset: data
});
<td data-title="'日期'" filter="customFilter"> {{user.date|date:'yyyy-MM-dd'}} </td>
$scope.customFilterFn = function (date,filterValues) {
return data.filter(function (item) {
var result = true;
if (typeof(filterValues.name) != undefined && filterValues.name != null) {
result = result && (item.name.indenOf(filterValues.name) > -1);
}
if (typeof(filterValues.sex) != undefined && filterValues.sex != null) {
result = result && (item.sex == filterValues.sex);
}
if (typeof(filterValues.startDate) != undefined && filterValues.startDate != null) {
result = result && (item.startDate >= filterValues.startDate);
}
return result;
})
}
<!DOCTYPE html> <html lang="en" ng-app="myApp" ng-controller="myCtrl"> <head> <Meta charset="UTF-8"> <title>自定义过滤器</title> </head> <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.bootcss.com/ng-table/1.0.0/ng-table.min.css" rel="stylesheet"> <body> <div class="row"> <script type="text/ng-template" id="dateFilter.html"> <input type="date"> </script> <div class="col-md-12 table-responsive"> <table ng-table="userTable" class="table table-condensed table-bordered table-striped table-hover " show-filter="true"> <tr ng-repeat="user in $data"> <td data-title="'用户名'" filter="{username: 'text'}" sortable="'username'">{{user.username}}</td> <td data-title="'性别'" filter="{sex: 'select'}" filter-data="sexs" sortable="'sex'">{{user.sex}}</td> <td data-title="'性别'" filter="customFilter" sortable="'date'"> {{user.date|date:'yyyy-MM-dd'}} </td> </tr> </table> </div> </div> </body> <script type="text/ng-template" id="/filter/js/startDateFilter.html"> <input type="text"> </script> <script src="https://cdn.bootcss.com/angular.js/1.5.8/angular.min.js"></script> <script src="https://cdn.bootcss.com/ng-table/1.0.0/ng-table.min.js"></script> <script type="text/javascript"> var app = angular.module('myApp',['ngTable']); app.controller('myCtrl',function ($scope,NgTableParams) { $scope.sexs = [{ "id": "男","title": "男" },{ "id": "女","title": "女" }] var data = [{ "username": "农夫山泉","sex": "男","date": 1004570428580 },{ "username": "哇哈哈","sex": "女","date": 1784570428580 },{ "username": "Alice","date": 1466570428580 },{ "username": "CCC","date": 1584570428580 }]; $scope.userTable = new NgTableParams({ sorting: {id: "asc"} },{ filterDelay: 0,filterOptions: {filterFn: $scope.customFilterFn},dataset: data }); $scope.customFilter = { start:{ id:'/filter/js/startDateFilter.html',placeholder:'Start' } } $scope.customFilterFn = function (date,filterValues) { return data.filter(function (item) { var result = true; console.log(filterValues); if (typeof(filterValues.name) != undefined && filterValues.name != null) { result = result && (item.name.indenOf(filterValues.name) > -1); } if (typeof(filterValues.sex) != undefined && filterValues.sex != null) { result = result && (item.sex == filterValues.sex); } if (typeof(filterValues.startDate) != undefined && filterValues.startDate != null) { result = result && (item.startDate >= filterValues.startDate); } return result; }) } }) </script> </html>
原文链接:https://www.f2er.com/angularjs/147223.html