代码之家  ›  专栏  ›  技术社区  ›  harunB10

ExtJS基于存储项动态创建元素

  •  1
  • harunB10  · 技术社区  · 6 年前

    Ext.panel.Grid 使用 store 财产?

    例如。假设我有一个小组:

    Ext.create('Ext.panel.Panel', {
    
    layout: 'vbox',
    scrollable: true,
    items: [
        myItemsFunction()
    ]
    }));
    

    {
        "rows": [
        {
            "CreateDateTime": "2015-02-09 14:05:46",
            "Name": "de.txt",
            "id": "1"
        },
        {
            "CreateDateTime": "2015-02-09 14:05:46",
            "Name": "en.txt",
            "id": "2"
        },
        {
            "CreateDateTime": "2015-02-09 14:05:46",
            "Name": "it.txt",
            "id": "3"
        }]
    }
    

    我把它放在商店里:

    var store_documents = Ext.create('Ext.data.Store', {
        remoteSort: true,
        remoteFilter: true,
        proxy: {
            type: 'ajax',
            api: {
                read: baseURL + '&SubFunc=Documents&Action=view',
            },
            reader: { type: 'json', rootProperty: 'rows', totalProperty: 'total' }
        },
        autoLoad: true
    });
    

    现在,假设我想要这三个文件的下载按钮(数据元素.txt, 英文.txt, 信息技术.txt). 如何根据商店商品动态创建它们?我想把它放在这个里面 myItemsFunction() 并在面板项中显示(第一块代码示例)?

    或者一个商店只能和网格绑定?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Moataz Sanad    6 年前

    您可以使用ExtJs store而无需将其绑定到网格,因为外部数据存储有一个代理,当您调用存储.加载().

    所以你可以找到这个有效的例子 ExtJs Fiddle 基本思想是定义一个新的panel类,并使用initComponent()函数允许您根据从请求中检索到的数据创建动态项

    Ext.application({
        name: 'Fiddle',
        launch: function () {
            var storeData = {};
            let store = Ext.create('Ext.data.Store', {
                storeId: 'myStoreId',
                fields: ['id', 'name'],
                proxy: {
                    type: 'ajax',
                    url: 'app/data.json',
                    reader: {
                        type: 'json',
                        rootProperty: 'rows'
                    }
                }
            });
            store.load(function(){
                storeData = this.getData().items;
                Ext.create('Fiddle.MyPanel',{panelData:storeData});
            });
        }
    });
    

    应用程序/我的面板.js

    Ext.define('Fiddle.MyPanel', {
        extend: 'Ext.panel.Panel',
        renderTo: Ext.getBody(),
        title: 'Dynamic button creation',
        width: 600,
        height: 300,
        initComponent: function () {
            let panelItems = [];
            //Creating items dynamicly
            for (btn in this.panelData) {
                let me = this;
                panelItems.push(
                    Ext.create('Ext.button.Button', {
                        text:me.panelData[btn].data.Name
                    })
                );
            }
            this.items = panelItems;
            //must call this.callParent()
            this.callParent();
        }
    })
    

    应用程序/数据.json

    {
        "rows": [
        {
            "CreateDateTime": "2015-02-09 14:05:46",
            "Name": "de.txt",
            "id": "1"
        },
        {
            "CreateDateTime": "2015-02-09 14:05:46",
            "Name": "en.txt",
            "id": "2"
        },
        {
            "CreateDateTime": "2015-02-09 14:05:46",
            "Name": "it.txt",
            "id": "3"
        }]
    }
    
        2
  •  1
  •   Diego Victor de Jesus    6 年前

    为您的面板定义一个控制器;

    为afterrender创建事件函数;

    在里面,装载你的商店;

    将一个回调参数传递给你的商店的load函数,在那里你可以迭代加载的数据,创建按钮组件;

    this.getView().add(button) items 属性