Select datatable row based on JSON object data from other array/object

Select datatable row based on JSON object data from other array/object

BScharfBScharf Posts: 9Questions: 2Answers: 0

I recently got my selection logic fixed and I can now send everything I need to my MVC controller. The one final case I need to handle is giving me some trouble, so Im trying to figure it out. I can have a user search for some records, and then select some of those and try to save something in the database. If an error occurs, I return the rows they selected and perform their original search again, but I need to some how take the rows that were selected before the error, and re-select them in the search I run for them again. See below for more information.

User searches for data

var searchData = [
    { ID = 1, Name = 'John', Age: 35 },
    { ID = 2, Name = 'Jane', Age: 50 },
    { ID = 3, Name = 'Megan', Age: 30 },
    { ID = 4, Name = 'Mike', Age: 49 }
];

Using the example above, say the user selects Jane and Megan, which I then pass that data to my MVC controller. An error occurs, so I return them to the view with those 2 records they selected (below).

var selectedRows = [
    { ID = 2, Name = 'Jane', Age: 50 },
    { ID = 3, Name = 'Megan', Age: 30 }
];

I then redo their original search, which returns the 4 original users, but I want to reselect Jane and Megan based on the ID being found in both the searchData and selectedRows, is this possible? One thing to keep in mind is I'm trying to avoid looping through one array and then the other, because the search data as well as selected data could be thousands of records. Also, some of the selected rows that I then need to re-select, could be on separate pages, (plus I'm using deferRender), so I cant do something at the DOM level, I need to manipulate the data object that I use when initializing the datatable.

This question has an accepted answers - jump to answer

Answers

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

    Hi @BScharf ,

    Probably the best way is to get the selected rows first,

    ids = table.rows({selected:true}).data().pluck('ID')
    

    then pass that into table.rows().select(), like

    table.rows(ids).select()
    

    Hopefully that would do the trick,

    Cheers,

    Colin

  • BScharfBScharf Posts: 9Questions: 2Answers: 0
    edited July 2019

    I just tried that and it didn't work. The pluck() method did work to get an array of all of my specific IDs, but when I called the select, nothing actually gets selected. Also, when I refer to ID, I don't mean the ID/index of the dataSource, I mean ID was a field within the dataSource. So it might have been better to call it PersonID.

    I updated a previous JSBin with the above changes.
    http://live.datatables.net/wimecujo/3/edit

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

    Ah, I see. If it's not the index of the data source, you'll need to iterate through the data unfortunately to get the matches you need - DataTables is heavily optimised so it shouldn't be too non-performant. If you use rows().every(), it should be pretty quick.

  • BScharfBScharf Posts: 9Questions: 2Answers: 0
    edited July 2019

    So I would essentially have to do something like this? if so, that seems like it would possibly take a VERY long time if I have 10s of thousands of rows either in the table, or possibly even 10s of thousands of rows in the selectedRows.

    table.rows().every(function () {
        var rowData = this.data();
        $.each(selectedRows, function (key, data) {
            if (rowData["PersonID"] = data["PersonID"]) {
                this.select();
            });
        });
    });
    
  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    I wouldn't iterate through the selected rows - just pluck() the value that you want to match as an array as you are above, then use indexOf to see if that row's data is in the array.

  • BScharfBScharf Posts: 9Questions: 2Answers: 0
    edited July 2019

    I ended up updating the JSBin to try that and I get an out of memory error, so that's not very comforting so far.

  • BScharfBScharf Posts: 9Questions: 2Answers: 0

    Ok, so I can use the pluck method on what was selected, and then iterate through that against the tables.rows(), but how would I use the indexOf to match, say the PersonID from the plucked rows against the table?

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

    You would do something like this: http://live.datatables.net/qurevalo/1/edit

    Cheers,

    Colin

  • BScharfBScharf Posts: 9Questions: 2Answers: 0

    Looks like that is a viable solution for right now. I will do some testing to make sure it runs properly. But so far so good, I appreciated the help.

This discussion has been closed.