Search code examples
servletsextjs4

ExtJS reloading store on button click event with different parameter


I am new to ExtJs and I am using ExtJs4.

Now As shown in below image, There is one textfield named keywords, What I want to do is When I click on the button it will pass data of textfield to servlet and display resulted record in grid.

enter image description here

Now I have no idea how to do this. I am receiving JSON data response from servlet but don't know how to reload the store and refresh the grid.

Below is code for my store and grid.

Ext.define("Post", {
    extend: 'Ext.data.Model',
    proxy: {
        type: 'ajax',
        url: '/ezdi/searchServlet',
        method: 'POST',
        reader: {
            type: 'json',
            root: 'rows'
            //,totalProperty: 'totalCount'
        }
    },

    fields: [{
        name: 'docid',
        mapping: 'docid'
    }, {
        name: 'mrn',
        mapping: 'mrn'
    }, {
        name: 'fname',
        mapping: 'fname'
    }]
});

var gridDataStore = Ext.create('Ext.data.Store', {
    model: 'Post'
});

// Data store for grid end

Ext.define('Ezdi.Grid', {
    extend: 'Ext.grid.GridPanel',
    alias: 'widget.ezdigrid',
    initComponent: function() {
        var config = {
            store: gridDataStore,
            columns: [{
                header: "DocID",
                width: 100,
                sortable: true,
                dataIndex: 'docid'
            }, {
                header: "MRN",
                width: 100,
                sortable: true,
                dataIndex: 'mrn'
            }, {
                header: "FirstName",
                width: 100,
                sortable: true,
                dataIndex: 'fname'
            }],
            viewConfig: {
                forceFit: false,
                autoLoad: false
            },
            loadMask: true
        };
    }
});

Solution

  • You could use:

    {
        xtype: 'button',
        text: 'Search',
        handler: function() {
            store.clearFilter(); //clear previous search value
            var searchValue = Ext.getCmp("textFieldId").getValue(); //get new value 
            store.load().filter('jsonGridFielName', searchValue); //load filtered data
        }
    }
    

    And for for multiple textfield search:

    //FILTERS
    var searchValue1 = Ext.getCmp("textFieldId1").getValue(); //value1
    var searchValue2 = Ext.getCmp("textFieldId2").getValue(); //value2
    var noValue = "0000xxxx"; //no Value, for empty field, use value that you are sure it is not going to be searched!!!
    var clear = store.clearFilter(); //shortcut
    
    if (!searchValue1 && !searchValue2) {
        clear;
        store.load().filter("jsonGridFielName1", noValue);
    } else if (searchValue1) {
        clear;
        store.load().filter('jsonGridFielName1', searchValue1);
        //...else if(searchValue n...)...
    } else {
        clear;
        store.load().filter('jsonGridFielName2', searchValue2);
    }