localStorage 本地缓存使用

文章展示了如何在JavaScript中使用localStorage进行数据存储。当文件导入导致的数据量过大时,数据会被暂存到缓存中。`storageRoation`函数用于存储数据到localStorage,而`queryRoation`函数则负责从缓存读取并显示在表格中,利用layui库渲染表格。

文件导入功能支持修改的状态下,数据量过大想要可以暂存在缓存中,并且可以从缓存中读取。

 实现代码如下:

<div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-filter="formSubmitBtn" lay-submit>保存</button>
                    <input type="button" class="layui-btn" value="暂存" onclick="storageRoation()" />
                    <input type="button" class="layui-btn" value="读取" onclick="queryRoation()" />

                </div>
            </div>





//暂存方法
 /**
     * 缓存
     */
    function storageRoation () {
        if(window.Storage){
            //轮转集合字符串
            var roationListStr = localStorage.getItem("roationList");

            var roationList;

            if (!roationListStr) {
                roationListStr = "[]";
            }else{
                roationListStr = importData;
            }
            // 转换json对象 我这里就是所以这一步不需要
            // roationList = JSON.parse(roationListStr);

            // 转换为字符串
            roationList = JSON.stringify(roationListStr);


            // 保存到storage
            localStorage.setItem("roationList", roationList);
        }
    };



//读取缓存方法
/**
     * 读取缓存
     */
    function queryRoation () {
        if (!localStorage.hasOwnProperty("roationList"))
            return;

        // 把Storage中的数据绑定到 表格
        if (window.Storage) {
            var roationListStr = localStorage.getItem("roationList");
            var roationList = JSON.parse(roationListStr);
            console.log(roationList)

            //注意这里要使用layui.table
            layui.table.render({
                elem: '#test',
                height: 460,
                page: true,
                data: roationList
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
                , cols: [
                    [
                        {field: 'uname', title: '姓名', width: 120},
                        {field: 'ucode', title: '工号', width: 150},
                    ]
                ],
                done : function(){
                }
            });
        }
    };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值