38

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.

4 Answers 4

88

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",
   }
 ]
Sign up to request clarification or add additional context in comments.

1 Comment

Alternative of @aabiro 's answer: "targets": [0,1,2] You can add multiple columns value here as well
23

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] },
  ], ...

Comments

4

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"
}

Comments

3

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>';
            }
        },
    ],
    ....
});

1 Comment

Thanks! This is the best answer. The problem with the columnDefs and className approach shown in other answers is that the style is applied on the entire column including header and body cells. It doesn't work if you want to apply the cell styles on the header or body cells separately. Moreover, the base styles of jQuery DataTable cells don't work with Bootstrap themes. Bootstrap seems to override them. In that scenario, this approach suits the best as you don't have to depend on DataTable styles and apply your own styles on the cell content independently.

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.