如何使用 jQuery DataTables 发布整个表的数据 [英] How to post data for the whole table using jQuery DataTables

查看:19
本文介绍了如何使用 jQuery DataTables 发布整个表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 jQuery DataTables 和分页的表格

I have a table working with jQuery DataTables and pagination

我有超过 300 个注册表,其中每页最多 50 个.每次我从一个页面切换到另一个页面时,其他 250 个注册表都会从 DOM 中删除,所以当我提交并执行 POST 到我的控制器时,我只会得到一个包含 50 个模型的列表,这些模型是我当前页面上的模型.

I have over 300 registrys which 50 are the max on each page. Every time I change from page to page the other 250 registrys are removed from DOM so when I submit and do the POST to my controller I only get a list of 50 models which are the ones on my current page.

我如何访问其他 250 个,或告诉表将它们全部发送?我已经搜索过并且读到这是分页的正常行为.但是你们是如何解决这个问题的?

How do I access the other 250, or tell the table to send them all? I have searched and I have read that this is the normal behavior of pagination. But how do you guys work around it?

推荐答案

CAUSE

当使用带有分页的 DataTables 插件时,为了性能和兼容性,DOM 中仅存在当前页面行.因此,当您提交表单时,只会提交当前页面表单控件值.

CAUSE

When using DataTables plug-in with pagination only current page rows exist in the DOM for performance and compatibility. Therefore when you submit the form, only current page form controls values are being submitted.

诀窍是在提交表单之前将当前页面以外的表单元素转换为 .

The trick is to turn form elements from pages other than current into <input type="hidden"> before submitting the form.

var table = $('#example').DataTable();

// Handle form submission event
$('#frm-example').on('submit', function(e){
   var form = this;

   // Encode a set of form elements from all pages as an array of names and values
   var params = table.$('input,select,textarea').serializeArray();

   // Iterate over all form elements
   $.each(params, function(){
      // If element doesn't exist in DOM
      if(!$.contains(document, form[this.name])){
         // Create a hidden element
         $(form).append(
            $('<input>')
               .attr('type', 'hidden')
               .attr('name', this.name)
               .val(this.value)
         );
      }
   });
});

参见 jQuery DataTables:如何提交所有页面表单数据 - 直接提交以获取更多详细信息和示例.

See jQuery DataTables: How to submit all pages form data - Submit directly for more details and examples.

另一种解决方案是通过 Ajax 提交表单.

Another solution is to submit the form via Ajax.

var table = $('#example').DataTable();

// Handle form submission event
$('#frm-example').on('submit', function(e){
   // Prevent actual form submission
   e.preventDefault();

   // Serialize form data
   var data = table.$('input,select,textarea').serialize();

   // Submit form data via Ajax
   $.ajax({
      url: '/echo/jsonp/',
      data: data,
      success: function(data){
         console.log('Server response', data);
      }
   });
});

参见 jQuery DataTables:如何提交所有页面表单数据 - 通过 Ajax 提交 了解更多详细信息和示例.

See jQuery DataTables: How to submit all pages form data - Submit via Ajax for more details and examples.

请注意,这两种解决方案都只能在客户端处理模式下工作.

Please note that both solutions will work in client-side processing mode only.

参见 jQuery 数据表:如何提交所有页面表单数据了解更多详情.

这篇关于如何使用 jQuery DataTables 发布整个表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆