Datatables和jQuery $ .post

时间:2012-12-20 00:57:12

标签: javascript jquery datatables

我的网页中有一个datatables表,其中包含多页分页数据。根据{{​​3}},我使用以下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>

当我通过$.post('', sData);发帖时,我的所有表格行都会发布。当我使用$.post('', sData, $('body').html(data));发帖时,只会发布表格可见页面的行。为什么呢?

我对jQuery很新 - 也许我错过了一些东西......

以下是我用来初始化表的代码:

<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>

1 个答案:

答案 0 :(得分:1)

因为它需要是一个闭包或对函数的引用。

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

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