// 这个继承比较简单,看注释就会清楚了,文件名定义为CustomGridPanel.js
Ext.grid.CustomGridPanel = function(c){
 Ext.grid.CustomGridPanel.superclass.constructor.call(this, c);
};

Ext.extend(Ext.grid.CustomGridPanel, Ext.grid.GridPanel, {
 // 选择下一行记录,如果没有下一行记录则选择上一行记录,会刷新表格
   selectNext: function(){
     var grid_ = this;
     var selModel = grid_.getSelectionModel();
     var nextSel = null;
     if(selModel.hasNext()){
      selModel.selectNext();
     }else if(selModel.hasPrevious()){
      selModel.selectPrevious();
     }else{
      return null;
     }
     nextSel = selModel.getSelected();
     grid_.getView().refresh();
     return nextSel;
//    if(nextSel){selModel.selectRecords([nextSel]);}
   },
  // 根据已经选择的排序信息,插入一条记录,并选择插入的记录
   addSorted: function(record,dwr,dwrOpts){
     var grid_ = this;
     var result = grid_.invokeDwr(record,dwr,dwrOpts);
     if(result){
     grid_.getStore().addSorted(record);
     grid_.getView().refresh();
     grid_.getSelectionModel().selectRecords([record]);
    }
    return result;
   },
   // 在最后面插入一条记录,并选择插入的记录
   insertLast: function(record,dwr,dwrOpts){
     var grid = this;
    var index = grid.getStore().getCount();
    return grid.insert(index,record,dwr,dwrOpts);
   },
   // 在给定索引上插入一条记录,并选择插入的记录
   insert: function(index,record,dwr,dwrOpts){
     var grid_ = this;
     var result = grid_.invokeDwr(record,dwr,dwrOpts);
     if(result){
     grid_.getStore().insert(index, record);
     grid_.getView().refresh();
     grid_.getSelectionModel().selectRecords([record]);
  }
  return result;
   },
  
   invokeDwr: function(record, dwr, dwrOpts){
     var flag = true;
    if(dwr){
        var dwrObj = {async: false};
        if(dwrOpts){
         dwrObj = Ext.apply(dwrObj, dwrOpts);
        }
        dwrObj.errorHandler = function(text,e){
          flag = false;
          Ext.CustomUtil.error(text);
        };
//     dwr.apply(this, [record.data, dwrObj]);
   dwr(record.data, dwrObj);
     return flag;
    } else {
     return false;
    }
   },
   // 删除一条记录,选择下一条可以删除的记录
   removeRecord: function(record, dwr, dwrOpts){
    var grid = this;
    var err = false;
    err = ! grid.invokeDwr(record, dwr, dwrOpts);
    if(err){return;}
    var next = grid.selectNext();
    grid.getStore().remove(record);
    if(next){grid.getSelectionModel().selectRecords([next]);}
   },
   // 获取选择的单一记录
   getSelected: function(){
    return this.getSelectionModel().getSelected();
   }
});