Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 483
Shahid Iqbal Avatar asked Sep 07 '13 06:09

Shahid Iqbal


3 Answers

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

like image 144
Shahid Iqbal Avatar answered Nov 04 '22 18:11

Shahid Iqbal


try:

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

or:

//if you don't want the table reorder/resorted
$('#ClassRegistredDataTable').dataTable().fnDraw(false);
like image 21
Eric Uldall Avatar answered Nov 04 '22 18:11

Eric Uldall


Even you require to clear your table, like this:

$('#ClassRegistredDataTable tbody').html('');
$('#ClassRegistredDataTable').dataTable().fnDestroy();
like image 1
user3073265 Avatar answered Nov 04 '22 18:11

user3073265