Home > Software engineering >  How save multiple js objects in SessionStorage?
How save multiple js objects in SessionStorage?

Time:12-16

I'm trying to send objects in a sessionStorage with a button calls ".buttonClipboard". But only send one. I want send more objects from the table.

This is the code:

$('#buttonsDocumentation .buttonClipboard').on('click', function(){
    clipboard = JSON.parse(sessionStorage.getItem("clipboard"));
    if($.inArray(rowSelected[0], clipboard) < 0){
        if(clipboard == null){
            clipboard = [];
        }
        clipboard.push(rowSelected[0]);
        sessionStorage.setItem('clipboard', JSON.stringify(clipboard));
    }
    testClipboard();
}); 

How is it?

PD: rowSelected[0] is like id from object row.

CodePudding user response:

To add multiple selected rows to the clipboard, you need to send all of them in push(), not just selectedRow[0]. You can use spread syntax for this:

clipboard.push(...selectedRow);

CodePudding user response:

I have an alternative, but only works in one table (tabDocument) instead of two (tabSearch):

$('#buttonsDocumentacion .botonClipboard').on('click', function(){
    var idsClip=[];
    if(clipboard = tableDocument.rows('.selected').data()){
        for(var i=0; i<clipboard.length; i  ){
            idsClip.push(clipboard[i][0]);
        }   
    }
    sessionStorage.setItem('clipboard', JSON.stringify(idsClip));
    compruebaClipboard();
})
  • Related