Setting column names with HTML5 data-* attributes

Setting column names with HTML5 data-* attributes

rink_attendant_6rink_attendant_6 Posts: 16Questions: 4Answers: 1

Is there any way to use HTML5 data-* attributes to set the values of the object outlined in https://datatables.net/reference/option/columns.name?
If not, are there any plans to add this feature?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,908Questions: 1Answers: 10,148 Site admin
    Answer ✓

    Yes, you can set column options using the data-* attributes as described here in the manual.

    Allan

  • Jeff BucklesJeff Buckles Posts: 2Questions: 0Answers: 0
    edited May 2016

    I'm afraid it's still not clear to me how to use the data-* attributes to apply column names. For example, should it be sufficient to put a 'data-name' attribute on each <th>, or does it have to be on every <td>, or do I need to put a 'data-column-defs' attribute on the < table > with the entire array of names? I tried the first two unsuccessfully, though I would prefer to make it work with attributes on <th>.

    Thanks and Best Regards,

  • allanallan Posts: 61,908Questions: 1Answers: 10,148 Site admin

    should it be sufficient to put a 'data-name' attribute on each <th>

    Yes. The example I linked to shows how to use columns.className. The same applied to columns.name. Live example.

    Allan

  • Jeff BucklesJeff Buckles Posts: 2Questions: 0Answers: 0
    edited May 2016

    Allan, thank you for the quick reply and excellent example.
    Later I'll put up a live example of what I'm trying to do that seems to be not working.

    In brief, I'm trying to refer to the column name provided by the HTML5 tag while inside the 'columnDefs' block of the dataTables invocation.

    <th data-name="FName">....
    
    var inst_table = $('#inst_table').DataTables({....
        "columnDefs": [
            { "visible": false, "targets": "FName:name" },
            ....
        ]
    });
    

    My fallback is using the column index, but I can imagine that quickly becoming a maintenance nightmare.

    Thanks and Best Regards,

  • allanallan Posts: 61,908Questions: 1Answers: 10,148 Site admin

    Ah I see - the columns.targets option can't accept a name (since it is that option which can define a name - it couldn't find it based on that!).

    The columns.targets reference documentation shows the values that it can take.

    Allan

This discussion has been closed.