columnDefs how to target _all except one target without naming every index

columnDefs how to target _all except one target without naming every index

RafVeccRafVecc Posts: 1Questions: 1Answers: 0

I have my columnDefs with some targets like that

columnDefs: [
{
targets: [-1],
className: 'dt-center',
orderable: false
},
{
className: 'dt-head-center dt-body-left',
targets: ['_all']
}

    ],

but i want to all target to have the className: 'dt-head-center dt-body-left' and the last column to not have this class and have only the dt-center.
Its there a way to do that without naming every index like [0,1,2,3,4,5,6]?

Answers

  • RichardD2RichardD2 Posts: 21Questions: 2Answers: 1

    Assuming you know the number of columns, and the index of the column you want to exclude, perhaps something like this might work?

    targets: Array
        .from({ length: numberOfColumns }, (x, i) => i)
        .filter(i => i !== columnIndexToExclude)
    

    The Array.from call creates an array of every number from 0 to numberOfColumns - 1.

    The filter call then excludes the specific column from the array.

    Array.from() - JavaScript | MDN
    Array.prototype.filter() - JavaScript | MDN

  • kthorngrenkthorngren Posts: 22,070Questions: 26Answers: 5,086
    edited June 26

    The columnDefs.targets docs provide all the options. The only option I see is to assign a classname to the th you want to target. Although this seems to negate the idea of using columns.className.

    You can use initComplete to remove the undesired classnames from the last column.

    Kevin

Sign In or Register to comment.