2

Following .js files are used.

  • jquery.dataTables.min.js (DataTables 1.10.16).
  • dataTables.bootstrap.min.js (DataTables Bootstrap 3 integration).
  • dataTables.responsive.min.js (Responsive 2.2.1).
  • dataTables.scroller.min.js (Scroller 1.4.4).

Following .css files are used.

  • jquery.dataTables.min.css
  • dataTables.bootstrap.min.css
  • responsive.dataTables.min.css
  • scroller.dataTables.min.css

Here is the table layout:

<table class="table table-responsive table-bordered table-striped table-hover" id="tbl-cat-lvl-two" style="width:100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Code</th>
            <th>Icon</th>
            <th>Category Label</th>
            <th>Precedence</th>
            <th>Visibility</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

Here is the table script:

catLvl2table = $('#tbl-cat-lvl-two').DataTable( {
    "processing"    : true,
    "serverSide"    : true,
    "order"         : [[ 4, "asc" ]],
    "responsive"    : true,
    "scrollY"       : "236px",
    "scrollCollapse": true,
    "ajax"          : {
        "url"       : baseUrl+getCatLvl2DataUrl+lvl1CatId,
        "type"      : "POST"
    },
    "columnDefs"    : [
        { "visible"     : false, "targets": [0] },
        { "orderable"   : false, "targets": [0, 2, 5, 6] },
        { 
            className: "align-center check-align-center",
            "targets": [6],
            "render"    : function (data, type, full, meta){
                let id = full.id;
                return `<button href="" class="btn btn-sm btn-info dt-btn-view" data-id="${id}"><i class="fa fa-eye"></i></button><button href="" class="btn btn-sm btn-primary dt-btn-edit" data-id="${id}"><i class="glyphicon glyphicon-pencil"></i></button><button href="" class="btn btn-sm btn-danger dt-btn-del" data-id="${id}"><i class="glyphicon glyphicon-trash"></i></button>`;
            } 
        }
    ],
    "columns": [
        { "data": "id" },
        { "data": "code" },
        { "data": "icon" },
        { "data": "category" },
        { "data": "precedence" },
        { "data": "visibility" },
    ],
});

Here is the output:enter image description here

2
  • Please, someone help me in this issue.. Commented May 5, 2018 at 21:51
  • No anyone?? :/ :((( Commented May 9, 2018 at 17:59

1 Answer 1

2
<table class="table table-responsive table-bordered table-striped table-hover" id="tbl-cat-lvl-two" style="width:100%">
    <thead>
        <tr>
            <!-- <th>ID</th> --> //Remove completely this column
            <th>Code</th>
            <th>Icon</th>
            <th>Category Label</th>
            <th>Precedence</th>
            <th>Visibility</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

Just remove following code from "columnDefs" array.

{ 
    className: "align-center check-align-center",
    "targets": [6],
    "render"    : function (data, type, full, meta){
        let id = full.id;
        return `<button href="" class="btn btn-sm btn-info dt-btn-view" data-id="${id}"><i class="fa fa-eye"></i></button><button href="" class="btn btn-sm btn-primary dt-btn-edit" data-id="${id}"><i class="glyphicon glyphicon-pencil"></i></button><button href="" class="btn btn-sm btn-danger dt-btn-del" data-id="${id}"><i class="glyphicon glyphicon-trash"></i></button>`;
    } 
}

And add this code:

"columns": [
        { "data": "code" },
        { "data": "icon" },
        { "data": "category" },
        { "data": "precedence" },
        { "data": "visibility" },
        { "data" : null,
            className : "align-center" // You should style for this class
        }
],
"rowCallback": function(row, data, index) {
    $('td:eq(5)', row).html(
        '<button href="" class="btn btn-sm btn-info dt-btn-view" data-id="'+data.id+'"><i class="fa fa-eye"></i></button>'+
        '<button href="" class="btn btn-sm btn-primary dt-btn-edit" data-id="'+data.id+'"><i class="glyphicon glyphicon-pencil"></i></button>'+
        '<button href="" class="btn btn-sm btn-danger dt-btn-del" data-id="'+data.id+'"><i class="glyphicon glyphicon-trash"></i></button>'
    );
},

In external css class

.align-center {
  text-align: center; // whatever you want
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks.. I will try that way.
Hey, I tried as you suggested. Width mismatch can be solved by that, but now, passing [object Object] instead of buttons in td-6:actions. How to solve that? Please, let me know.
This [object object] may be passed give to number of column issue.
Remove completely hidden 'ID' column(0). You can handle button actions by 'data.id'(objectname.id). And just add 'td:eq(5)'.
I removed column[0]: Id and I am not getting [object Object] error. But, data-id="${id}" is not passing now. :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.