Update Filter/Sort data when changing cell content in Ajax loaded table

Update Filter/Sort data when changing cell content in Ajax loaded table

darrenmdarrenm Posts: 29Questions: 11Answers: 0

I have a table which is built from Ajax data delivered from a file hosted on my server. The data is using the 'objects' method and I am using the columns() option to set the filter and sort values from object attributes as follows:

                data: {
                    _: "mydatavalue.display",
                    sort: "mydatavalue.sorton",
                    filter: "mydatavalue.filteron"
                },

This works fine on initial table load, but when I later need to update the value of a cell in javascript, I cannot work out how to set the values Datatables is storing for sorting ad filtering.

I'm updating the cell data like this:

catalogueindextable.cell("#mycellsid").data("My New Value").invalidate();

However, despite the cell content changing, the old data is used in filter and sort operations. I understand that this is because I haven't given Datatables new data to work with, but I cannot work out how to do that. I have tried explicitly setting the HTML data- attributes and then using .invalidate('dom') but that doesn't seem to work.

How can I target the search and sort data and update them for the selected cell?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @darrenm ,

    Yep, that should work - see here. Could you look at that, please, and see if it helps. If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.

    Cheers,

    Colin

  • darrenmdarrenm Posts: 29Questions: 11Answers: 0

    Hi Colin,

    Thanks for that. I was trying to work out how to do a demo using AJAX but I gave up. So your example does work and I have some other cases similar to that which are also working fine.

    In this particular cases I'm assigning different values to display and filter/sort at table load but I can't work out how to change sort/filter values when I change the cell content. DataTables obviously can't do that for me because it won't know what values to assign.

    This example shows how I'm loading data:

    http://live.datatables.net/doqugije/1/edit

    I've tried to approximate my situation by putting the "extn" field from the AJAX data into the filter property of the second column. I changed your search command to look for '1562'. In my situation I would want to change 1562 to some other value when the cell content changes and have it used when performing filter operations.

    Hope that makes sense, thanks for your assistance.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    Answer ✓

    Hi @darrenm ,

    I'm not quite following, but maybe this will help - it's showing how you can use a function for columns.render which can modify the value when you update the cell.

    Cheers,

    Colin

  • darrenmdarrenm Posts: 29Questions: 11Answers: 0

    OK thanks, I've got that working here: http://live.datatables.net/folupaxu/1/edit

    That's a better representation of what I'm trying to do. Unfortunately it's not working properly in my project which suggests I've got a bug somewhere, so I'll have to try and track that down. But at least I know render() is the way to go. Cheers.

This discussion has been closed.