0

我有一个网格并使用rowactions 分组插件在分组标题中有一个操作按钮。网格中的第一个可见列是复选框列。我需要使同一组中的复选框的行为方式相同。1) 如果用户选中其中一个复选框,则必须选中同一组中的所有框。2)我还需要查看同一行中的另一个字段,如果它没有值,请禁用复选框(都在同一组中),否则应启用所有复选框。

我也试过 xtype:checkbox。对于#1,我只是不知道如何获得兄弟姐妹。我尝试使用 checkchange - 我可以在那里获得记录 - 但我不知道如何从同一组中获取其他记录。对于#2 - 我如何获取记录 - 我需要检查其中一个值并设置启用/禁用 ckeckbox。

有任何想法吗?谢谢你。

编辑:对于#2,我使用disable checkcolumn for some rows answer here

这是我的 index.cshtml

     <script type="text/javascript">
          Ext.require([
          'Ext.container.Viewport',
          'Ext.grid.*',
          'Ext.util.*',
          'Ext.Date.*',
          'Ext.ux.CheckColumn',

          'ACTGRID.ui.GroupPanelGrid'
         ]);

      Ext.onReady(function () {
          initialize();
      });


      function initialize() {
          Ext.Ajax.timeout = 60000; // 60 seconds

         var myGrid = Ext.create('ACTGRID.ui.GroupPanelGrid');
         var viewport = Ext.create('Ext.container.Viewport', {
              layout: 'border',
              items: [{
                      region: 'center',
                      title: 'Grid',
                      items: myGrid }]
          });
      };
   </script>

这是 GroupPanelGrid.js:

 Ext.Date.patterns = {
    ISO8601Long: "Y-m-d H:i:s",
    ISO8601Short: "Y-m-d",
    ShortDate: "n/j/Y",
    LongDate: "l, F d, Y",
    FullDateTime: "l, F d, Y g:i:s A",
    MonthDay: "F d",
    ShortTime: "g:i A",
    LongTime: "g:i:s A",
    SortableDateTime: "Y-m-d\\TH:i:s",
    UniversalSortableDateTime: "Y-m-d H:i:sO",
    YearMonth: "F, Y"
};


Ext.define('ACTGRID.ui.TransactionsLateModel', {
extend: 'Ext.data.Model',
fields: [
    { name: 'Id', type: 'int' },
    { name: 'Approval', type: 'boolean' },
    { name: 'ErrorComment', type: 'string' },
    { name: 'ErrorSource', type: 'string'},
    { name: 'RecordDate', type: 'date', dateFormat: 'MS' }],


idProperty: 'Id'
});

Ext.define("ACTGRID.ui.GroupPanelGrid", {
    extend: "Ext.grid.Panel",
    requires: ['ACTGRID.ui.TransactionsLateModel',
              'Ext.ux.grid.RowActions',
              'Ext.grid.feature.Grouping'],

    initComponent: function () { 
        var me = this;
        me.features = [Ext.create('Ext.grid.feature.Grouping', {
                      groupHeaderTpl: 'Grouping Header: {name} ({rows.length} 
                      Item{[values.rows.length > 1 ? "s" : ""]})',
                      enableGroupingMenu: false })];

        me.columns = me.buildColumns();
        me.store = Ext.create('Ext.data.Store', {
            model: 'ACTGRID.ui.TransactionsLateModel',
            remoteSort: true,
            storeId: 'TransactionsLateStoreId',
            autoLoad: true,
            buffered: true,
            autoSync: true,
            groupField: 'RecordDate',
            pageSize: 70,
            proxy: {
                type: 'rest',
                timeout: 600000,

                url: '/Home/ActionsGrid/',
                reader: {
                    type: 'json',
                    root: 'transaction',
                    totalProperty: "Total"
                },
                writer: {
                    type: 'json',
                    root: 'transaction'
                }
            }
        });


       me.autoSizeColumns = true;
       me.autoScroll = true;
       me.forcefit = true;
       me.callParent(arguments);

  },


buildColumns: function () {
    var me = this;

    return [
    {
        xtype: 'rowactions', hidden: true, hideable: false,
        actions: [{}],
        keepSelection: true,
        groupActions: [{
            iconCls: 'icon-grid',
            qtip: 'Action on Group',
            align: 'left',
            callback: function (grid, records, action, groupValue) {
                var rec = [].concat(records);
                /*does something unrelated*/
            }
        }]
    },
    {
        text: 'Approval', dataIndex: 'Approval', sortable: true,
        width: 50,
        xtype: 'checkcolumn',
        listeners: {
            checkchange: function (column, recordIndex, checked) {
                var record = me.getStore().data.items[recordIndex].data;
                /* Do something here? - How do I get all the siblings */
            },

            beforerender: function (e, eOpts) {
                /* How do I check another cell in this row to see if it has a value
                   And disable the ckeckbox and set it read only?  */
            }
        }
    },
    { text:'ErrorComment', dataIndex: 'ErrorComment' },
    { text:'ErrorSource', dataIndex: 'ErrorSource'},
    { text:'RecordDate', dataIndex: 'RecordDate', renderer: Ext.util.Format.dateRenderer        (Ext.Date.patterns.ShortDate)}];
    },
   height: 600,
   width: 'auto'

   });

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        return View();
    }


    [AcceptVerbs(HttpVerbs.Get)]
    [ActionName("ActionsGrid")]
    public ActionResult GetActionsGrid()
    {
        DetailsViewModel model = new DetailsViewModel();
        List<ExtJsTreeGrid.Models.ActionGrid> ActionsFromDB = model.GetActionsGrid();

        model.transaction = ActionsFromDB;
        model.success = true;
        return Json(model, JsonRequestBehavior.AllowGet);
    }
    [AcceptVerbs(HttpVerbs.Put)]
    [ActionName("ActionsGrid")]
    public ActionResult SetActionsGrid()
    {
       // do something 
    }

}

模型:

 public class ActionGrid
 {
    public Int32 Id { get; set; }
    public bool Approval { get; set; }
    public string ErrorComment { get; set; }
    public string ErrorSource { get; set; }
    public DateTime RecordDate { get; set; }
}



 public class DetailsViewModel
 {
    public List<ActionGrid> transaction = new List<ActionGrid>();
    public bool success = true;

    public List<ActionGrid> GetActionsGrid()
    {
       return new List<ActionGrid> {
             new ActionGrid { Id = 1, 
                        Approval = true, 
                        ErrorComment = "Comment", 
                        ErrorSource = string.Empty,
                        RecordDate = new DateTime(1979, 11, 23)
             },
              new ActionGrid { Id = 5, 
                        Approval = true, 
                        ErrorComment = "Comment", 
                        ErrorSource = "brodas",
                        RecordDate = new DateTime(1979, 11, 23)
             },
              new ActionGrid { Id = 6, 
                        Approval = true, 
                        ErrorComment = "Comment", 
                        ErrorSource = string.Empty,
                        RecordDate = new DateTime(1979, 11, 23)
             },
              new ActionGrid { Id = 7, 
                        Approval = true, 
                        ErrorComment = string.Empty, 
                        ErrorSource = "brodas",
                        RecordDate = new DateTime(1979, 11, 23)
             },
             new ActionGrid { Id = 2, 
                        Approval = true, 
                        ErrorComment = string.Empty, 
                        ErrorSource = string.Empty,
                        RecordDate = new DateTime(1980, 05, 20)
             },
             new ActionGrid { Id = 3, 
                        Approval = true, 
                        ErrorComment = "Comment", 
                        ErrorSource = "brodas",
                        RecordDate = new DateTime(1995, 01, 12)
             },
             new ActionGrid { Id = 4, 
                        Approval = true, 
                        ErrorComment = string.Empty, 
                        ErrorSource = string.Empty,
                        RecordDate = new DateTime(2010, 09, 02)
             },
             new ActionGrid { Id = 8, 
                        Approval = true, 
                        ErrorComment = string.Empty, 
                        ErrorSource = "brodas",
                        RecordDate = new DateTime(2010, 09, 02)
             },
             new ActionGrid { Id = 9, 
                        Approval = true, 
                        ErrorComment = "Comment Errors", 
                        ErrorSource = string.Empty,
                        RecordDate = new DateTime(2010, 09, 02)
             },
             new ActionGrid { Id = 10, 
                        Approval = true, 
                        ErrorComment = "Comment Errors", 
                        ErrorSource = "brodas",
                        RecordDate = new DateTime(2010, 09, 02)
             },
             new ActionGrid { Id = 11, 
                        Approval = true, 
                        ErrorComment = string.Empty, 
                        ErrorSource = "brodas",
                        RecordDate = new DateTime(2010, 09, 02)
             }};

    }
4

2 回答 2

1

对于需求 #1,checkchange监听器确实是实现它的正确位置。

要获取组中的所有记录,您需要获取对网格存储的引用,然后使用其query方法、分组字段和检查记录中的值。

这是一个例子:

checkchange: function(col, index, checked) {
    var grid = col.up('tablepanel'), 
        store = grid.getStore(),
        record = store.getAt(index),
        group = record.get('group'),
        // returns a MixedCollection (not a simple array)
        groupRecords = store.query('group', group);

    // apply the same check status to all the group
    groupRecords.each(function(record) {
        record.set('checked', checked);
    });
}

对于您的第二个要求,我真的不明白您希望它何时发生。

无论如何,检查列不支持在行级别禁用,因此您必须先实现它。

最简单的方法是在模型中添加一个字段以保持禁用状态。我们称它为“禁用”。然后,你需要两件事。

首先,渲染复选框禁用状态。为此,让我们覆盖检查列的渲染器:

,renderer: function(value, meta) {
    // Adding disabled class according to record's checkable
    // field
    if (meta.record.get('disabled')) {
        meta.tdCls += ' ' + this.disabledCls;
    }

    // calling the overridden method (cannot use callParent
    // since we haven't define a new class)
    return Ext.grid.column.CheckColumn.prototype.renderer.apply(this, arguments);
}

接下来,我们需要防止选中或取消选中禁用的记录。这可以在beforecheckchange事件中完成:

,beforecheckchange: function(col, index) {
    var grid = col.up('tablepanel'), 
        store = grid.getStore(),
        record = store.getAt(index);

    if (record.get('disabled')) {
        return false;
    }
}

现在,您可以通过设置记录的“禁用”字段来启用/禁用记录的复选框。

record.set('disabled', true); // enable
record.set('disabled', false); // disable

完整示例

这是一个完整的示例,它实现了您的第一个要求,也许是您的第二个要求。如果那不是您对第二个要求的意思,那应该带您到那里...

可以看到这个例子在这个 fiddle中运行。

Ext.widget('grid', {
    renderTo: Ext.getBody()
    ,height: 300

    ,features: [{ftype: 'grouping'}]

    ,store: {
        proxy: {type: 'memory', reader: 'array'}
        ,fields: [
            'name', // something to see
            'group', // grouping field
            'checkable', // for req #2
            'checked', // for check column
            'disabled' // for disabled state
        ]
        ,groupField: 'group'
        ,data: [
            ['Foo 1', 'foo', true],
            ['Foo 2', 'foo', false],
            ['Foo 3', 'foo', true],
            ['Bar 1', 'bar', false],
            ['Bar 2', 'bar', true],
            ['Baz 1', 'baz', false],
            ['Baz 2', 'baz', false],
            ['Baz 3', 'baz', true]
        ]
    }

    ,columns: [{
        xtype: 'checkcolumn'
        ,width: 50
        ,dataIndex: 'checked'

        ,listeners: {
            checkchange: function(col, index, checked) {
                var grid = col.up('tablepanel'), 
                    store = grid.getStore(),
                    record = store.getAt(index),
                    group = record.get('group'),
                    // returns a MixedCollection (not a simple array)
                    groupRecords = store.query('group', group),
                    // for req #2
                    disableGroup = !record.get('checkable');

                groupRecords.each(function(record) {
                    record.set('checked', checked);

                    // Here's how to disable the group... If that's
                    // really what you want.
                    record.set('disabled', disableGroup);
                });
            }

            // Returning false from this listener will prevent the
            // check change. This is used to implement disabled
            // checkboxes.
            ,beforecheckchange: function(col, index) {
                var grid = col.up('tablepanel'), 
                    store = grid.getStore(),
                    record = store.getAt(index);

                if (record.get('disabled')) {
                    return false;
                }
            }
        }

        ,renderer: function(value, meta) {
            // Adding disabled class according to record's checkable
            // field
            if (meta.record.get('disabled')) {
                meta.tdCls += ' ' + this.disabledCls;
            }

            // calling the overridden method (cannot use callParent
            // since we haven't define a new class)
            return Ext.grid.column.CheckColumn.prototype.renderer.apply(this, arguments);
        }
    },{
        dataIndex: 'name'
        ,text: "Name"
        ,flex: 1
    },{
        dataIndex: 'checkable'
        ,text: "Checkable"
    }]
});
于 2013-07-24T22:35:39.827 回答
0

至于我的情况,我有一个单独的checkbox外部gridpanel控制checkcolumn.

//Avoid view update after each row
grid.store.suspendEvents();
grid.store.each(function(rec) {
    rec.set('ctrl_select_flag', ctrl.value);
});
grid.store.resumeEvents();
grid.getView().refresh();

ctrl.value指的是控制我的checkcolumn选择的组件。ctrl_select_flag指的dataIndex是我的checkcolumn

于 2013-07-25T01:19:12.663 回答