How to select rows in datatable based on attribute values

Hello,

Whenever I edit some records in the datatable I refresh the table to make sure the table shows updated records from users of the application as well. But by refreshing I lose the selections i made before the update. I would like to preserve those selections (blue background). The records might switch positions in the table after refresh so I am guessing the IDs switch too. So is there a way I can select a record from datatable based on an attribute(column) after the refresh.

Comments

  • edited May 2019

    Hello @kchhatani ,

    First of all, I want to mention that Webix datatable provides a possibility to store/restore the state of the grid to cookie, local or session storage.
    The state object will contain information about the current datatable state including IDs of currently selected items (each ID is an object with row, column and id properties)

    To save the current datatable state to the local storage you should call the getState method as in:

    var state = grid.getState();
    webix.storage.local.put("state", state);
    

    To restore the saved state you should call the setState method:

    var state = webix.storage.local.get("state");
    if (state)
        grid.setState(state);
    

    More information you can find here

    If the id comes from server(which a random id) or data comes without id (so getting webix.uid()) - the solution will be different. With the help of unload it's possible to store the item and look for it by attributes via find()
    Like this:

    // before unload
    webix.attachEvent("unload", function(){
        webix.storage.local.put("selected_obj", grid.getSelectedId());
    });
    
    // after init
    var obj =  webix.storage.local.get("selected_obj");
    if (obj){
          var item_id = grid.find(/* find ID by other properties  */)
          if (item_id)
                grid.select(item_id);
    }
    
  • edited May 2019

    Thanks a lot @Nastja I appreciate the answer. I got it working but I am now having an occasional problem with the selection after the datatable load as sometimes the IDs are not valid.

    I am loading the datatable with following logic so I would assume that all the IDs would be valid as I am waiting for the table to load first

     datatable.load(datatable.config.url)
            .fail(() => {
                console.log('Problem loading table');
            })
            .then(() => {
                //logic for selection of the table 
                var item_id = datatable.find(/* find ID by other properties  */)
                if (item_id)
                    datatable.select(item_id);
            });
    

    But on datatable.select I get id 'undefined' sometimes. Can you see anything wrong with the above logic?

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion