JQuery Datatables center align 1 column

Want to center align only in the first column titled "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 do I need to add the columns parameter for this to happen? I am trying to read all Datatables doc and google.

+19


source to share


4 answers


You can use your own theme classes (bootstrap) or your own in the columndef file. as

text-right, text-left, text-center



 'columnDefs': [
  {
      "targets": 0, // your case first column
      "className": "text-center",
      "width": "4%"
 },
 {
      "targets": 2,
      "className": "text-right",
 }],

      

+40


source


Alternatively, you can group columns to apply one style to many, like this:



  columnDefs: [
    { className: 'text-right', targets: [7, 10, 11, 14, 16] },
    { className: 'text-center', targets: [5] },
  ], ...

      

+3


source


You can style this 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>';
            }
        },
    ],
    ....
});

      

+2


source


You can also specify one 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"
}

      

+1


source







All Articles