harunB10
harunB10

Reputation: 5207

ExtJS create elements dynamically based on store items

Is it possible to create some other elements except Ext.panel.Grid using store property?

For example. Lets say that I have a panel:

Ext.create('Ext.panel.Panel', {

layout: 'vbox',
scrollable: true,
items: [
    myItemsFunction()
]
}));

And from the backend I get this response:

{
    "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"
    }]
}

which I load in the store:

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
});

Now, lets say that I want to have download buttons for these three files (de.txt, en.txt, it.txt). How can I create them dynamically based on store items? I want to put it in this myItemsFunction() and show it in panel items (first block of code sample)?

Or a store is only possible to bind with Grid?

Upvotes: 1

Views: 1808

Answers (2)

Diego Victor de Jesus
Diego Victor de Jesus

Reputation: 3003

Define a controller for you panel;

create an event function for afterrender;

inside it, load your store;

pass a callback parameter to your store's load function, where you iterate over loaded data creating button components;

call this.getView().add(button) to add your buttons to your panel items attribute

Upvotes: 1

Moataz Sanad
Moataz Sanad

Reputation: 284

You can use ExtJs store without binding it to a grid, because Ext.data.Store has a proxy which act as ajax request when you call store.load().

So you can find this working example ExtJs Fiddle the basic idea is to define a new panel class and to use initComponent() function to allow you to create dynamic items based on the data retrieved from the request

app.js

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});
        });
    }
});

app/MyPanel.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();
    }
})

app/data.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"
    }]
}

Upvotes: 2

Related Questions