2

I am trying to build a search application using ExtJS. I have created dummy form to search for personal details. I have a php script connected to mysql DB. I am able to pass form parameters to php and able to get the return result in msg box. but I am not understanding how to pass it to store and display the same in grid in MVC. I have tried to pass the return data of php to store and then called Grid (List.js) in controller. still did not work. I have shown all the codes that i have used to do this.Another doubt which i have, is that essential to use proxy part of code (i.e url:app/scripts/Info.php) in both store and onSearchButtonClick function in controller? as I can directly pass the return values to store from onSearchButtonClick function, I hope it is not essential to connect php script in both places. However, it would be really nice experts clarify this.

Following is my store:

Ext.define('App.store.Info', {
    extend: 'Ext.data.Store',
    model: 'App.model.Info',
    alias: 'widget.infostore',
    pageSize : 50,
    autoLoad : false,
    remoteFilter: true,
    proxy   :{
            type    : 'ajax',
            url     : 'app/scripts/Info.php',
            reader  : {
                    type    : 'json',
                    root    : 'result',
                    successProperty : 'success'
            }
    },
 listeners: {
     load : function(store) {
         store.each(function(record) {
            record.commit();
        });
     }
 }
 });

My model looks perfect, simply to reduce somuch code I havent put here

Here is my grid:

Ext.define('App.view.info.List' ,{
extend: 'Ext.grid.Panel',
alias : 'widget.infolist',
store : 'Info',
initComponent: function(){
    this.columns = [
            {header:'PID',dataIndex:'pid'},
            {header:'Name',dataIndex:'name'},
            {header:'Address', dataIndex:'address'},
            {header:'Contact', dataIndex:'contact'}
    ];
    this.callParent(arguments);

}
});

This is what my php script returns:

{'success':true, 'result':{'pid':'100','name':'Suman','address':'Bangalore','contact':'suman@xyz.com'}}

Here is controller:

Ext.define('App.controller.Info', {
    extend: 'App.controller.Base',
    models: ['Info'],
    stores: ['Info'],
    views: [
            'info.Index',
            'info.List'
            ],
    refs: [{ref: 'info',selector: 'info'}],
    init: function(){
            console.log('Main controller init');
            this.control({
                    'button[action=search]':{
                            click: this.onSearchButtonClick
                    }
            });
    },
    onSearchButtonClick:function(){
            var form = Ext.getCmp('ppanel');
            if(form.getForm().isValid()){
                    Ext.Ajax.request({
                            waitMsg: 'Searching...', 
                            method: 'POST',
                            url: 'app/scripts/Info.php',
                            params: {
                                    searchData: Ext.encode(form.getValues())
                            },
                            scope:this,
                            success: this.onSearchSuccess,
                            failure: this.onSearchFailure
                            //Ext.MessageBox.alert("XXXXX","dat");
                    });
            }
    },
    onSearchSuccess: function(response){
            var gData = Ext.JSON.decode(response.responseText);
            //var grid = Ext.widget('infolist');  //not working -need help
            this.getInfoStore().load(gData);
            //Ext.getCmp().setActiveItem('infolist');  //not working-need help
            //this.getViewport().getLayout().setActiveItem('infolist'); //not working need help
            Ext.MessageBox.alert("XXXXX",response.responseText); //works
    },
    onSearchFailure: function(err){
            Ext.MessageBox.alert('Status', 'Error occured during searching...');
    }
});

I hope I have provided required information to understand the problem. Looking forward some sort of help.

4

1 に答える 1

1

問題は、ストアのインスタンスが 2 つ (グリッドに 1 つ、コントローラーに 1 つ) あることです。単一のインスタンス ストアが必要な場合 (お望みのように)、次の 2 つのオプションがあります。

  1. アプリケーションに追加する
  2. storeIdをストア定義に割り当てます。

(すでにそのストアをアプリケーションに追加している場合は、上記のテキストを無視してください)

または、さらに良いのは、次のように、ストアではなくグリッドで直接作業することです。

最初にrefコントローラーのビュー - >グリッドに a を追加します。

refs: [{ref: 'info',selector: 'info'},{selector:'infolist', ref:'infoGrid'}]

次に、onSearchSuccessハンドラーで、次を呼び出す代わりに、次を呼び出すthis.getInfoStore().load(gData);必要があります。this.getInfoGrid().getStore().loadData(gData);

ところで:this.getInfoStore().load(gData);データの配列またはレコードをロードすることはありません。そのためには、次を使用する必要があります。this.getInfoStore().loadData(gData);

これが正しい軌道に乗ることを願っています。

于 2013-01-07T18:17:16.283 に答える