extjs4 grid 增删改

今天的任务是:grid删除选择的行,动态新增行中各列规则的校验
说明:这个任务是其实是就是对Grid进行增删查改。Grid包括EditorGridPanel


1.对Grid进行删除
function() {
     var _grid = Ext.getCmp("id_of_grid");
     var content = _grid.getSelectionModel().getSelected();                           //取得选择的那一行
     if (content != null) {
      store.remove(content);                                                                       //grid的数据是store,修改store就能修改显示的数据
     } else {
      Ext.MessageBox.alert("系统提示", "请选择要删除的数据!!!");
     }
     if (store.getCount() <= 0) {
      Ext.getCmp("departmentid").enable();
     }
}

2.对Grid进行增加, 并验证增加内容是否合法
1>对Ext.GridPanel进行增加
 

add = function(){
  var rec;

 var id;
 var gridStore = Ext.getCmp("eGrid").store;
var plant = Ext.getCmp("eGrid").store.recordType;                            //与record.create一致
 var _rs = new plant({                                                                      //增加的数据
    id:6,
    name:'小兰',
    email:'[email protected]',
    sex:'女',
    bornDate:'1993-3-7'
   });
 var len = gridStore.getCount();
 
 for (var i = 0; i < len; i++) {
  if (_rs.get("id") == gridStore.getAt(i)
    .get("id")) {
   Ext.Msg.alert("提示", "该ID已经存在,不能重复录入!");
   return;
  }
 }

 if (_rs.get("id") > 100 || _rs.get("id") < 1)
 {
  Ext.Msg.alert("提示", "ID错误");
  return;
 }
 Ext.getCmp("eGrid").store.add(_rs);                                     //往store里添加一项
}


2>对Ext.EditorGridPanel进行增加

 listeners :{
  validateedit : check                                                               //注意:不是afteredit
 }


check = function(obj){
 if (obj.field =="id"){
  var oldValue = obj.originalValue;
  var gridStore = Ext.getCmp("eGrid").store;
  var newValue = obj.value;
  var len = gridStore.getCount();
  
  for (var i = 0; i < len; i++) {
   if (newValue == gridStore.getAt(i).get("id")) {
    Ext.Msg.alert("Message", "The ID you input is Exsiting!");
    return false;
   }
  }
 
  if (newValue > 100 || newValue < 1)
  {
   Ext.Msg.alert("Message", "The ID should 1~100!");
   return false;
  }
 }
}
 


 

你可能感兴趣的:(职场,delete,insert,extjs4,grid,休闲)