在Select(DropDown)更改事件上重新初始化Jquery DataTable

时间:2013-09-07 06:13:18

标签: jquery datatable

我正在使用Jquery UI DataTable,它填充在select(DropDown) change事件中。 在PageLoad可以。当我执行dropdown change事件时,DataTable使用ReinitializedfnDestroy(),但DataTable的格式会发生变化。以下是我的代码..

  campusChangeEvent = function () {
        $('#cmbClassCP').on('change', function () {
        $('#ClassRegistredDataTable').dataTable().fnDestroy();
            GetAllClassbyCampus($('#cmbClassCP :selected').val());
        });
    }, 

 GetAllClassbyCampus = function (id) {
        var oTable = $('#ClassRegistredDataTable').dataTable({
            "bJQueryUI": true,
            "sPaginationType": "full_numbers",
            "bServerSide": true,
            "bRetrieve": true,
            "bDestroy": true,
            "sAjaxSource": "/forms/Setup/Setup.aspx/GetAllClassBycampus?CampusId=" + id,
            "fnServerData": function (sSource, aoData, fnCallback) {
                $.ajax({
                    "type": "GET",
                    "dataType": 'json',
                    "contentType": "application/json; charset=utf-8",
                    "url": sSource,
                    "data": aoData,
                    "success": function (data) {
                        fnCallback(data.d);
                    }
                });
            },
            "aoColumns": [
                         {
                             "mDataProp": "RowNo",
                             "bSearchable": false,
                             "bSortable": false,
                             "sWidth": "20"
                         },
                        {
                            "mDataProp": "CampusName",
                            "bSearchable": false,
                            "bSortable": false,

                        },
                        {
                            "mDataProp": "ClassName",
                            "bSearchable": true,
                            "bSortable": false

                        },
                        {
                            "mDataProp": "Section",
                            "bSearchable": false,
                            "bSortable": false
                        },
                        {
                            "mDataProp": "Description",
                            "bSearchable": false,
                            "bSortable": false
                        },
                        {
                            "mData": null,
                            "bSearchable": false,
                            "bSortable": false,
                            "fnRender": function (oObj) {
                                return '<a class="edit" href="">Edit</a>';

                            }
                        }
            ]
        });

我的表单在Page Load上看起来像..

enter image description here

DropDown更改事件后,如下所示..

enter image description here

任何帮助......

3 个答案:

答案 0 :(得分:10)

我用这种方法做到了..

 $('#ClassRegistredDataTable').dataTable().fnDestroy();

这将覆盖 jquery.dataTables.css

dataTable css

默认情况下,它看起来像

table.dataTable {
    margin: 0 auto;
    clear: both;
    width: 100%;
}

将其更改为..

table.dataTable {
    margin: 0 auto;
    clear: both;
    width: 100% !important;
}

它对我有用..

答案 1 :(得分:2)

尝试:

$('#ClassRegistredDataTable').dataTable().fnDraw();

或:

//if you don't want the table reorder/resorted
$('#ClassRegistredDataTable').dataTable().fnDraw(false);

答案 2 :(得分:1)

即使您需要清理表格,例如:

$('#ClassRegistredDataTable tbody').html('');
$('#ClassRegistredDataTable').dataTable().fnDestroy();