Using jquery Datatable in a recent project is easier to use, but during testing, it is found that when conditions change, sometimes the data in the query results is incorrect.
When using Firebug tracing, it was found that when the AJAX request was used, a search button was clicked, but two requests were sent, one for the original condition and one for the new request.
The following is the code for the query button. First to determine whether it is a DataTable, if so, first destroy off, and then re-created.
[JavaScript]View Plaincopy
- function search (data) {
- var $searchResult = $ ("#search-result. Result-panel");
- if (resultdatatable) {
- Resultdatatable.fncleartable ();
- $searchResult. DataTable (). Fndestroy ();
- $ ("#resultList"). empty ();
- } Else {
- $searchResult. Show ();
- }
- resultdatatable = $searchResult. DataTable ({
- "Bpaginate": true,
- "Bautowidth": false,
- "bprocessing": true,
- "Bfilter": false,
- "Bjqueryui": true,
- " spaginationtype": "Full_numbers",
- "Olanguage": { //Chinese
- " slengthmenu": "show _menu_ Records per page",
- "Szerorecords": "no data Retrieved",
- " sinfo": "The current data is from _start_ to _end_ data; there are _total_ records in total",
- " sinfoemtpy": "no data",
- "sprocessing": "Loading Data ...",
- "Opaginate": {
- " Sfirst": "Home",
- " sprevious": "front page",
- " snext": "Back Page",
- "Slast": "last"
- }
- },
- "Bserverside": true,
- " Sservermethod": "POST",
- " Sajaxsource": "${baseurl}/zpzxresumesearch.do?&method=<bean:message key=" ZjzxResume.button.search '/> ',
- //"Fnserverdata": ExecuteQuery,
- "Fnserverparams": function (aodata) {
- Aodata.push ({"name": "Conds", "value": Data});
- },
- "Aocolumns": [
- { "mdata": null},
- { "mdata": "Name"},
- { "mdata": "Sex"},
- { "mdata": "Birthday"},
- { "mdata": "Mobilephone"},
- { "mdata": "Diploma"},
- { "mdata": "Workyears"},
- { "mdata": "Currentaddress"},
- { "mdata": "Hukouaddress"},
- { "mdata": "UpdateTime"},
- { "mdata": null}
- ],
- "Aocolumndefs": [
- {
- "Atargets": [1],
- "Mrender": function (data, type, full) {
- return "<a target= ' _blank ' onmouseover= ' showlabels (this, event); ' Onmouseout= ' hiddenlabels (this, event); ' Labels= ' "+buildresumelabels (full) +" ' href= ' ${baseurl}/zpzxresumelookresult.do?resumeid= "
- +full.resumeid+"&zpzxtalentid="
- +full.zpzxtalentid+"&method=<bean:message key= ' zjzxResume.button.showDetailInfo '/> ' >"
- + Data + "</a>";
- }
- },
- {
- "atargets": [0],
- "Mrender": function (data, type, full) {
- return "<input type= ' checkbox ' value= '" +full.resumeid+"' class= ' chkexportresume '/> ';
- }
- },
- {
- "Atargets": [Ten],
- "Mrender": function (data, type, full) {
- return Buildlink (data, type, full);
- }
- }
- ]
- });
After tracing the DataTable, it was found that in the execution
[JavaScript]View Plaincopy
- Resultdatatable.fncleartable ();
method, the Fndraw method is actually called once, which invokes the AJAX request and then calls the AJAX request again in the DataTable's _fninitialise method.
View the DataTable Discovery Fncleartable method actually has a Boolean parameter to control whether the Fndraw method is executed, that is, just change the statement to
[JavaScript]View Plaincopy
- Resultdatatable.fncleartable (false);
Everything is OK.
Google for a long time to investigate the problem, and finally by looking at the source to solve. However, it is also seen that the DataTable author, when writing this plugin, uses a number of excellent programming methods, making the plugin easy to use and extend. Thanks for their contribution!
Bootstrap_datatable Ajax Request Two-time problem resolution