annotate DataTables-1.9.4/media/src/core/core.filter.js @ 0:ac5f9272033b draft

first upload
author saskia-hiltemann
date Tue, 01 Jul 2014 11:42:23 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
1
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
2
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
3 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
4 * Generate the node required for filtering text
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
5 * @returns {node} Filter control element
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
6 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
7 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
8 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
9 function _fnFeatureHtmlFilter ( oSettings )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
10 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
11 var oPreviousSearch = oSettings.oPreviousSearch;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
12
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
13 var sSearchStr = oSettings.oLanguage.sSearch;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
14 sSearchStr = (sSearchStr.indexOf('_INPUT_') !== -1) ?
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
15 sSearchStr.replace('_INPUT_', '<input type="text" />') :
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
16 sSearchStr==="" ? '<input type="text" />' : sSearchStr+' <input type="text" />';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
17
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
18 var nFilter = document.createElement( 'div' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
19 nFilter.className = oSettings.oClasses.sFilter;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
20 nFilter.innerHTML = '<label>'+sSearchStr+'</label>';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
21 if ( !oSettings.aanFeatures.f )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
22 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
23 nFilter.id = oSettings.sTableId+'_filter';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
24 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
25
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
26 var jqFilter = $('input[type="text"]', nFilter);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
27
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
28 // Store a reference to the input element, so other input elements could be
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
29 // added to the filter wrapper if needed (submit button for example)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
30 nFilter._DT_Input = jqFilter[0];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
31
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
32 jqFilter.val( oPreviousSearch.sSearch.replace('"','&quot;') );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
33 jqFilter.bind( 'keyup.DT', function(e) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
34 /* Update all other filter input elements for the new display */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
35 var n = oSettings.aanFeatures.f;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
36 var val = this.value==="" ? "" : this.value; // mental IE8 fix :-(
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
37
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
38 for ( var i=0, iLen=n.length ; i<iLen ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
39 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
40 if ( n[i] != $(this).parents('div.dataTables_filter')[0] )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
41 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
42 $(n[i]._DT_Input).val( val );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
43 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
44 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
45
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
46 /* Now do the filter */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
47 if ( val != oPreviousSearch.sSearch )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
48 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
49 _fnFilterComplete( oSettings, {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
50 "sSearch": val,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
51 "bRegex": oPreviousSearch.bRegex,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
52 "bSmart": oPreviousSearch.bSmart ,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
53 "bCaseInsensitive": oPreviousSearch.bCaseInsensitive
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
54 } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
55 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
56 } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
57
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
58 jqFilter
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
59 .attr('aria-controls', oSettings.sTableId)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
60 .bind( 'keypress.DT', function(e) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
61 /* Prevent form submission */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
62 if ( e.keyCode == 13 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
63 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
64 return false;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
65 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
66 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
67 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
68
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
69 return nFilter;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
70 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
71
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
72
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
73 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
74 * Filter the table using both the global filter and column based filtering
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
75 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
76 * @param {object} oSearch search information
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
77 * @param {int} [iForce] force a research of the master array (1) or not (undefined or 0)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
78 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
79 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
80 function _fnFilterComplete ( oSettings, oInput, iForce )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
81 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
82 var oPrevSearch = oSettings.oPreviousSearch;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
83 var aoPrevSearch = oSettings.aoPreSearchCols;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
84 var fnSaveFilter = function ( oFilter ) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
85 /* Save the filtering values */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
86 oPrevSearch.sSearch = oFilter.sSearch;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
87 oPrevSearch.bRegex = oFilter.bRegex;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
88 oPrevSearch.bSmart = oFilter.bSmart;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
89 oPrevSearch.bCaseInsensitive = oFilter.bCaseInsensitive;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
90 };
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
91
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
92 /* In server-side processing all filtering is done by the server, so no point hanging around here */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
93 if ( !oSettings.oFeatures.bServerSide )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
94 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
95 /* Global filter */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
96 _fnFilter( oSettings, oInput.sSearch, iForce, oInput.bRegex, oInput.bSmart, oInput.bCaseInsensitive );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
97 fnSaveFilter( oInput );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
98
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
99 /* Now do the individual column filter */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
100 for ( var i=0 ; i<oSettings.aoPreSearchCols.length ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
101 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
102 _fnFilterColumn( oSettings, aoPrevSearch[i].sSearch, i, aoPrevSearch[i].bRegex,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
103 aoPrevSearch[i].bSmart, aoPrevSearch[i].bCaseInsensitive );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
104 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
105
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
106 /* Custom filtering */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
107 _fnFilterCustom( oSettings );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
108 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
109 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
110 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
111 fnSaveFilter( oInput );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
112 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
113
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
114 /* Tell the draw function we have been filtering */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
115 oSettings.bFiltered = true;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
116 $(oSettings.oInstance).trigger('filter', oSettings);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
117
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
118 /* Redraw the table */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
119 oSettings._iDisplayStart = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
120 _fnCalculateEnd( oSettings );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
121 _fnDraw( oSettings );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
122
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
123 /* Rebuild search array 'offline' */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
124 _fnBuildSearchArray( oSettings, 0 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
125 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
126
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
127
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
128 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
129 * Apply custom filtering functions
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
130 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
131 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
132 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
133 function _fnFilterCustom( oSettings )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
134 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
135 var afnFilters = DataTable.ext.afnFiltering;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
136 var aiFilterColumns = _fnGetColumns( oSettings, 'bSearchable' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
137
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
138 for ( var i=0, iLen=afnFilters.length ; i<iLen ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
139 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
140 var iCorrector = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
141 for ( var j=0, jLen=oSettings.aiDisplay.length ; j<jLen ; j++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
142 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
143 var iDisIndex = oSettings.aiDisplay[j-iCorrector];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
144 var bTest = afnFilters[i](
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
145 oSettings,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
146 _fnGetRowData( oSettings, iDisIndex, 'filter', aiFilterColumns ),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
147 iDisIndex
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
148 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
149
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
150 /* Check if we should use this row based on the filtering function */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
151 if ( !bTest )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
152 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
153 oSettings.aiDisplay.splice( j-iCorrector, 1 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
154 iCorrector++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
155 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
156 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
157 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
158 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
159
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
160
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
161 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
162 * Filter the table on a per-column basis
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
163 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
164 * @param {string} sInput string to filter on
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
165 * @param {int} iColumn column to filter
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
166 * @param {bool} bRegex treat search string as a regular expression or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
167 * @param {bool} bSmart use smart filtering or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
168 * @param {bool} bCaseInsensitive Do case insenstive matching or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
169 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
170 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
171 function _fnFilterColumn ( oSettings, sInput, iColumn, bRegex, bSmart, bCaseInsensitive )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
172 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
173 if ( sInput === "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
174 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
175 return;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
176 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
177
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
178 var iIndexCorrector = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
179 var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart, bCaseInsensitive );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
180
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
181 for ( var i=oSettings.aiDisplay.length-1 ; i>=0 ; i-- )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
182 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
183 var sData = _fnDataToSearch( _fnGetCellData( oSettings, oSettings.aiDisplay[i], iColumn, 'filter' ),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
184 oSettings.aoColumns[iColumn].sType );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
185 if ( ! rpSearch.test( sData ) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
186 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
187 oSettings.aiDisplay.splice( i, 1 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
188 iIndexCorrector++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
189 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
190 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
191 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
192
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
193
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
194 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
195 * Filter the data table based on user input and draw the table
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
196 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
197 * @param {string} sInput string to filter on
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
198 * @param {int} iForce optional - force a research of the master array (1) or not (undefined or 0)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
199 * @param {bool} bRegex treat as a regular expression or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
200 * @param {bool} bSmart perform smart filtering or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
201 * @param {bool} bCaseInsensitive Do case insenstive matching or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
202 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
203 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
204 function _fnFilter( oSettings, sInput, iForce, bRegex, bSmart, bCaseInsensitive )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
205 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
206 var i;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
207 var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart, bCaseInsensitive );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
208 var oPrevSearch = oSettings.oPreviousSearch;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
209
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
210 /* Check if we are forcing or not - optional parameter */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
211 if ( !iForce )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
212 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
213 iForce = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
214 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
215
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
216 /* Need to take account of custom filtering functions - always filter */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
217 if ( DataTable.ext.afnFiltering.length !== 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
218 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
219 iForce = 1;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
220 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
221
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
222 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
223 * If the input is blank - we want the full data set
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
224 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
225 if ( sInput.length <= 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
226 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
227 oSettings.aiDisplay.splice( 0, oSettings.aiDisplay.length);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
228 oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
229 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
230 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
231 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
232 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
233 * We are starting a new search or the new search string is smaller
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
234 * then the old one (i.e. delete). Search from the master array
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
235 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
236 if ( oSettings.aiDisplay.length == oSettings.aiDisplayMaster.length ||
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
237 oPrevSearch.sSearch.length > sInput.length || iForce == 1 ||
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
238 sInput.indexOf(oPrevSearch.sSearch) !== 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
239 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
240 /* Nuke the old display array - we are going to rebuild it */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
241 oSettings.aiDisplay.splice( 0, oSettings.aiDisplay.length);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
242
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
243 /* Force a rebuild of the search array */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
244 _fnBuildSearchArray( oSettings, 1 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
245
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
246 /* Search through all records to populate the search array
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
247 * The the oSettings.aiDisplayMaster and asDataSearch arrays have 1 to 1
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
248 * mapping
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
249 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
250 for ( i=0 ; i<oSettings.aiDisplayMaster.length ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
251 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
252 if ( rpSearch.test(oSettings.asDataSearch[i]) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
253 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
254 oSettings.aiDisplay.push( oSettings.aiDisplayMaster[i] );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
255 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
256 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
257 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
258 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
259 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
260 /* Using old search array - refine it - do it this way for speed
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
261 * Don't have to search the whole master array again
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
262 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
263 var iIndexCorrector = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
264
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
265 /* Search the current results */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
266 for ( i=0 ; i<oSettings.asDataSearch.length ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
267 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
268 if ( ! rpSearch.test(oSettings.asDataSearch[i]) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
269 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
270 oSettings.aiDisplay.splice( i-iIndexCorrector, 1 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
271 iIndexCorrector++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
272 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
273 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
274 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
275 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
276 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
277
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
278
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
279 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
280 * Create an array which can be quickly search through
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
281 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
282 * @param {int} iMaster use the master data array - optional
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
283 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
284 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
285 function _fnBuildSearchArray ( oSettings, iMaster )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
286 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
287 if ( !oSettings.oFeatures.bServerSide )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
288 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
289 /* Clear out the old data */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
290 oSettings.asDataSearch = [];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
291
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
292 var aiFilterColumns = _fnGetColumns( oSettings, 'bSearchable' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
293 var aiIndex = (iMaster===1) ?
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
294 oSettings.aiDisplayMaster :
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
295 oSettings.aiDisplay;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
296
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
297 for ( var i=0, iLen=aiIndex.length ; i<iLen ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
298 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
299 oSettings.asDataSearch[i] = _fnBuildSearchRow(
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
300 oSettings,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
301 _fnGetRowData( oSettings, aiIndex[i], 'filter', aiFilterColumns )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
302 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
303 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
304 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
305 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
306
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
307
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
308 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
309 * Create a searchable string from a single data row
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
310 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
311 * @param {array} aData Row data array to use for the data to search
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
312 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
313 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
314 function _fnBuildSearchRow( oSettings, aData )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
315 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
316 var sSearch = aData.join(' ');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
317
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
318 /* If it looks like there is an HTML entity in the string, attempt to decode it */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
319 if ( sSearch.indexOf('&') !== -1 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
320 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
321 sSearch = $('<div>').html(sSearch).text();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
322 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
323
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
324 // Strip newline characters
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
325 return sSearch.replace( /[\n\r]/g, " " );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
326 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
327
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
328 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
329 * Build a regular expression object suitable for searching a table
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
330 * @param {string} sSearch string to search for
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
331 * @param {bool} bRegex treat as a regular expression or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
332 * @param {bool} bSmart perform smart filtering or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
333 * @param {bool} bCaseInsensitive Do case insensitive matching or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
334 * @returns {RegExp} constructed object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
335 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
336 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
337 function _fnFilterCreateSearch( sSearch, bRegex, bSmart, bCaseInsensitive )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
338 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
339 var asSearch, sRegExpString;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
340
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
341 if ( bSmart )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
342 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
343 /* Generate the regular expression to use. Something along the lines of:
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
344 * ^(?=.*?\bone\b)(?=.*?\btwo\b)(?=.*?\bthree\b).*$
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
345 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
346 asSearch = bRegex ? sSearch.split( ' ' ) : _fnEscapeRegex( sSearch ).split( ' ' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
347 sRegExpString = '^(?=.*?'+asSearch.join( ')(?=.*?' )+').*$';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
348 return new RegExp( sRegExpString, bCaseInsensitive ? "i" : "" );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
349 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
350 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
351 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
352 sSearch = bRegex ? sSearch : _fnEscapeRegex( sSearch );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
353 return new RegExp( sSearch, bCaseInsensitive ? "i" : "" );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
354 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
355 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
356
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
357
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
358 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
359 * Convert raw data into something that the user can search on
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
360 * @param {string} sData data to be modified
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
361 * @param {string} sType data type
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
362 * @returns {string} search string
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
363 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
364 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
365 function _fnDataToSearch ( sData, sType )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
366 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
367 if ( typeof DataTable.ext.ofnSearch[sType] === "function" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
368 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
369 return DataTable.ext.ofnSearch[sType]( sData );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
370 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
371 else if ( sData === null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
372 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
373 return '';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
374 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
375 else if ( sType == "html" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
376 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
377 return sData.replace(/[\r\n]/g," ").replace( /<.*?>/g, "" );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
378 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
379 else if ( typeof sData === "string" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
380 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
381 return sData.replace(/[\r\n]/g," ");
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
382 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
383 return sData;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
384 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
385
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
386
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
387 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
388 * scape a string such that it can be used in a regular expression
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
389 * @param {string} sVal string to escape
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
390 * @returns {string} escaped string
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
391 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
392 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
393 function _fnEscapeRegex ( sVal )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
394 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
395 var acEscape = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\', '$', '^', '-' ];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
396 var reReplace = new RegExp( '(\\' + acEscape.join('|\\') + ')', 'g' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
397 return sVal.replace(reReplace, '\\$1');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
398 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
399