ozz
ozz

Reputation: 45

jQuery Datatables Plugin / Column name as a variable for a function on columnDefs

I'm trying to send column name/title as a variable for a function (which takes the column name and the data and appends them into a textbox) on clicking the links on the datatable. But I couldn't manage to get the column name.

Here's my code:

"columnDefs": [
                {
                    name: "Destination",
                    targets: 5,
                    render: function ( data, type, row, meta ) {
                        if(type === 'display'){
                            var columnName = ?
                            data = '<a href="javascript:myFunction(\''+ data + ' ' + columnName +'\')">' + data + '</a>';
                        }

                        return data;
                    }
                }
            ]

If someone helps me to access the column properties on columnDefs, or advice something else it would be very appreciated.

Thanks

Upvotes: 0

Views: 708

Answers (1)

bassxzero
bassxzero

Reputation: 5041

https://datatables.net/reference/api/column().header()

"columnDefs": [
                {
                    name: "Destination",
                    targets: 5,
                    render: function ( data, type, row, meta ) {
                        if(type === 'display'){
                            //  get header element based on column index
                            var title = table.column( meta.col ).header();                             
                            // create a jquery object of the header and get the innerHTML text
                            var columnName = $(title).html();
                            data = '<a href="javascript:myFunction(\''+ data + ' ' + columnName +'\')">' + data + '</a>';
                        }

                        return data;
                    }
                }
            ]

Upvotes: 1

Related Questions