DataTable刪除行後的頁面更新利用Ajax解決

來源:互聯網
上載者:User

   使用Jquery的DataTable進行資料表處理非常方便,常遇到的一個問題就是刪除一行後頁面必須進行更新,需要注意的方法如下:前台頁面中初始化table時注意:

  代碼如下:

  var table = $('#sorting-advanced');

  table.dataTable({

  'bServerSide': true,

  'sAjaxSource': 'servlet/UserList',

  'bProcessing': true, 'bStateSave': true,

  'aoColumnDefs': [

  { 'bSortable': false, 'aTargets': [0,1,6]}

  ],

  'sPaginationType': 'full_numbers',

  'sDom': '<"dataTables_header"lfr>t<"dataTables_footer"ip>',

  'fnInitComplete': function( oSettings )

  {

  // Style length select

  table.closest('.dataTables_wrapper').find('.dataTables_length select').addClass('select blue-gradient glossy').styleSelect();

  tableStyled = true;

  }

  });

  'bStateSave': true, 這個必須設定,這樣就可以在刪除返回時,保留在同一頁上'bStateSave': true, 這個必須設定,這樣就可以在刪除返回時,保留在同一頁上

  刪除的代碼如下:

  代碼如下:

  function deleteConfirm(deleteID)

  {

  $.modal.confirm('確實要刪除此使用者嗎?', function()

  {

  $.ajax('servlet/DeleteUser', {

  dataType : 'json',

  data: {

  userID: deleteID

  },

  success: function(data)

  {

  if (data.success =='true')

  {

  $.modal.alert('刪除成功!');

  start = $("#sorting-advanced").dataTable().fnSettings()._iDisplayStart;

  total = $("#sorting-advanced").dataTable().fnSettings().fnRecordsDisplay();

  window.location.reload();

  if((total-start)==1){

  if (start > 0) {

  $("#sorting-advanced").dataTable().fnPageChange( 'previous', true );

  }

  }

  }

  else

  {

  $.modal.alert('刪除發生錯誤,請聯絡管理員!');

  }

  },

  error: function()

  {

  $.modal.alert('伺服器無響應,請聯絡管理員!');

  }

  });

  }, function()

  {

  //$.modal.alert('Meh.');

  });

  };

  其中只要是需要判斷一下當前頁中是否有資料,如果是最後一條的話,就在刪除後調用

  $("#sorting-advanced").dataTable().fnPageChange( 'previous', true );已回到上一頁中

  注意$("#sorting-advanced").dataTable().fnPageChange( 'previous'); 是不行的,必需進行重新整理,否則頁面中顯示的iDisplayStart會從cookie中取得,還是刪除前的iDisplayStart

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.