javascript - 带有自定义 ListItem 的 NestedList

标签 javascript extjs extjs6.5.1

我正在尝试使用自定义 NestedListItem 组件创建 ExtJS 6.5.1 NestedList。我在互联网上或 ExtJS 文档中找不到工作示例。

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

最佳答案

您需要使用 listConfigitemTpl 来在 NestedList 中获取自定义 XTemplate 样式。

NestedList 文档说:

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.

但我发现它在 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>'
                }
            }]
        });
    }
});

fiddle 示例: 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

关于javascript - 带有自定义 ListItem 的 NestedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47392004/

相关文章:

javascript - this.fireEvent 不工作 extjs3

javascript - ExtJS 4.2.1 : Using "split" in border layout in panel, 调整大小问题

extjs - 如何在ExtJS GridPanel中隐藏行?

javascript - Extjs - 如何从 subview Controller 调用父 View Controller 方法?

javascript - 尽管 robots.txt 配置正确,网站仍出现在 Google SERP 上

javascript - 何时向原型(prototype)添加成员?

javascript - Safari 浏览器上的强制“另存为”对话框

javascript - 如何将组合框链接到图像文件夹?