最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I want to hide checkboxes on non-leaf nodes in a treegrid (treepanel with columns) - Stack Overflow

programmeradmin2浏览0评论

In an Ext JS app I have a tree panel with a checkbox column (xtype: 'checkcolumn'). But I only want to display the checkbox on the leaf nodes. In the parent nodes I want to hide the checkboxes (or at least disable them).

How can I achieve this?

In an Ext JS app I have a tree panel with a checkbox column (xtype: 'checkcolumn'). But I only want to display the checkbox on the leaf nodes. In the parent nodes I want to hide the checkboxes (or at least disable them).

How can I achieve this?

Share Improve this question asked Oct 25, 2013 at 9:48 Eugene GillEugene Gill 3594 silver badges15 bronze badges 2
  • Which Ext JS version? – Krzysztof Commented Oct 25, 2013 at 11:59
  • Version is Ext JS 4.2.1 – Eugene Gill Commented Oct 25, 2013 at 14:45
Add a ment  | 

6 Answers 6

Reset to default 4

Building on matt's answer, this can be more easily achieved using the renderer method, ala:

renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
   if (!record.isLeaf()) {
      metaData.style = "display: none;";
   }
}

The above methods didn't work for me. I am using ExtJS 5.01. The above solutions generated some XTemplate errors. So I used the following:

Ext.define('MyApp.view.LeafOnlyCheckColumn', {
extend: 'Ext.grid.column.CheckColumn',
alias: 'widget.leafOnlyCheckColumn',

processEvent: function(type, view, cell, recordIndex, cellIndex, e, record, row) {
    if (record.isLeaf()) {
        return this.callParent(arguments);
    }
    else {
        return MyApp.view.LeafOnlyCheckColumn.superclass.superclass.processEvent.apply(this, arguments);
    }
},

defaultRenderer: function(value, cellValues){
    if(cellValues.record.isLeaf()){
        return this.callParent(arguments);
    }else{
        var cssPrefix = Ext.baseCSSPrefix,
        cls = cssPrefix + 'grid-checkcolumn';
        cellValues.tdCls += ' hiddenCheckCls';
        return '<img class="' + cls + '" src="' + Ext.BLANK_IMAGE_URL + '"/>';
    }
}
});

Then I added the following css:

.hiddenCheckCls{
    visibility: hidden;
}

And used the class with the xtype: leafOnlyCheckColumn

Hope that helps!

It's not supported by default, but you could provide your own column class which extends Ext.grid.column.CheckColumn:

Ext.define('My.tree.column.CheckColumn', {
    extend: 'Ext.grid.column.CheckColumn',
    alias: 'widget.mytreecheckcolumn',

    processEvent: function(type, view, cell, recordIndex, cellIndex, e, record, row) {
        if (record.isLeaf()) {
            return this.callParent(arguments);
        }
        else {
            return My.tree.column.CheckColumn.superclass.superclass.processEvent.apply(this, arguments);
        }
    },

    renderer : function(value, meta, record) {
        if (record.isLeaf()) {
            return this.callParent(arguments);
        }
        return '';
    }
});

And use that in your tree panel:

columns: [{
    xtype: 'treecolumn'
},{
    xtype: 'mytreecheckcolumn',
    dataIndex: 'active'
}]

Also this model fields specification can help you:

Ext.define("ExampleModel",{
  extend: "Ext.data.Model",
  fields: [{
    name: 'checked',
    type: 'boolean', 
    convert: function(v,rec) {
      return !rec.isLeaf() ? null : rec.data.checked; 
    }
  }]        
});

Simple solution is to use 2 different models. One for your parent nodes that does not have a field named 'checked' in it.

Ext.define('App.model.Parent', {
    extend: 'Ext.data.TreeModel',
    fields: [{
        name: 'title'
    }]
});

One for child nodes that does have a field named 'checked'.

Ext.define('App.model.Child', {
    extend: 'Ext.data.TreeModel',
    fields: [{
        name: 'checked',
        type: 'boolean'
    },{
        name: 'title'
    }]
});

Then in the tree only nodes with the 'checked' field in them will show checkboxes.

I spent much time to solve same task in ExtJS 7.5.1, for some reason "callParent" and superclass didn't work for me. But this.defaultRenderer in renderer function works:

renderer: function(value, meta, record) {
    if (record.isLeaf()) {
         return this.defaultRenderer(value);
    }
}

I put it just in the property "selModel" of the panel, after the "mode: 'MULTI',", but you can use checkcolumn if you need.

发布评论

评论列表(0)

  1. 暂无评论