annotate DataTables-1.9.4/media/src/core/core.ajax.js @ 9:7300ed4c1481 draft default tip

Uploaded
author saskia-hiltemann
date Mon, 04 Sep 2017 10:49:00 -0400
parents ac5f9272033b
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 * Update the table using an Ajax call
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
5 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
6 * @returns {boolean} Block the table drawing or not
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 _fnAjaxUpdate( oSettings )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
10 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
11 if ( oSettings.bAjaxDataGet )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
12 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
13 oSettings.iDraw++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
14 _fnProcessingDisplay( oSettings, true );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
15 var iColumns = oSettings.aoColumns.length;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
16 var aoData = _fnAjaxParameters( oSettings );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
17 _fnServerParams( oSettings, aoData );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
18
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
19 oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aoData,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
20 function(json) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
21 _fnAjaxUpdateDraw( oSettings, json );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
22 }, oSettings );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
23 return false;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
24 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
25 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
26 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
27 return true;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
28 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
29 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
30
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
31
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
32 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
33 * Build up the parameters in an object needed for a server-side processing request
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
34 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
35 * @returns {bool} block the table drawing or not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
36 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
37 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
38 function _fnAjaxParameters( oSettings )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
39 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
40 var iColumns = oSettings.aoColumns.length;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
41 var aoData = [], mDataProp, aaSort, aDataSort;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
42 var i, j;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
43
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
44 aoData.push( { "name": "sEcho", "value": oSettings.iDraw } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
45 aoData.push( { "name": "iColumns", "value": iColumns } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
46 aoData.push( { "name": "sColumns", "value": _fnColumnOrdering(oSettings) } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
47 aoData.push( { "name": "iDisplayStart", "value": oSettings._iDisplayStart } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
48 aoData.push( { "name": "iDisplayLength", "value": oSettings.oFeatures.bPaginate !== false ?
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
49 oSettings._iDisplayLength : -1 } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
50
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
51 for ( i=0 ; i<iColumns ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
52 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
53 mDataProp = oSettings.aoColumns[i].mData;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
54 aoData.push( { "name": "mDataProp_"+i, "value": typeof(mDataProp)==="function" ? 'function' : mDataProp } );
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 /* Filtering */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
58 if ( oSettings.oFeatures.bFilter !== false )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
59 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
60 aoData.push( { "name": "sSearch", "value": oSettings.oPreviousSearch.sSearch } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
61 aoData.push( { "name": "bRegex", "value": oSettings.oPreviousSearch.bRegex } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
62 for ( i=0 ; i<iColumns ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
63 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
64 aoData.push( { "name": "sSearch_"+i, "value": oSettings.aoPreSearchCols[i].sSearch } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
65 aoData.push( { "name": "bRegex_"+i, "value": oSettings.aoPreSearchCols[i].bRegex } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
66 aoData.push( { "name": "bSearchable_"+i, "value": oSettings.aoColumns[i].bSearchable } );
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
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
70 /* Sorting */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
71 if ( oSettings.oFeatures.bSort !== false )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
72 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
73 var iCounter = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
74
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
75 aaSort = ( oSettings.aaSortingFixed !== null ) ?
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
76 oSettings.aaSortingFixed.concat( oSettings.aaSorting ) :
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
77 oSettings.aaSorting.slice();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
78
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
79 for ( i=0 ; i<aaSort.length ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
80 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
81 aDataSort = oSettings.aoColumns[ aaSort[i][0] ].aDataSort;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
82
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
83 for ( j=0 ; j<aDataSort.length ; j++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
84 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
85 aoData.push( { "name": "iSortCol_"+iCounter, "value": aDataSort[j] } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
86 aoData.push( { "name": "sSortDir_"+iCounter, "value": aaSort[i][1] } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
87 iCounter++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
88 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
89 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
90 aoData.push( { "name": "iSortingCols", "value": iCounter } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
91
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
92 for ( i=0 ; i<iColumns ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
93 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
94 aoData.push( { "name": "bSortable_"+i, "value": oSettings.aoColumns[i].bSortable } );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
95 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
96 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
97
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
98 return aoData;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
99 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
100
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
101
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
102 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
103 * Add Ajax parameters from plug-ins
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
104 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
105 * @param array {objects} aoData name/value pairs to send to the server
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
106 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
107 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
108 function _fnServerParams( oSettings, aoData )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
109 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
110 _fnCallbackFire( oSettings, 'aoServerParams', 'serverParams', [aoData] );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
111 }
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 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
115 * Data the data from the server (nuking the old) and redraw the table
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
116 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
117 * @param {object} json json data return from the server.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
118 * @param {string} json.sEcho Tracking flag for DataTables to match requests
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
119 * @param {int} json.iTotalRecords Number of records in the data set, not accounting for filtering
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
120 * @param {int} json.iTotalDisplayRecords Number of records in the data set, accounting for filtering
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
121 * @param {array} json.aaData The data to display on this page
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
122 * @param {string} [json.sColumns] Column ordering (sName, comma separated)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
123 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
124 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
125 function _fnAjaxUpdateDraw ( oSettings, json )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
126 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
127 if ( json.sEcho !== undefined )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
128 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
129 /* Protect against old returns over-writing a new one. Possible when you get
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
130 * very fast interaction, and later queries are completed much faster
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
131 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
132 if ( json.sEcho*1 < oSettings.iDraw )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
133 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
134 return;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
135 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
136 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
137 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
138 oSettings.iDraw = json.sEcho * 1;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
139 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
140 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
141
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
142 if ( !oSettings.oScroll.bInfinite ||
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
143 (oSettings.oScroll.bInfinite && (oSettings.bSorted || oSettings.bFiltered)) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
144 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
145 _fnClearTable( oSettings );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
146 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
147 oSettings._iRecordsTotal = parseInt(json.iTotalRecords, 10);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
148 oSettings._iRecordsDisplay = parseInt(json.iTotalDisplayRecords, 10);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
149
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
150 /* Determine if reordering is required */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
151 var sOrdering = _fnColumnOrdering(oSettings);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
152 var bReOrder = (json.sColumns !== undefined && sOrdering !== "" && json.sColumns != sOrdering );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
153 var aiIndex;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
154 if ( bReOrder )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
155 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
156 aiIndex = _fnReOrderIndex( oSettings, json.sColumns );
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 var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
160 for ( var i=0, iLen=aData.length ; i<iLen ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
161 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
162 if ( bReOrder )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
163 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
164 /* If we need to re-order, then create a new array with the correct order and add it */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
165 var aDataSorted = [];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
166 for ( var j=0, jLen=oSettings.aoColumns.length ; j<jLen ; j++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
167 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
168 aDataSorted.push( aData[i][ aiIndex[j] ] );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
169 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
170 _fnAddData( oSettings, aDataSorted );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
171 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
172 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
173 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
174 /* No re-order required, sever got it "right" - just straight add */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
175 _fnAddData( oSettings, aData[i] );
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 oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
179
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
180 oSettings.bAjaxDataGet = false;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
181 _fnDraw( oSettings );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
182 oSettings.bAjaxDataGet = true;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
183 _fnProcessingDisplay( oSettings, false );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
184 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
185