ExtJS:当底层存储空为空时,从tabpanel中删除网格

时间:2010-03-08 16:26:03

标签: javascript extjs grid listener

我有TabPanel,其中包含连接到商店的网格。

有几个事件可能会从商店中删除元素。

我希望当商店为空时将Grid从TabPanel中删除,并且可能在我的代码中有一个地方来检查此事件。

我考虑过使用商店监听器,但不幸的是,这会导致Ext代码中出现异常。 我的假设是发生这种情况是因为在从tabpanel中删除后,在网格上执行渲染。

如何在不搞乱Ext的情况下完成这样的任务的任何想法都非常感谢。 谢谢:))

顺便说一句,这是一段代码摘录:

var myStore = new Ext.data.Store({
 reader: new Ext.data.JsonReader({fields: MyRecord}),
 listeners:{
  'clear': function(store, recs) {
   myTabPanel.remove(myGrid);
  },
  'remove': function(store, rec, idx) {
   if (store.getCount() == 0) {
    myTabPanel.remove(myGrid);
   }
  }
 }
});

var myGrid = new Ext.grid.GridPanel({
 id: "myGrid",
 title: "A Grid",
 store: myStore,
 frame:false,
 border:false,
 columns: [
 {
  header: 'Remove',
  align:'center',
  width: 45,
  sortable: false,
  renderer: function(value, metaData, record, rowIndex, colIndex, store) {
   return '<img src="images/remove.png" width="34" height="18"/>';
  }
 },{
  header: 'Some Data',
  dataIndex: 'data',
  sortable: true
 }
 ],
 listeners:{
  'cellclick':function(grid, rowIndex, colIndex, e){
   var rec = myStore.getAt(rowIndex);
   if(colIndex == 0){
    myStore.remove(rec);
   }
  }
 } 
});

var myTabPanel= new Ext.TabPanel({ 
 activeTab: 0,
 items: [ fooPanel, barPanel, myGrid]
});

2 个答案:

答案 0 :(得分:0)

您是否正在从标签中删除网格?看起来像一个奇怪的UI范例 - 为什么不保持空网格的“空”消息?

无论如何,我认为您的方法(使用商店事件和记录计数来确定何时删除它)基本上没问题。您可能想弄清楚如何解决您所说的错误。他们是什么?

答案 1 :(得分:0)

问题解决了:我只需要删除网格,将“autoDestroy”参数设置为“false”。 修正了以下代码。

var myStore = new Ext.data.Store({
 reader: new Ext.data.JsonReader({fields: MyRecord}),
 listeners:{
  'clear': function(store, recs) {
   myTabPanel.remove(myGrid, false);
  },
  'remove': function(store, rec, idx) {
   if (store.getCount() == 0) {
    myTabPanel.remove(myGrid, false);
   }
  }
 }
});

var myGrid = new Ext.grid.GridPanel({
 id: "myGrid",
 title: "A Grid",
 store: myStore,
 frame:false,
 border:false,
 columns: [
 {
  header: 'Remove',
  align:'center',
  width: 45,
  sortable: false,
  renderer: function(value, metaData, record, rowIndex, colIndex, store) {
   return '<img src="images/remove.png" width="34" height="18"/>';
  }
 },{
  header: 'Some Data',
  dataIndex: 'data',
  sortable: true
 }
 ],
 listeners:{
  'cellclick':function(grid, rowIndex, colIndex, e){
   var rec = myStore.getAt(rowIndex);
   if(colIndex == 0){
    myStore.remove(rec);
   }
  }
 } 
});

var myTabPanel= new Ext.TabPanel({ 
 activeTab: 0,
 items: [ fooPanel, barPanel, myGrid]
});