Shahid Iqbal
Shahid Iqbal

Reputation: 2135

Reinitialize Jquery DataTable on Select(DropDown) change event

i am using Jquery UI DataTable, which is filled on select(DropDown) change event. On PageLoad its ok. When i perform dropdown change event, DataTable is Reinitialized by using fnDestroy(), but the format of DataTable changes. Below is my code..

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

                            }
                        }
            ]
        });

My form looks on Page Load like..

enter image description here

After DropDown change event, looks like below..

enter image description here

Any Help....

Upvotes: 9

Views: 18286

Answers (3)

user3073265
user3073265

Reputation: 11

Even you require to clear your table, like this:

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

Upvotes: 1

Shahid Iqbal
Shahid Iqbal

Reputation: 2135

i have done it by this method..

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

This will override css of dataTable in jquery.dataTables.css

By Default it looks like

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

change it to..

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

It worked for me..

Upvotes: 10

Eric Uldall
Eric Uldall

Reputation: 2391

try:

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

or:

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

Upvotes: 2

Related Questions