Search code examples
extjsextjs4extjs4.2

treecolumn renderer with icon - extjs 4.2.1


I'm building simple application using Ext Scheduler (http://www.bryntum.com/products/scheduler/). I'm trying to add custom renderer for treecolumn that will have icon of person in tree. enter image description here

I've created rendered for that column:

renderer: function (v, m, r) {
            if (r.get('leaf')) {
                return '<div style="float: left">' +
                    r.get('Name') +
                    '</div>'+
                    '<img data-qtip="<img src=iconUrl.png width=60px height=60px/>" '+
                    'src="iconUrl.png" '+
                    'width="20px" height="20px" style="float: right; margin-right: 5px"/>';
            }
            return v;
        }

This looks OK, but for long names I get unwanted look:

enter image description here

Looking thrue code I found some unwanted markup: enter image description here

What I need is almost identical renderer as in standard treecolumn, but I need to add extra icon on right of that column - employee image thumbnail.

enter image description here

I was searching over docs and I found out that there is private property called cellTpl (http://docs.sencha.com/extjs/4.2.1/source/Column2.html#Ext-tree-Column)

but I don't know how should I change this and should I change this at all because it is marked as private on top.

How should I change that renderer to get effect as on last image?

Here is some jsfiddle to play: http://jsfiddle.net/Misiu/gwFdr/


Solution

  • I ended extending Ext.tree.Column to instead of normal leaf icons it shows thumbnails.
    Here's my code:

    Ext.define('Grafik.component.tree.Column2', {
        extend: 'Ext.tree.Column',
        alias: 'widget.treecolumn2',
    
        thumbnailsServiceUrl:'',
    
        cellTpl: [
            '<tpl for="lines">',
                '<img src="{parent.blankUrl}" class="{parent.childCls} {parent.elbowCls}-img ',
                '{parent.elbowCls}-<tpl if=".">line<tpl else>empty</tpl>"/>',
            '</tpl>',
            '<img src="{blankUrl}" class="{childCls} {elbowCls}-img {elbowCls}',
                '<tpl if="isLast">-end</tpl><tpl if="expandable">-plus {expanderCls}</tpl>"/>',
            '<tpl if="checked !== null">',
                '<input type="button" role="checkbox" <tpl if="checked">aria-checked="true" </tpl>',
                    'class="{childCls} {checkboxCls}<tpl if="checked"> {checkboxCls}-checked</tpl>"/>',
            '</tpl>',
            '<img src="{loginUrl}" class="{childCls} {baseIconCls} ',
                '{baseIconCls}-<tpl if="leaf">leaf<tpl else>parent</tpl> {iconCls}"',
                '<tpl if="icon">style="background-image:url({icon})"</tpl>/>',
            '<tpl if="href">',
                '<a href="{href}" target="{hrefTarget}" class="{textCls} {childCls}">{value}</a>',
            '<tpl else>',
                '<span class="{textCls} {childCls}">{value}</span>',
            '</tpl>'
        ],
    
    
        treeRenderer: function (value, metaData, record, rowIdx, colIdx, store, view) {
            var me = this,
                cls = record.get('cls'),
                renderer = me.origRenderer,
                data = record.data,
                parent = record.parentNode,
                rootVisible = view.rootVisible,
                lines = [],
                parentData;
    
            if (cls) {
                metaData.tdCls += ' ' + cls;
            }
    
            while (parent && (rootVisible || parent.data.depth > 0)) {
                parentData = parent.data;
                lines[rootVisible ? parentData.depth : parentData.depth - 1] =
                        parentData.isLast ? 0 : 1;
                parent = parent.parentNode;
            }
    
            return me.getTpl('cellTpl').apply({
                record: record,
                baseIconCls: me.iconCls,
                iconCls: data.iconCls,
                icon: data.icon,
                checkboxCls: me.checkboxCls,
                checked: data.checked,
                elbowCls: me.elbowCls,
                expanderCls: me.expanderCls,
                textCls: me.textCls,
                leaf: data.leaf,
                expandable: record.isExpandable(),
                isLast: data.isLast,
                blankUrl: Ext.BLANK_IMAGE_URL,
                loginUrl: data.leaf ? me.thumbnailsServiceUrl + record.get('Login') : Ext.BLANK_IMAGE_URL,
                href: data.href,
                hrefTarget: data.hrefTarget,
                lines: lines,
                metaData: metaData,
                // subclasses or overrides can implement a getChildCls() method, which can
                // return an extra class to add to all of the cell's child elements (icon,
                // expander, elbow, checkbox).  This is used by the rtl override to add the
                // "x-rtl" class to these elements.
                childCls: me.getChildCls ? me.getChildCls() + ' ' : '',
                value: renderer ? renderer.apply(me.origScope, arguments) : value
            });
        }
    });
    

    And usage looks like this:

    {
        text: 'Users',
        dataIndex: 'Name',
        hideable: false,
        width: 260,
        xtype: 'treecolumn2',
        sortable: true,
        resizable: false,
        thumbnailsServiceUrl: window.appUrl + 'api/Thumbnails/'
    }
    

    Where api/Thumbnails is service that takes login and returns image.
    Simple solution, hope it helps someone :)