在Javascript中刷新按钮更新手风琴

时间:2015-02-11 23:21:15

标签: javascript jquery json

我想编辑accordion header(formationName),一旦我点击刷新按钮,它应该更新手风琴标题。我无法弄清楚如何处理这个问题。

   $("#refresh").click(function(){
     myData.offsetFormations[0]["FormationName"]="party";
     json = JSON.stringify(myData);
     alert( json );
 });

小提琴:http://jsfiddle.net/xg7cr0g4/76/

1 个答案:

答案 0 :(得分:2)

这取决于您的情况。如果经常刷新大量数据,您可能需要进行就地编辑。

如果您只是在点击时刷新并且数据不是实质性的,那么只需按照原来的方式重建表格。这方面的一个例子(尽管它不太有用):

http://jsfiddle.net/xg7cr0g4/78/

var build = function(){
  //...build the grid/accordion here on demand (load,reload,programmatically)
};

var refresh = function(){
  //update json  
  build(); //rebuild;
};

$(document).ready(function(){
  // build on load
  build();
});

jQuery UI手风琴需要在重建时销毁:

re-initialize jquery accordion on callback

这是工作小提琴:

http://jsfiddle.net/xg7cr0g4/79/

您还可以按照模块模式更好地组织代码并避免全局功能:

var Grid = function(){
   var self = this;

   this.build = function(){
     ...
   };

   this.rebuild = function(){
      ...
   };

   this.init = function(){
     ...
     this.build();

     $('#refresh').on('click', this.refresh);

     ...etc
   };

   $(this.init);
};

var grid = new Grid();

grid.rebuild();

要回答您的其他问题,请添加其他方法:

this.setHeader = function(header){
   // similar to rebuild, change the json using param
   x[...] = header;
   this.build();
};