Skip to content

Commit cb3d84d

Browse files
committed
Doco(tut): add single filter tut, fix #1765
1 parent 59f4771 commit cb3d84d

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

misc/tutorial/103_filtering.ngdoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ to make any columns with a filter condition have blue text.
7474

7575
### Single filter box (similar to 2.x)
7676

77-
Refer the {@link 499_FAQ FAQ}, it is possible to implement this using a rowsProcessor.
77+
Refer {@link 321_singleFilter singleFilter tutorial}, it is possible to implement this using a rowsProcessor.
7878

7979
@example
8080
<example module="app">

misc/tutorial/321_singleFilter.ngdoc

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
@ngdoc overview
2+
@name Tutorial: 321 Single filter
3+
@description
4+
5+
Provide a single filter box that searches across multiple columns in the grid.
6+
7+
@example
8+
<example module="app">
9+
<file name="app.js">
10+
var app = angular.module('app', ['ngTouch', 'ui.grid']);
11+
12+
app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
13+
var today = new Date();
14+
$scope.gridOptions = {
15+
enableFiltering: false,
16+
onRegisterApi: function(gridApi){
17+
$scope.gridApi = gridApi;
18+
$scope.gridApi.grid.registerRowsProcessor( $scope.singleFilter, 200 );
19+
},
20+
columnDefs: [
21+
{ field: 'name' },
22+
{ field: 'gender', cellFilter: 'mapGender' },
23+
{ field: 'company' },
24+
{ field: 'email' },
25+
{ field: 'phone' },
26+
{ field: 'age' },
27+
{ field: 'mixedDate' }
28+
]
29+
};
30+
31+
$http.get('/data/500_complex.json')
32+
.success(function(data) {
33+
$scope.gridOptions.data = data;
34+
$scope.gridOptions.data[0].age = -5;
35+
36+
data.forEach( function addDates( row, index ){
37+
row.mixedDate = new Date();
38+
row.mixedDate.setDate(today.getDate() + ( index % 14 ) );
39+
row.gender = row.gender==='male' ? '1' : '2';
40+
});
41+
});
42+
43+
$scope.filter = function() {
44+
$scope.gridApi.grid.refresh();
45+
};
46+
47+
$scope.singleFilter = function( renderableRows ){
48+
var matcher = new RegExp($scope.filterValue);
49+
renderableRows.forEach( function( row ) {
50+
var match = false;
51+
[ 'name', 'company', 'email' ].forEach(function( field ){
52+
if ( row.entity[field].match(matcher) ){
53+
match = true;
54+
}
55+
});
56+
if ( !match ){
57+
row.visible = false;
58+
}
59+
});
60+
return renderableRows;
61+
};
62+
}])
63+
.filter('mapGender', function() {
64+
var genderHash = {
65+
1: 'male',
66+
2: 'female'
67+
};
68+
69+
return function(input) {
70+
if (!input){
71+
return '';
72+
} else {
73+
return genderHash[input];
74+
}
75+
};
76+
});
77+
78+
</file>
79+
<file name="index.html">
80+
<div ng-controller="MainCtrl">
81+
<input ng-model='filterValue'/><button ng-click='filter()'>Filter</button>
82+
<div id="grid1" ui-grid="gridOptions" class="grid"></div>
83+
</div>
84+
</file>
85+
<file name="main.css">
86+
.grid {
87+
width: 500px;
88+
height: 450px;
89+
}
90+
</file>
91+
</example>

misc/tutorial/499_FAQ.ngdoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ There are a number of common gotchas in using the grid, this FAQ aims to cover m
8484
if each row should be visible, and appropriately set the visible flag.
8585

8686
This is a little tricky to build as a generic feature, but for specific use cases would be reasonably
87-
simple.
87+
simple. Refer the {@link 321_singleFilter single filter tutorial}.
8888

0 commit comments

Comments
 (0)