NestedList with Custom ListItem

213 Views Asked by At

I am trying to create an ExtJS 6.5.1 NestedList with a custom NestedListItem component. I can't find a working example on internet or in the ExtJS documentation.

Would anyone be able to show me a working example of a List or NestedList with a custom component item?

1

There are 1 best solutions below

2
On BEST ANSWER

You will need to use listConfig along with itemTpl to get custom XTemplate style in NestedList.

NestedList documentation says:

getItemTextTpl ( node ) : String

Override this method to provide custom template rendering of individual nodes. The template will receive all data within the Record and will also receive whether or not it is a leaf node.

But I found it does not work in ExtJS 6.x. It ends up throwing error as can not override getItemTextTpl.

Here is a working example with listConfig and itemTpl:

Ext.application({
    name: 'Fiddle',

    launch: function () {
        var data = {
            property: 'Groceries',
            items: [{
                property: 'Drinks',
                items: [{
                    property: 'Water',
                    items: [{
                        property: 'Sparkling',
                        leaf: true
                    }, {
                        property: 'Still',
                        leaf: true
                    }]
                }, {
                    property: 'Coffee',
                    leaf: true
                }, {
                    property: 'Espresso',
                    leaf: true
                }, {
                    property: 'Redbull',
                    leaf: true
                }, {
                    property: 'Coke',
                    leaf: true
                }, {
                    property: 'Diet Coke',
                    leaf: true
                }]
            }, {
                property: 'Fruit',
                items: [{
                    property: 'Bananas',
                    leaf: true
                }, {
                    property: 'Lemon',
                    leaf: true
                }]
            }, {
                property: 'Snacks',
                items: [{
                    property: 'Nuts',
                    leaf: true
                }, {
                    property: 'Pretzels',
                    leaf: true
                }, {
                    property: 'Wasabi Peas',
                    leaf: true
                }]
            }]
        };

        var store = Ext.create('Ext.data.TreeStore', {
            defaultRootProperty: 'items',
            root: data
        });

        Ext.Viewport.add({
            xtype: 'panel',
            layout: 'fit',
            title: 'Example',
            items: [{
                xtype: 'nestedlist',
                fullscreen: true,
                title: 'Groceries',
                displayField: 'property',
                store: store,
                listConfig: {
                    itemTpl: '<span<tpl if="leaf == true"> class="x-list-item-leaf"</tpl>>{property} --- {leaf} --- Yeah --- Custom Thing here from template</span>'
                }
            }]
        });
    }
});

Example Fiddle: https://fiddle.sencha.com/#view/editor&fiddle/29t3

EDIT:

Example Using Component instead of itemTpl:

Ext.application({
    name: 'Fiddle',

    launch: function () {
        var data = {
            property: 'Groceries',
            items: [{
                property: 'Drinks',
                items: [{
                    property: 'Water',
                    items: [{
                        property: 'Sparkling',
                        leaf: true
                    }, {
                        property: 'Still',
                        leaf: true
                    }]
                }, {
                    property: 'Coffee',
                    leaf: true
                }, {
                    property: 'Espresso',
                    leaf: true
                }, {
                    property: 'Redbull',
                    leaf: true
                }, {
                    property: 'Coke',
                    leaf: true
                }, {
                    property: 'Diet Coke',
                    leaf: true
                }]
            }, {
                property: 'Fruit',
                items: [{
                    property: 'Bananas',
                    leaf: true
                }, {
                    property: 'Lemon',
                    leaf: true
                }]
            }, {
                property: 'Snacks',
                items: [{
                    property: 'Nuts',
                    leaf: true
                }, {
                    property: 'Pretzels',
                    leaf: true
                }, {
                    property: 'Wasabi Peas',
                    leaf: true
                }]
            }]
        };

        var store = Ext.create('Ext.data.TreeStore', {
            defaultRootProperty: 'items',
            root: data
        });

        Ext.Viewport.add({
            xtype: 'panel',
            layout: 'fit',
            title: 'Example',
            items: [{
                xtype: 'nestedlist',
                fullscreen: true,
                title: 'Groceries',
                displayField: 'property1',
                store: store,
                listConfig: {
                    xtype: 'list',
                    itemConfig: {
                        xtype: 'panel',
                        layout: 'fit',
                        items: [{
                            xtype: 'textfield',
                            value: 'Custom thing here',
                        }]
                    }
                    //itemTpl: '<span<tpl if="leaf == true"> class="x-list-item-leaf"</tpl>>{property} --- {leaf} --- Yeah --- Custom Thing here from template</span>'
                }
            }]
        });
    }
});

Example Fiddle with Component: https://fiddle.sencha.com/#view/editor&fiddle/29u0

For mapping data in listItem you can use https://docs.sencha.com/extjs/6.2.0/modern/Ext.dataview.ListItem.html#cfg-dataMap

Here is example on using ListItem with dataMap: https://www.sencha.com/forum/showthread.php?183774-dataMap-to-DataItem-s-items