简体   繁体   中英

How save multiple js objects in SessionStorage?

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.

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);

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();
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM