ExtJS-3,Ext.grid.GridPanel,多行复选框:如何一次提交所有选中的项目?

时间:2012-01-20 22:00:47

标签: javascript forms extjs multi-select

我正在使用ExtJS3,我有一个Ext.grid.GridPanel,其中每行左边都有一个复选框。在顶部有两个按钮:'刷新'和'提交'。我希望能够选择多行并通过单击提交按钮提交所有行。正如现在我选择多行并点击提交时,它将提交第一个,然后从网格中删除该项,我必须再次点击提交(以前检查的行仍然被检查)。

如何更改以下代码以使提交按钮一次发送所有行?

var dataStore = new Ext.data.SimpleStore({   fields: [
        {name: 'node'},
        {name: 'ip'},
        {name: 'groups'}
        ]});
var checkBoxSelMod = new Ext.grid.CheckboxSelectionModel();
var dataGrid = new Ext.grid.GridPanel ({
renderTo: document.body,
   clickstoEdit: 2,
   selModel : checkBoxSelMod,
   xtype: 'grid',
   layout: 'fit',
   sm: new Ext.grid.CheckboxSelectionModel(),
   tbar: [
          {
          text: 'Refresh',
              icon:    '$nroot/includes/extjs3/resources/images/slate/button/table_refresh.png',
              cls: 'x-btn-text-icon',
              handler: function() {
              window.location = '$nroot/index.php/imaging/index';},
              scope: this,
              },
          {
          text: 'Submit Machine(s)',
              icon: '$nroot/includes/extjs3/resources/images/slate/button/table_add.png',
              cls: 'x-btn-text-icon',
              handler: function() {
              var sm = dataGrid.getSelectionModel();
              var sel = sm.getSelected();
              if (sm.hasSelection()) {


                Ext.Msg.show({
                  title: 'Image Machine?',
                      buttons: Ext.MessageBox.YESNO,
                      msg: 'Continue with imaging (no undo!) process for server: '+sel.data.node+'?',
                      fn: function(btn){
                      if (btn == 'yes'){
                        var conn = new Ext.data.Connection();
                        conn.request({
                          url: '$nroot/index.php/imaging/image/',
                              params: {
                            action: 'delete',
                                node: sel.data.node,
                                mgmtip: sel.data.ip
                                },                            
                              success: function(resp,opt) {
                              dataGrid.getStore().remove(sel);
                            },
                              failure: function(resp,opt) {
                              Ext.Msg.alert('Error','Unable to image server - check debug logs');
                            }
                          });
                      }
                    }
                  });
              }
            }
          }               
          ],
   store: dataStore,
   columns: [                  
             checkBoxSelMod,
             { id: 'node', header: "Node", width: 150, sortable: true, dataIndex: 'node'},
             { id: 'ip', header: "IP", width: 120, sortable: false, dataIndex: 'ip'},
             { id: 'groups', header: "Groups", width: 100, sortable: true, dataIndex: 'groups'},
             ],
   stripeRows: true,
   autoExpandColumn: 'node',
   listeners: { 
 render: function(){ this.store.loadData(dataList); }
     }      
})});

当我将代码从getSelected更改为getSelections时,它会在页面加载时返回:

item type is invalid for AcceptItem action

我找不到任何显示使用提交为GridPanels进行多选的示例。有没有我可以参考的?

编辑,根据以下解决方案,我修改了代码,工作如下:

              var sels = sm.getSelections();
              if (sels.length > 0) {
                var ips = [], nodes = [];
                Ext.each(sels, function(sel) {
                           ips.push(sel.get('ip'));
                           nodes.push(sel.get('node'));
                         });

                Ext.Msg.show({
                  title: 'Image Machine?',
                      buttons: Ext.MessageBox.YESNO,
                      msg: 'Continue with imaging (no undo!) process for servers: '+nodes.join(",")+'?',
                      fn: function(btn){
                      if (btn == 'yes'){
                        Ext.each(sels, function(sel) {
                                   var conn = new Ext.data.Connection();
                                   conn.request({
                                     url: '$nroot/index.php/imaging/image/',
                                         params: {
                                       node: sel.get('node'),
                                           mgmtip: sel.get('ip')
                                           },                            
                                         success: function(resp,opt) {
                                         dataGrid.getStore().remove(sel);
                                       },
                                         failure: function(resp,opt) {
                                         Ext.Msg.alert('Error','Unable to image server - check debug logs');
                                       }
                                     });
                                 })
                          }
                    }
                  });
              }
            }

1 个答案:

答案 0 :(得分:0)

最简单的方法是遍历所选记录并为每条记录提出问题。

var sels = sm.getSelections();
Ext.each(sels, function(sel) {
    var node = sel.get('node'),
        ip = sel.get('ip');
    Ext.Msg.show({
        title: 'Image Machine?',
        buttons: Ext.MessageBox.YESNO,
        msg: 'Continue with imaging (no undo!) process for server: '+node+'?',
        fn: function(btn){
            if (btn == 'yes'){
                var conn = new Ext.data.Connection();
                conn.request({
                    url: '$nroot/index.php/imaging/image/',
                    params: {
                        action: 'delete',
                        node: node,
                        mgmtip: ip
                    },                            
                    success: function(resp,opt) {
                        dataGrid.getStore().remove(sel);
                    },
                    failure: function(resp,opt) {
                        Ext.Msg.alert('Error','Unable to image server - check debug logs');
                    }
                });
            }
        }
    });
});

但是,对于用户来说,获取DataGrid中选择的每一行的提示可能不太好。但是,如果您的服务('$ nroot / index.php / imaging / image /')支持发布多个项目,您可以询问用户一次并发布所有项目。即。

var sels = sm.getSelections();
if (sels.length > 0) {
    var ips = [], nodes = [];
    Ext.each(sels, function(sel) {
        ips.push(sel.get('ip'));
        nodes.push(sel.get('node'));
    });

    Ext.Msg.show({
        title: 'Image Machine?',
        buttons: Ext.MessageBox.YESNO,
        msg: 'Continue with imaging (no undo!) process for servers: '+nodes.join(",")+'?',
        fn: function(btn){
            if (btn == 'yes'){
                var conn = new Ext.data.Connection();
                conn.request({
                    url: '$nroot/index.php/imaging/image/',
                    params: {
                        action: 'delete',
                        nodes: nodes,
                        mgmtips: ips
                    },                            
                    success: function(resp,opt) {
                        Ext.each(sels, function() { dataGrid.getStore().remove(this) });
                    },
                    failure: function(resp,opt) {
                        Ext.Msg.alert('Error','Unable to image server - check debug logs');
                    }
                });
            }
        }
    });
}