2014-02-21 76 views
0

的状态如何保存添加的窗口小部件的状态:下面是我使用的按钮,点击添加插件的功能:保存添加小部件

$('.js-resize-random').on('click', function() { 
    gridster.add_widget('<li class="new">The new widget...</li>', 2, 1); 
}); 

下面是HTML:

<div> 
<button class="js-resize-random">Add widget</button></div> 

在刷新时我需要添加小部件以保持其状态。我怎么做? 任何想法?????

提前致谢!

回答

0

我最近为保存和加载仪表板小部件的位置做了一个指令。这是小部件div的一个简单属性。

appControl.directive('saveLoadGridsterPosition', ['whateverStoringDependency', function($whateverStoringDependency) 
{ 
    return function(scope, element, attrs) { 

     var objectLoaded = whateverStoringDependency.load(attrs.saveLoadGridsterPosition); 
     // Empty or errorous load 
     if ((objectLoaded == undefined) || (objectLoaded == null)) { 
      console.log('Error loading ' + attrs.saveLoadGridsterPosition+'\'s position.'); 
     } 
     else { 
      // Here you set the row/col somewhere in the scope, for me its like scope.widget.row 
      scope.widget.row = objectLoaded.row; 
      scope.widget.col = objectLoaded.col; 
     } 

     var save = function() { 
      // Here you should get the row/col from the scope 
      var r = scope.widget.row; 
      var c = scope.widget.col; 
      var savingObject = {row: r, col: c}; 
      whateverStoringDependency.save(attrs.saveLoadGridsterPosition, savingObject); 
     } 

     scope.$on('$destroy', save); 
    } 
}]); 

'whateverStoringDependency'可以是从本地存储,Cookie或会话保存/负载的东西的定制服务。然后我这样使用它:

<div gridster="gridsterOpts"> 
    <ul> 
     <li ng-repeat="widget in widgets" gridster-item row="widget.row" col="widget.col" size-x="widget.sizeX" size-y="widget.sizeY"> 
      <div data-widget="widget" save-load-gridster-position="{{widget.name}}"></div> 
     </li> 
    </ul> 
</div> 

也许这不是很好,但对我来说它完美的作品。它也可以在任何角度事件上做到这一点,就像你定义"savePositions"并听取它而不是"$destroy"。希望能帮助到你!

0

我会说如果你想永久性的改变你可能需要用Ajax做一些事情来将新的标记写入你的数据库。

如果它是更具体的会话特定,你可能会用cookie来解决它。

<?php 
//check for cookie value 
$widgetVar = $_COOKIE['widgetStatus']; 
?> 

<?php if($widgetVar!='true'): ?> 
$('.js-resize-random').on('click', function() { 
    gridster.add_widget('<li class="new">The new widget...</li>', 2, 1); 
    //code to set the cookie once the button is clicked 
    document.cookie = "widgetStatus=true"; 

}); 
<?php else: ?> 

    This markup renders if the cookie is true when the page loads. So this is where the widget would go. 

<?php endif; ?> 
+0

我需要获取我添加的widget来获取'widgetStatus'的id?如果是这样,我该怎么做?也FYI:我正在使用jquery-gridster。 – user2942566

+0

这听起来像你说的,可能有不同的小部件,所以你需要代码来检测哪些存在?也许那么'widgetStatus'应该是逗号描述的ID字符串而不是简单的布尔值?你可以将字符串解析成数组并通过PHP或JS进行检查。 – Ecksley