首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用gridstack.js设置本地存储中的小部件数据

如何使用gridstack.js设置本地存储中的小部件数据
EN

Stack Overflow用户
提问于 2018-06-19 00:07:23
回答 1查看 3.7K关注 0票数 1

场景

我有一个仪表板与一些小部件分组到选项卡中,用户可以点击通过。小部件具有默认的起始位置,包括高度和宽度,但是如果用户移动它们或调整它们的大小(使用gridstack.js),我会将新的小部件数据保存到本地存储。

如果用户在选项卡之间单击,新的小部件位置将保持不变。

问题

当用户刷新页面时,将加载默认的小部件位置,而不是本地存储中的数据。我试着用getWidgetData()设置新的位置,但是没有成功。

我已经检查了我的grid-layout数据的本地存储,新的位置已经存在。

代码示例

下面是Codepen,因为我认为它需要allow-same-origin,从而打破了代码片段。

代码语言:javascript
复制
// Initialize zurb foundation
$(document).foundation();

$(function() {
  var grid = $('.grid-stack').data('gridstack');

  var options = {
    cellHeight: 40,
    verticalMargin: 28,
    animate: false,
  };

  // Initialize grid-stack widgets
  widgetsInit = function(options) {
    $('.grid-stack').gridstack(options);
    getWidgetData(); // Find and set positions from local storage
  }

  updateVisibleWidgetHeights = function(options) {
    var targetPanel = '.tabs-panel.is-active';

    $(targetPanel + ' .grid-stack .grid-stack-item').each(function(i){
      $(targetPanel + ' .grid-stack').data('gridstack').resize(
        $(targetPanel + ' .grid-stack .grid-stack-item')[i],
        $($(targetPanel + ' .grid-stack .grid-stack-item')[i]).attr('data-gs-width'),
        Math.ceil(($(targetPanel + ' .grid-stack .grid-stack-item .grid-stack-item-content')[i].scrollHeight + $(targetPanel + ' .grid-stack').data('gridstack').opts.verticalMargin) / ($(targetPanel + ' .grid-stack').data('gridstack').cellHeight() + $(targetPanel + ' .grid-stack').data('gridstack').opts.verticalMargin))
      );
    });
  }

  updateAllWidgetHeights = function(options) {
    $('.grid-stack .grid-stack-item').each(function(i){
      $('.grid-stack').data('gridstack').resize(
        $('.grid-stack .grid-stack-item')[i],
        $($('.grid-stack .grid-stack-item')[i]).attr('data-gs-width'),
        Math.ceil(($('.grid-stack .grid-stack-item .grid-stack-item-content')[i].scrollHeight + $('.grid-stack').data('gridstack').opts.verticalMargin) / ($('.grid-stack').data('gridstack').cellHeight() + $('.grid-stack').data('gridstack').opts.verticalMargin))
      );
    });
  }

  getWidgetData = function() {
    // // Load from local storage
    var serialization = null;
    if (serialization = localStorage.getItem("grid-layout")) {
      _.each(JSON.parse(serialization), function (node) {
        // Update each widget's properties from values in storage
        $('.grid-stack-item[data-custom-id='+ node.id +']').attr({
          'data-gs-x': node.x,
          'data-gs-y': node.y,
          'data-gs-width': node.width,
          'data-gs-height': node.height,
        });
      });
    } else {
      console.log("There was no local storage data to read")
    }
  }

  widgetsInit(options);
  updateVisibleWidgetHeights();

  $('.grid-stack').on('change', function (event, items) {
    // Save data to local storage
    var res = _.map($('.grid-stack .grid-stack-item'), function (el) {
      el = $(el);
      node = el.data('_gridstack_node');
      return {
        // Grab widget properties
        id: el.attr('data-custom-id'),
        x: node.x,
        y: node.y,
        width: node.width,
        height: node.height
      };
    });
    localStorage.setItem("grid-layout", JSON.stringify(res))
  });

  // On tab change update widget heights to the height of the content they contain
  $(document).on('change.zf.tabs', function() {
    updateVisibleWidgetHeights();
  })
});
代码语言:javascript
复制
.grid-stack > .grid-stack-item > .grid-stack-item-content {
    background-color: #fff;
    cursor: move;
    border: 1px solid #e3e3e3;
    border-radius: 4px;
	padding: 1rem;
}
代码语言:javascript
复制
<link href="https://cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.4.0/gridstack.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.4.3/css/foundation.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.4.0/gridstack.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.4.0/gridstack.jQueryUI.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.4.3/js/foundation.min.js"></script>


<ul class="tabs" data-tabs id="example-tabs" data-deep-link="true" data-update-history="true" data-match-height="false">
  <li class="tabs-title is-active"><a href="#panel1" aria-selected="true">Tab 1</a></li>
  <li class="tabs-title"><a href="#panel2">Tab 2</a></li>
</ul>

<div class="tabs-content unstyled" data-tabs-content="example-tabs">
  <div class="tabs-panel is-active" id="panel1">
    <div class="grid-stack">
      <div class="grid-stack-item" data-gs-x="0" data-gs-y="0" data-gs-width="4" data-gs-height="2">
        <div class="grid-stack-item-content">#1</div>
      </div>
      <div class="grid-stack-item" data-gs-x="4" data-gs-y="0" data-gs-width="4" data-gs-height="2">
        <div class="grid-stack-item-content">#2</div>
      </div>
      <div class="grid-stack-item" data-gs-x="8" data-gs-y="0"data-gs-width="4" data-gs-height="2">
        <div class="grid-stack-item-content">#3</div>
      </div>
    </div>
  </div>
  <div class="tabs-panel" id="panel2">
    <div class="grid-stack">
      <div class="grid-stack-item" data-gs-x="0" data-gs-y="0" data-gs-width="4" data-gs-height="2">
        <div class="grid-stack-item-content">#4</div>
      </div>
      <div class="grid-stack-item" data-gs-x="4" data-gs-y="0" data-gs-width="4" data-gs-height="2">
        <div class="grid-stack-item-content">#5</div>
      </div>
      <div class="grid-stack-item" data-gs-x="8" data-gs-y="0" data-gs-width="4" data-gs-height="2">
        <div class="grid-stack-item-content">#6</div>
      </div>
    </div>
  </div>
</div>

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50913511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档