【问题标题】:ExtJS-3, Ext.grid.GridPanel, muti-row checkboxes: how do I submit all checked items at once?ExtJS-3、Ext.grid.GridPanel、多行复选框:如何一次提交所有选中的项目?
【发布时间】:2012-01-20 22:00:46
【问题描述】:

我正在使用 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

我找不到任何显示多选并提交 GridPanel 的示例。有没有可以参考的?

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

              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');
                                       }
                                     });
                                 })
                          }
                    }
                  });
              }
            }

【问题讨论】:

    标签: javascript forms extjs multi-select


    【解决方案1】:

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

    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');
                        }
                    });
                }
            }
        });
    }
    

    【讨论】:

    • 我最终对您的第二个解决方案进行了修改。
    猜你喜欢
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    相关资源
    最近更新 更多