Add data-search to td on ajax call

Add data-search to td on ajax call

tdevine33tdevine33 Posts: 4Questions: 3Answers: 0

I have a dataTable being populated using an ajax call to return WordPress data.

    var $grantsTable = $('#grantsList').DataTable({
                processing: true,
                ajax: {
                    url: ajax_object.ajax_url + '?action=dt_archive_grants',
                    dataSrc: 'data'
                }...

I need to have the table searchable by a data-search element on one of the td elements. I tried adding it through the columnDefs.createdCell but found through another thread that data-search must be added before initialization?

                    createdCell: function(td, celldata, rowData, row, col) {
                        $(td).attr('data-search',  rowData.search);
                    }

Is there any way to dynamically add a data-search attribute to a td after an ajax call? I tried using columns.render but it seems like you don't have access to the td in that method.

Any help is greatly appreciated, thanks!

Answers

  • colincolin Posts: 14,618Questions: 1Answers: 2,497

    This thread should help - it's also asking about HTML5 attributes with Ajax data,

    Colin

  • tdevine33tdevine33 Posts: 4Questions: 3Answers: 0

    Thanks for the response.

    That helps with adding data-attributes with Ajax data, but it still doesn't seem to use the data-search attribute that's added to the <td> element when searching the table.

  • allanallan Posts: 57,737Questions: 1Answers: 9,204 Site admin

    If you are using Ajax loaded data, don't bother adding it to the attributes - that would just slow things down due to an extra DOM interaction. Instead use orthogonal data via the columns.render option.

    Allan

Sign In or Register to comment.