Bootstrap_datatable Ajax Request Two-time problem resolution

Source: Internet
Author: User

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
  1. function search (data) {
  2. var $searchResult = $ ("#search-result. Result-panel");
  3. if (resultdatatable) {
  4. Resultdatatable.fncleartable ();
  5. $searchResult. DataTable (). Fndestroy ();
  6. $ ("#resultList"). empty ();
  7. } Else {
  8. $searchResult. Show ();
  9. }
  10. resultdatatable = $searchResult. DataTable ({
  11. "Bpaginate": true,
  12. "Bautowidth": false,
  13. "bprocessing": true,
  14. "Bfilter": false,
  15. "Bjqueryui": true,
  16. " spaginationtype": "Full_numbers",
  17. "Olanguage": { //Chinese
  18. " slengthmenu": "show _menu_ Records per page",
  19. "Szerorecords": "no data Retrieved",
  20. " sinfo": "The current data is from _start_ to _end_ data; there are _total_ records in total",
  21. " sinfoemtpy": "no data",
  22. "sprocessing": "Loading Data ...",
  23. "Opaginate": {
  24. " Sfirst": "Home",
  25. " sprevious": "front page",
  26. " snext": "Back Page",
  27. "Slast": "last"
  28. }
  29. },
  30. "Bserverside": true,
  31. " Sservermethod": "POST",
  32. " Sajaxsource": "${baseurl}/zpzxresumesearch.do?&method=<bean:message key=" ZjzxResume.button.search '/> ',
  33. //"Fnserverdata": ExecuteQuery,
  34. "Fnserverparams": function (aodata) {
  35. Aodata.push ({"name": "Conds", "value": Data});
  36. },
  37. "Aocolumns": [
  38. { "mdata": null},
  39. { "mdata": "Name"},
  40. { "mdata": "Sex"},
  41. { "mdata": "Birthday"},
  42. { "mdata": "Mobilephone"},
  43. { "mdata": "Diploma"},
  44. { "mdata": "Workyears"},
  45. { "mdata": "Currentaddress"},
  46. { "mdata": "Hukouaddress"},
  47. { "mdata": "UpdateTime"},
  48. { "mdata": null}
  49. ],
  50. "Aocolumndefs": [
  51. {
  52. "Atargets": [1],
  53. "Mrender": function (data, type, full) {
  54. return "<a target= ' _blank ' onmouseover= ' showlabels (this, event); ' Onmouseout= ' hiddenlabels (this, event); ' Labels= ' "+buildresumelabels (full) +" ' href= ' ${baseurl}/zpzxresumelookresult.do?resumeid= "
  55. +full.resumeid+"&zpzxtalentid="
  56. +full.zpzxtalentid+"&method=<bean:message key= ' zjzxResume.button.showDetailInfo '/> ' >"
  57. + Data + "</a>";
  58. }
  59. },
  60. {
  61. "atargets": [0],
  62. "Mrender": function (data, type, full) {
  63. return "<input type= ' checkbox ' value= '" +full.resumeid+"' class= ' chkexportresume '/> ';
  64. }
  65. },
  66. {
  67. "Atargets": [Ten],
  68. "Mrender": function (data, type, full) {
  69. return Buildlink (data, type, full);
  70. }
  71. }
  72. ]
  73. });

After tracing the DataTable, it was found that in the execution

[JavaScript]View Plaincopy
    1. 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
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.