Target key instead of index in columnDefs

Target key instead of index in columnDefs

Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0
edited April 2022 in Free community support

Link to test case: http://themeadow.se/ntjperrors/
Debugger code (debug.datatables.net):
Error messages shown: No error message shown.
Description of problem:
All my indexes are changed everytime I add or remove a data point. It's very tidious to correct this. Is it possible to somehow use:

"columnDefs": [
    {
        "targets": ["ServiceProducerDescription"],
        "visible": false,
    }
]

...instead of this:

"columnDefs": [
    {
        "targets": [ 8 ],
        "visible": false,
    }
]

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    edited April 2022

    I know. The examples in the docs are full of those hard coded column numbers. I cleaned up my code some time ago and got rid of every single hard coded number.

    columnDefs: [
        // targets may be classes
        {   targets: "hiddenCols", visible: false},
        //assigns the className to to all td fields in the 
        //respective column(s) with the class noSelectCols
        {   targets: "noSelectCols", className: "noSelectFields"},
        {   targets: "cnt", className: "text-center"},
        {   targets: "maxExpDate", className: "mED" },
        {   targets: "econEndDate", className: "eeD" }
    ],
    

    As you can see you can target a column by its class and you can also assign classes to the td elements targeting the column (th). That is also quite useful.

    https://datatables.net/reference/option/columnDefs.targets

  • Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0

    I'm not sure I understand. Do you mean that { targets: "hiddenCols", visible: false} actually works for you? If so, what do I have to change in order for it for me (because it does not)?

  • kthorngrenkthorngren Posts: 21,348Questions: 26Answers: 4,955

    See the columnDefs.targets docs for the options:

    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)

    The string is a class not a columns.data definition, if thats what you are asking. If you are using columns and you are targeting only one column with columnDefs then you can move your config options to the columns definitions. columnDefs is designed for applying the same config options to multiple columns.

    Kevin

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    edited April 2022 Answer ✓

    I assign classes in my HTML table definitions. All I need to make sure is to assign the right class to the respective column. Then I don't have to worry about the position of the column in the table.

    <table id="tblLim" class="table table-striped table-bordered"
           cellspacing="0" width="100%">
        <thead>
            <tr>            
                <th>Bla Bla Bla</th>
                <th class="hiddenCols">Bla Bla Bla Bla Bla</th>
                <th class="cnt">Bla Bla Bla Bla Bla</th>
            </tr>
        </thead>
    </table>    
    

    With my code above the 2nd column isn't visible and the tds of the third column get the class "text-center".

  • Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0

    Thanks alot!

Sign In or Register to comment.