Add a attribute/comment to a column

Add a attribute/comment to a column

Wooly65Wooly65 Posts: 85Questions: 25Answers: 1

Is there an easy way to add an extra attribute/comment to a column to be retrieved later. For each column we want to add metadata to be used later when processing the table. I currently define on dataTable using:

looping ({
        cols.push(
            {
                title : label,
                data  : field,
                comment  : 'something interesting'
            }
        );
});

jqDataTable.DataTable({
...,
columns : cols, 
deferRender : true
});

Then later we could perform:
$('#jqDataTable').dataTable().api().columns().comment()

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Wooly65 ,

    There's two things you could do - there's columns.name and columns.className , both of those would give you some form of metadata.

    Cheers,

    Colin

  • Wooly65Wooly65 Posts: 85Questions: 25Answers: 1

    Thanks for the response @colin . I had previously thought about both.

    columns.className - I didn't want to misuse this attribute and cause potential HTML issues.
    columns.name - I gave serious consideration to using this attribute since I don't need to reference a column by its name. But I can only use the attribute and not retrieve the attribute.
    table.column( 'location:name' ).data() vs table.columns().name()

  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Hi,

    Currently no - there isn't I'm afraid. It is something I've thought a lot about as it could be really useful for plug-ins and the like, but I've not settled on a way that I like yet.

    Until then, the workaround is to use column().header() and then use $().data() on the header cell. That way you can use jQuery to store the data just like you would any other column.

    Allan

  • Wooly65Wooly65 Posts: 85Questions: 25Answers: 1

    Thank you for the suggestion. I was able to get that to work. Looks like you already use data to store the columnIndex.

This discussion has been closed.