3

我正在使用 jQuery DataTables 在我的表单上显示数据。我有一个要求,我需要发送多个参数来过滤数据。

目前我正在使用:

$('.datatable').dataTable({
        "sDom": "<'row'<'span3'l><'span6'f>r>t<'row'<'span5'i><'span4'p>>",
        "bServerSide": true,                                      
        "sAjaxSource": //myurl,                                             
        "bProcessing": true,                                      
        "sPaginationType": true,                              
        "bSort": true,                                  
        "iDisplayLength": 20,
        "fnServerParams": function (aoData) {
            if (filtersAdded === "true") {
                aoData.push({ "name": "chk1", "value": true});
            }
        },                                           
        "fnCreatedRow": function (nRow, aData, iDataIndex) { 
                // Do stuff with row data                
        },
        "bFilter": false,
        "bDestroy": true

    });

其中 chk1, chk2,.. 是复选框,为了简单起见,假设用户将其选中为 true。现在在我的代码中,我可以轻松检索 chk1 值,但我的要求是如果用户选择 3 或 4 个复选框(我的页面上将有 7-8 个复选框)怎么办?我想将数组中的所有这些 3-4 个选中的复选框值传递给代码(MVC)。

我现在正在做的是,将数组中所有选择的复选框数据获取为:

var filterarray = [];
filterarray .push({ "name": "chk1", "value": true});
filterarray .push({ "name": "chk2", "value": true});

现在我如何将 filterarray 作为fnServerParams值传递?任何帮助将不胜感激。

4

1 回答 1

1

I don't know if I've understood you correctly. Do you wanna send the information once or do you want to refresh the information as the user set/unset the checkboxes?

If you need to POST data for your ajax request you can use the code below:

 var oTable = $('#selector').dataTable( {
            "sAjaxSource": "your-url",
            "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( {"name": "param1", "value": 1} );
                    aoData.push( {"name": "param2", "value": 2} );
                    $.ajax( {
                            "dataType": 'json',
                            "type": "POST",
                            "url": sSource,
                            "data": aoData,
                            "success": function(result){
                                if (result.iTotalRecords == 0) {
                                        $("#datatable_unit_users").hide();
                                } else {
                                        $("#datatable_unit_users").show();
                                        fnCallback(result)
                                }
                            }

                    } );
            },
            ...
    )};

If you need to refresh your information you might use the API function fnReloadAjax (http://datatables.net/plug-ins/api).

Hope it helps

于 2013-07-12T21:59:02.423 回答