带自定义ListItem

时间:2017-11-20 12:26:22

标签: javascript extjs extjs6.5.1

我正在尝试使用自定义NestedListItem组件创建ExtJS 6.5.1 NestedList。我无法在互联网或ExtJS文档中找到一个有效的例子。

有人能用自定义组件项向我展示List或NestedList的工作示例吗?

1 个答案:

答案 0 :(得分:1)

您需要使用 listConfig 以及 itemTpl 在NestedList中获取自定义XTemplate样式。

NestedList文档说:

  

getItemTextTpl(node):String

     

重写此方法以提供单个节点的自定义模板呈现。模板将接收记录中的所有数据,并且还将接收它是否为叶节点。

但我发现它在ExtJS 6.x中不起作用。它最终会抛出错误,因为无法覆盖getItemTextTpl。

以下是listConfig和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>'
                }
            }]
        });
    }
});

示例小提琴: https://fiddle.sencha.com/#view/editor&fiddle/29t3

编辑:

使用Component代替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>'
                }
            }]
        });
    }
});

示例小提琴组件: https://fiddle.sencha.com/#view/editor&fiddle/29u0

对于listItem中的数据映射,您可以使用https://docs.sencha.com/extjs/6.2.0/modern/Ext.dataview.ListItem.html#cfg-dataMap

以下是将ListItem与dataMap一起使用的示例:https://www.sencha.com/forum/showthread.php?183774-dataMap-to-DataItem-s-items

相关问题