Extjs checkcolumn disable for some rows, based on value

前端 未结 5 1465
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 14:07

I have a grid, with checkcolumn. It\'s dataIndex is, for example, \'checked\'.

I want to disable or hide checkboxes for some rows, where another value, \'can_be_chec

5条回答
  •  余生分开走
    2020-12-16 14:36

    I was looking for a solution to this and came across this question, but no workable solution anywhere to show a disabled checkbox instead of NO checkbox as covered in the other answer. It was kind of involved but here's what I did (for 4.1.0):

    1. I found that the extjs\examples\ux\css\CheckHeader.css file that is used by Ext.ux.CheckColumn does not have a disabled class, so I had to modify it to be more like the standard checkbox styling contained in ext-all.css (which does include a disabled checkbox class).

    2. I had to extend Ext.ux.CheckColumn to prevent events being fired from disabled checkboxes.

    3. Finally, I had to provide my own renderer to apply the disabled class according to my logic.

    The code is as follows.

    Modified CheckHeader.css:

    .x-grid-cell-checkcolumn .x-grid-cell-inner {
        padding-top: 4px;
        padding-bottom: 2px;
        line-height: 14px;
    }
    .x-grid-with-row-lines .x-grid-cell-checkcolumn .x-grid-cell-inner {
        padding-top: 3px;
    }
    
    .x-grid-checkheader {
        width: 13px;
        height: 13px;
        background-image: url('../images/checkbox.gif');
        background-repeat: no-repeat;
        background-color: transparent;
        overflow: hidden;
        padding: 0;
        border: 0;
        display: block;
        margin: auto;
    }
    
    .x-grid-checkheader-checked {
        background-position: 0 -13px;
    }
    
    .x-grid-checkheader-disabled {
        background-position: -39px 0;
    }
    
    .x-grid-checkheader-checked-disabled {
        background-position: -39px -13px;
    }
    
    .x-grid-checkheader-editor .x-form-cb-wrap {
        text-align: center;
    }
    

    The background-image url above points to this image which normally ships with extjs 4.1.0 at: extjs\resources\themes\images\default\form\checkbox.gif.

    extjs\resources\themes\images\default\form\checkbox.gif

    The extended Ext.ux.CheckColumn class so that events will not get fired from disabled checkcolumns:

    Ext.define('MyApp.ux.DisableCheckColumn', {
        extend: 'Ext.ux.CheckColumn',
        alias: 'widget.disablecheckcolumn',
    
        /**
         * Only process events for checkboxes that do not have a "disabled" class
         */
        processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
            var enabled = Ext.query('[class*=disabled]', cell).length == 0,
                me = this;
    
            if (enabled) {
                me.callParent(arguments);
            }
        },
    
    });
    

    Implementation with custom renderer to apply disabled class according to my own logic:

    column = {
        xtype: 'disablecheckcolumn',
        text: myText,
        dataIndex: myDataIndex,
        renderer: function(value, meta, record) {
            var cssPrefix = Ext.baseCSSPrefix,
                cls = [cssPrefix + 'grid-checkheader'],
                disabled = // logic to disable checkbox e.g.: !can_be_checked
    
            if (value && disabled) {
                cls.push(cssPrefix + 'grid-checkheader-checked-disabled');
            } else if (value) {
                cls.push(cssPrefix + 'grid-checkheader-checked');
            } else if (disabled) {
                cls.push(cssPrefix + 'grid-checkheader-disabled');
            }
    
            return '
     
    '; } };

提交回复
热议问题