简体   繁体   English

如何在 SessionStorage 中保存多个 js 对象?

[英]How save multiple js objects in SessionStorage?

I'm trying to send objects in a sessionStorage with a button calls ".buttonClipboard".我正在尝试使用按钮调用“.buttonClipboard”在 sessionStorage 中发送对象。 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. PD:rowSelected[0] 类似于 object 行中的 id。

To add multiple selected rows to the clipboard, you need to send all of them in push() , not just selectedRow[0] .要将多个选定的行添加到剪贴板,您需要将它们全部发送到push()中,而不仅仅是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):我有一个替代方案,但只能在一个表(tabDocument)而不是两个(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();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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