problem with $.fn.dataTable.ext.search.push and sort

problem with $.fn.dataTable.ext.search.push and sort

RaphuRaphu Posts: 9Questions: 3Answers: 0

Link to test case: http://testdtraphu.ddns.net:10049/portables.php

Description of problem:

My problem is strange:
I am using $.fn.dataTable.ext.search.push to set up filters on articles. it works wonderfully ... but when I apply one of my filters the good articles appear then when I sort them by price or by description it is not the same articles which appear anymore ...
Even stranger, when I sort before applying my filters and then apply one of my filters, the articles displayed are directly bad ?? I do not understand...

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    Answer ✓

    This is your plugin code:

    $.fn.dataTable.ext.search.push(
        function(settings,data,dataIndex,row,loop){
            let stockArt=parseInt(data[3].replace(/(<([^>]+)>)/gi, ""),10); //enlève les tags html du stock
            return displayArtRow(settings.aoData[loop].nTr.attributes[1].value, stockArt, priceArtToFloat(data[4]), oGroupSortCheckedDom);
        }
    );
    

    You are using the loop parameter to directly access the Datatables data cache. The loop parameter is the wrong parameter to use as its the "This is the loop index that DataTables uses internally to loop over the rows in the table.". This is form the Search Plugin docs. This is causing the randomness you are seeing. Use the index parameter instead which is the row index for the data.

    Its not recommended to access the data cache directly, ie, settings.aoData[loop].nTr.attributes[1].value. You should use the Datatables APIs instead. See this example:
    http://live.datatables.net/cerudesi/1/edit

    It access the API and uses cell().node() to get the cell's HTML then checks to see if it has the class director.

    Change api.cell(index, 1).node() to api.cell(loop, 1).node() and you will see the same randomness. Also the console output will show you the difference between the loop and index values.

    Kevin

  • RaphuRaphu Posts: 9Questions: 3Answers: 0

    dear Kevin,
    it works perfectly ... 1000 thanks

    o:)

This discussion has been closed.