1

how can I pass parameters from the view to the store in extjs?

this is my store:

Ext.define('mycomponents.store.mystore', {
    extend: 'Ext.data.Store',
    alias: 'store.levels',
    model: 'mycomponents.model.mymodel',
    storeId: 'levels',
    restful: true,
    autoLoad: true,
    proxy: {
        type: 'ajax',
        headers: {
           'Accept': '*/*',
           'Cache-Control': 'no-cache',
           'Content-Type': 'application/json',
           'Authorization': localStorage.token
        },
        extraParams: {
           sort: 'levelid',
           'filter[active]': true,
           'filter[idparent]': 0
        },
        reader: {
            type: 'json',
            root: 'data',
            successProperty: 'success'
        },
        writer: {
            type: 'json',
            writeAllFields: true,
            encode: true,
            root: 'data'
        },
        listeners: {
            exception: function (proxy, response, operation) {
                var error = Ext.decode(response.responseText);
                Ext.MessageBox.show(
                    {
                        title: 'REMOTE EXCEPTION',
                        msg: error.message,
                        icon: Ext.MessageBox.ERROR,
                        buttons: Ext.Msg.OK
                    }
                );
            }
        },
        actionMethods: {
           read: 'GET'
        },
        api: {
           read: 'http://url...',
           create: 'http://url...',
           update: 'http://url...',
           destroy: 'http://url...'
        },
        autoSave: true
    },
    constructor: function (config) {
        this.callParent([config]);
    }
});

My view:

var store = Ext.create('mycomponents.store.mystore', {});

Ext.define('mycomponents.view.myview', {
    extend: 'Ext.container.Container',

    id: 'leves',
    alias: 'widget.levels',
    xtype: 'levels',

    items: [
        {
            xtype: 'combobox',
            ...
        }
    ]
    ...
}

I need from the view send 'filter[idparent]': 1, 'filter[idparent]': 2, or whatever on combobox change. How can I achieve this?

1 Answer 1

2

You need to attach on change listener in combobox and use method setExtraParam in store.

Example:

Ext.define('mycomponents.view.myview', {
    extend: 'Ext.container.Container',

    id: 'leves',
    alias: 'widget.levels',
    xtype: 'levels',

    items: [
        {
            xtype: 'combobox',
            listeners:{
                change: function (cmp, newV, oldV, opt) {
                    Ext.getStore('levels').getProxy().setExtraParam('filter[idparent]', newV);
                    Ext.getStore('levels').load();
                }
            }
            ...
        }
    ]
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

It didn't worked for me, in fact it shows me an error: What I did is this: store.proxy.extraParams = {'filter[idparent]': item.selection.data.idparent };... this way give me no errors, but it didn't reloaded the data either.
Look my edited answer, i just forgot about getProxy() after getStore(). If you need to load store after set the param just do method load()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.