1. 程式人生 > >ngTable自定義/重寫過濾器

ngTable自定義/重寫過濾器

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", "sex": "男", "date": 1466570428580 }, { "username"
: "CCC", "sex": "女", "date": 1584570428580 }]; $scope.userTable = new NgTableParams({ sorting: {id: "asc"} }, { // initial sort order 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; }) } })