简体   繁体   English

数据表和jQuery $ .post

[英]Datatables and jQuery $.post

I have a datatables table in my webpage with more than one page of paginated data in it. 我的网页中有一个datatables表,其中有一页以上的分页数据。 Per the example given by datatables , I use the following javascript/jQuery to submit the form: 根据datatables给出的示例 ,我使用以下javascript / jQuery提交表单:

<script>
var oTable;
$(document).ready(function() {
    $('#form').submit( function() {
        var sData = oTable.$('input').serialize();
        $.post('', sData); // WORKS
        // $.post('', sData, $('body').html(data)); // DOES NOT WORK
        return false;
    } );

    oTable = $('#meters').dataTable();
} );
</script>

When I post via $.post('', sData); 当我通过$.post('', sData); , all of my table rows are posted. ,我的所有表行都已发布。 When I post with $.post('', sData, $('body').html(data)); 当我用$.post('', sData, $('body').html(data)); , only the rows of the visible page of the table are posted. ,仅发布表的可见页面的行。 Why? 为什么?

I'm fairly new to jQuery -- perhaps I am missing something... 我对jQuery很陌生-也许我缺少一些东西...

Here is the code I use to initialize the table: 这是我用来初始化表的代码:

<script>    

/* Create an array with the values of all the input boxes in a column */
$.fn.dataTableExt.afnSortData['dom-text'] = function  ( oSettings, iColumn )
{
    var aData = [];
    $( 'td:eq('+iColumn+') input:last', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
        aData.push( this.value );
    } );
    return aData;
}

/* Create an array with the values of all the select options in a column */
$.fn.dataTableExt.afnSortData['dom-select'] = function  ( oSettings, iColumn )
{
    var aData = [];
    $( 'td:eq('+iColumn+') select:last', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
        aData.push( $(this).val() );
    } );
    return aData;
}

/* Create an array with the values of all the checkboxes in a column */
$.fn.dataTableExt.afnSortData['dom-checkbox'] = function  ( oSettings, iColumn )
{
    var aData = [];
    $( 'td:eq('+iColumn+') input:last', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
        aData.push( this.checked==true ? "1" : "0" );
    } );
    return aData;
}

    $(document).ready(function() {
        $('#meters').dataTable(
            {
                "aoColumns": [
                    { "sSortDataType": "dom-text" },
                    { "sSortDataType": "dom-select" },
                    { "sSortDataType": "dom-checkbox" },
                ],
                "aoColumnDefs": [
                    { 
                        "aTargets":     ["vbNoSearchSort"],
                        "bSearchable":  false,
                        "bSortable":    false
                    }
                ],
                "bProcessing"   :true,
                "bStateSave"    :true,
                "sPaginationType": "full_numbers"
            }
        );
    } );
</script>

because it needs to be a closure or a reference to a function. 因为它需要是闭包或对函数的引用。

$.post('', sData, function(data){$('body').html(data); }); 

or 要么

function callback(data) {
   $('body').html(data);
}
$.post('', sData, callback); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM