Luis Valencia
Luis Valencia

Reputation: 34038

datatables align to right

this seems an easy question, I have a code from a previous developer where all datatables is generated by code with no html other than just a div. So basically the plugin works fine and renders a few columns, but one of them is currency and I need to align it right with the least amount of effort, as this code is already in prod

$(tableSelector).DataTable({
                        data : [],
                        columns : vm.DataTableColumns,
                        order : containerConfig.SortOrder,
                        lengthMenu : [5, 10, 100],
                        deferRender : true,
                        footerCallback : function ( row, data, start, end, display ) {
                            var api = this.api(), data;

                            // Remove the formatting to get integer data for summation
                            var intVal = function ( i ) {
                                return typeof i === 'string' ?
                                    i.replace(/[\$,]/g, '')*1 :
                                    typeof i === 'number' ?
                                        i : 0;
                            };

                            // Total over all pages
                            total = api
                                .column( containerConfig.columnToTotal )
                                .data()
                                .reduce( function (a, b) {
                                    return intVal(a) + intVal(b);
                                }, 0 );

                            // Update footer
                            $( api.column( containerConfig.columnToTotal ).footer() ).html(
                                'Total: ' + total
                            );

                        }
                    }).on('draw.dt', function (e, settings, processing) {
                        // Make sure the UserPresence is added every time the DataTable's data changes (paging, sorting, search,..)
                        spService.UserPresenceComponent.AddPresence();
                    });
                    // Make component visible
                    $(componentSelector).css("visibility", "visible");

I think this is the only relevant code, but I can paste more if needed

Upvotes: 0

Views: 3194

Answers (1)

Remul
Remul

Reputation: 8252

You can change the column styling in the columns property.

Assuming the data provided from vm.DataTableColumns looks something like this:

columns: [
    { data: "id" },
    { data: "currency" },
]

You can add the className option.

columns: [
    { data: "id" },
    { data: "currency", className : "yourClass" },
]

Columns Data

Columns ClassName

Upvotes: 1

Related Questions