How can I update data-filter attribute?

How can I update data-filter attribute?

DimetriusDimetrius Posts: 7Questions: 2Answers: 0
edited June 2016 in Free community support

How can I update data-filter attribute for specific cell?

My cell has id, I update data-filter like

document.getElementById('cell-56').setAttribute('data-filter','new value');

After setAttribute DataTable filter not use 'new value'.

How can I update data-filter attribute to take effect?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    You need to call row().invalidate() after you update the element to tell DataTables that it needs to reread from it.

    Allan

  • DimetriusDimetrius Posts: 7Questions: 2Answers: 0
    edited June 2016

    I try add data-filter attribute in createdRow

    "createdRow": function(row, data, dataIndex) {
                var $dc = $(row).find('td:eq(1)');
                $dc.attr('data-filter', 'new value');
    
                var t = this.DataTable();
                t.row(row).invalidate(); // not take effect
                t.rows().invalidate(); // not take effect
    }
    
    

    This add new attribute, but filter still can't find "new value"

  • DimetriusDimetrius Posts: 7Questions: 2Answers: 0
    edited June 2016

    My html table cell looks like

    <td data-filter="one value">two value</td>
    
    

    After I do

    table.rows().invalidate();
    
    

    Filter can find row by "two value", but "one value" ignored.

    Maybe it is bug?

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    If you link to a page showing the issue I would be happy to take a look and try to debug it.

    Allan

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    Hmmm actually:

    This add new attribute,

    So it didn't have that attribute before? If so, DataTables won't automatically detect it having been added.

    Perhaps you could explain a bit about what it is you are looking to do overall.

    Allan

  • DimetriusDimetrius Posts: 7Questions: 2Answers: 0

    I made fiddle with simple table and three rows

    JSFiddle

    I want add a fourth row.
    New fourth row must be sortable and filter like first three.

    How to add new fourth row in my case?

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin
    Answer ✓

    To add a new row, rather than updating an existing one as we discussed above, you can create the tr element yourself and pass it in: https://jsfiddle.net/804dg124/1/ .

    The alternative is that you pass in an object that contains the same data structure that DataTables creates from the row and attributes:

    {
        "0": "1",
        "1": {
            "display": "o-n-e",
            "@data-order": "1",
            "@data-filter": "one o-n-e"
        }
    }
    

    Allan

  • DimetriusDimetrius Posts: 7Questions: 2Answers: 0

    This works, Allan, thank you very match! :) :) :)

This discussion has been closed.