Using selector for targets

Using selector for targets

LapointeLapointe Posts: 430Questions: 81Answers: 4

Hi
We can use

columns: [':gt(0)']

to select columns... Why not usable for targets ?

targets:['gt(0)']

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    edited November 2020

    I guess you are asking about the columnDefs.targets option. The docs state it supports the following:

    0 or a positive integer - column index counting from the left
    A negative integer - column index counting from the right
    A string - class name will be matched on the TH for the column (without a leading .)
    The string "_all" - all columns (i.e. assign a default)

    Using :gt(0) is a jQuery selector which is not an option that columnDefs.targets supports.

    Kevin

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    Hi Kevin

    I agree and already had a look to targets selector possibility.
    In fact it should be interresting to allow this kind of selector, because not need to count the column to disable all else the 2 first for example...

    So question is in feature request...

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    Answer ✓

    You could use a jQuery statement to add a classname to all but the first 2 columns then use that classname as the columnDefs.targets. For example:
    http://live.datatables.net/yumifozu/1/edit

    So question is in feature request...

    Sorry, didn't notice that :-)

    Kevin

  • allanallan Posts: 63,678Questions: 1Answers: 10,497 Site admin
    Answer ✓

    The issue with using a DOM selector for columns.targets is that the columns having been processed by DataTables at that point (i.e. they might not even exist if you are using columns.title to create the column). They also having had any classes assigned by columns.className.

    I took the decision of going with the lowest common denominator. Instead of allowing the targets to use DOM queries, which would only work in some cases, I thought it might save confusion. I might have been wrong to take that approach :).

    An option that would work just now, if you have DOM defined columns, would be to use jQuery to select those columns and then use a map combined with index() to get an array of the column indexes, which can be assigned to columns.targets.

    Allan

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    Hi Kevin, @allan
    No matter... It was just a question.
    Datatables is an amazing dev, and I'm just (each day) learning JS ans JQ to be a better software coder...
    In fact I'm a little (little ?) bit lazy and have to count from 1 to x many time in dev mode...
    Thanks again all
    <3

This discussion has been closed.