Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Datatables align center 1 column

Want to align center just the first column called "Status":

        $("#TransactionTable").DataTable({
            ajax: {
                url: '/Transaction/SearchMockup',
                type: 'POST',
                data: {
                    cardEndingWith: $("#ccn").val(),
                    status: $("#status").val(),
                    supplier: $("#supplier").val(),
                    fromDate: $("#fromDate").val(),
                    toDate: $("#toDate").val()
                }
            },
            columns: [
            {
                data: 'Status', render: function (data, type, row) {
                    switch (data) {
                        case 1:
                            return '<div id="circleRed"></div>'
                            break;
                        case 2:
                            return '<div id="circleGreen"></div>'
                            break;
                        case 3:
                            return '<div id="circleOrange"></div>'
                            break;
                    }
                }
            },
            { data: 'TransactionId' },
            { data: 'CreditCardNumber' },
            { data: 'Supplier' },
            { data: 'CreatedAt' },
            { data: 'Amount' }
            ]
        });

What i need to add the columns option to make it happen? I try to read all the Datatables doc and google.

like image 277
Ballon Ura Avatar asked Aug 01 '17 20:08

Ballon Ura


4 Answers

You can use your theme classes (bootstrap), or your own in columndef. like

text-right, text-left, text-center

 'columnDefs': [
    {
        "targets": 0, // your case first column
        "className": "text-center",
        "width": "4%"
   },
   {
        "targets": 2,
        "className": "text-right",
   }
 ]
like image 55
Ahmed Sunny Avatar answered Oct 22 '22 19:10

Ahmed Sunny


Also, you can group the columns to apply one style to many like so:

  columnDefs: [
    { className: 'text-right', targets: [7, 10, 11, 14, 16] },
    { className: 'text-center', targets: [5] },
  ], ...
like image 42
aabiro Avatar answered Oct 22 '22 18:10

aabiro


You can also specify a single css class name inside each column object.

In your case:

{
    data: 'Status', 
    render: function (data, type, row) {
        switch (data) {
            case 1:
                 return '<div id="circleRed"></div>';
            case 2:
                 return '<div id="circleGreen"></div>';
            case 3:
                 return '<div id="circleOrange"></div>';
        }
    },
    className: "text-center"
}
like image 3
Mario Tilli Avatar answered Oct 22 '22 18:10

Mario Tilli


You can style that manually

$("select_your_table").DataTable({
    ....
    columns: [
        {
            mData: "select_property_from_json",
            render: function (data) {
                return '<span style="display: flex; flex-flow: row nowrap; justify-content: center;">data</span>';
            }
        },
    ],
    ....
});
like image 1
Wahid Masud Avatar answered Oct 22 '22 18:10

Wahid Masud