2012-03-12 6 views
1

저는 ExtJS4를 사용하고 있고 양식을 사용하여 검색 기능을 구축하려고합니다. 양식이 표시되고 사용자가 4 가지 기준 중 하나를 입력하고 검색을 클릭하면 그리드가 작성되고 JSON 호출의 결과가 표시됩니다.ExtJS4 리로드 그리드 패널 버튼 핸들러

내가 얻으려고하는 것은 사용자가 다른 검색 조건을 입력하고 다시 검색을 클릭하고 새로운 결과를 표시하도록 그리드를 업데이트 할 수있게하는 것입니다. 첫 번째 시도에서 그리드는 Search를 클릭 할 때마다 렌더링되고 두 번째 시도에서는 단순히 이전 항목을 제거하지 않고 검색 결과를 그리드로 푸시합니다.

여기에 내 현재 설정입니다 : 내가 그리드를 새로 고침 및로드()를 호출하려고했습니다

Ext.define('job',{ 
     extend:'Ext.data.Model', 
     fields:['account_name', 'account_number','job_number','contract_year','program','type', 'version'] 
}) 

Ext.onReady(function(){ 

    var formPanel = Ext.widget('form', { 
     renderTo: 'search', 
     frame: true, 
     width: 350, 
     bodyPadding: 5, 
     bodyBorder: false, 
     title: 'Search', 

     defaults: { 
      anchor: '100%' 
     }, 
     { 
      xtype: 'combo', 
      name: 'jobyear', 
      fieldLabel: 'Job Year', 
      store: 
       new Ext.data.SimpleStore({ 
        fields: ['year'], 
        data: [ 
         ['2008'],['2009'],['2010'],['2011'],['2012'] 
        ] //end of data 
      }), 
      displayField: 'year', 
      typeAhead: true, 
      emptyText: 'Select a year' 
     }], //end of items 

     dockedItems: [{ 
      xtype: 'container', 
      dock: 'bottom', 
      layout: { 
       type: 'hbox', 
       align: 'middle' 
      }, 
      padding: '10 10 5', 

      items: [{ 
       xtype: 'component', 
       id: 'formErrorState', 
       baseCls: 'form-error-state', 
       flex: 1 
      }, { 
       xtype: 'button', 
       formBind: true, 
       id: 'search', 
       disabled: true, 
       text: 'Search', 
       width: 140, 
       height: 35, 
       handler: function() { 
        var columns = [ 
        {xtype: 'rownumberer',sortable: true}, 
        {text: 'School Name', sortable:true,dataIndex:'account_name'}, 
        {text: 'Acct Number', sortable:true,dataIndex:'account_number'}, 
        {text: 'Job Number',sortable:true,dataIndex:'job_number'}, 
        {text: 'Version',sortable:true,dataIndex:'version'}, 
        {text: 'Contract Year',sortable:true,dataIndex:'contract_year'}, 
        {text: 'Program', sortable:true,dataIndex:'program'}, 
        {text: 'Job Type', sortable:true,dataIndex:'type'} 
        ]; // end columns 

        var userGrid = new Ext.grid.Panel({ 
        id: 'FOO', 
        multiSelect:true, 
        store: store, 
        columns: columns, 
        stripeRows: true, 
        title: 'Results', 
        renderTo: Ext.get('results'), 
        dockedItems: [{ 
         xtype: 'pagingtoolbar', 
         store: store, 
         dock: 'bottom', 
         displayInfo: true 
        }], 
        }) 
        var form = this.up('form').getForm(); 
        var store = new Ext.data.Store({ 
         model: 'job', 
         pageSize: 10, 
         autoLoad: true, 
         remoteSort:true, 
         proxy: { 
          actionMethods: { 
           read: 'POST' 
          }, 
          type: 'ajax', 
          fields: ['job_number', 'account_name', 'account_number', 'contract_year','program','type','version'], 
          url: '/search?'+ form.getValues(true), 
          reader:{ 
           type: 'json', 
           root: 'results', 
           totalProperty: 'totalCount'}, 
         }, //end proxy 
         sorters:[{ 
          property:'version', 
          direction:'ASC' 
         }] 
        }).on('load', function(){ 
         userGrid.reconfigure(this); // ??? 
         }); 
      } // end button handler 
     }] //end items 
    }] // end dockeditems 

}); 
}); 

하지만 난 오른쪽 조합에 아직 명중했다고 생각하지 않습니다. 그리드의 내용을/ajax에 대한 최신 Ajax 호출의 내용으로 교체하고 싶습니다.

+0

이 ExtJs3입니까? – sha

+0

예 ExtJS4입니다. –

답변

2

검색 단추를 클릭 할 때마다 새 패널과 저장소를 만들어야하므로 단추 처리기 밖으로 이동해야합니다. 사용자가 검색 버튼을 누를 때 그리드 저장소에서 load ({params : {search : 'whatever'}})를 호출하면 그리드에 자동으로 채워지는 새 데이터를 얻게됩니다. 그리드를 재구성하거나 다른 작업을 수행 할 필요가 없습니다. 기본적으로 그리드는 저장소에 바인딩되며 저장소 데이터가 변경되면 그리드 뒤의보기가 자동으로 새로 고침됩니다.

0

DmitryB의 조언에 따라 해결했습니다. 나는이 코드를 상속 받았고 약간의 마사지 후에 의도 한대로 작동하도록했습니다. 마지막 해결책은 다음과 같습니다. 버튼의 핸들러 함수에서 반드시 store.load()를 호출 할 때 올바르게 표시되도록 상점에 정의 된 프록시의 URL을 업데이트하고 업데이트해야합니다.

Ext.require([ 
    'Ext.form.*', 
    'Ext.grid.*', 
    'Ext.data.*', 
    'Ext.dd.*' 
    //'extjs.SlidingPager' 
]); 

    /*Setup Data Model*/ 
    Ext.define('job',{ 
     extend:'Ext.data.Model', 
     fields:['account_name', 'account_number','job_number','contract_year','program','type', 'version'] 
}) 


Ext.onReady(function(){ 

    var formPanel = new Ext.widget('form', { 
     renderTo: 'search', 
     frame: true, 
     width: 350, 
     bodyPadding: 5, 
     bodyBorder: false, 
     title: 'Search', 

     defaults: { 
      anchor: '100%' 
     }, 

     fieldDefaults: { 
      labelAlign: 'left', 
      msgTarget: 'none' 
     }, 

     items: [{ 
      xtype: 'textfield', 
      name: 'jobnumber', 
      fieldLabel: 'Job Number', 
      allowBlank: true, 
      minLength: 7, 
      maxLength: 7 
     }, { 
      xtype: 'textfield', 
      name: 'accountnumber', 
      fieldLabel: 'Account Number', 
      allowBlank: true, 
      minLength: 6, 
      maxLength: 7 
     }, { 
      xtype: 'textfield', 
      name: 'customername', 
      fieldLabel: 'Customer Name', 
      allowBlank: true, 
     }, { 
      xtype: 'combo', 
      name: 'jobyear', 
      fieldLabel: 'Job Year', 
      store: 
       new Ext.data.SimpleStore({ 
        fields: ['year'], 
        data: [ 
         ['2008'],['2009'],['2010'],['2011'],['2012'] 
        ] //end of data 
      }), 
      displayField: 'year', 
      typeAhead: true, 
      emptyText: 'Select a year' 
     }], //end of items 

     dockedItems: [{ 
      xtype: 'container', 
      dock: 'bottom', 
      layout: { 
       type: 'hbox', 
       align: 'middle' 
      }, 
      padding: '10 10 5', 

      items: [{ 
       xtype: 'button', 
       formBind: true, 
       id: 'search', 
       disabled: true, 
       text: 'Search', 
       width: 140, 
       height: 35, 
       handler: function() { 
        store.proxy.url = '/search?' + formPanel.getForm().getValues(true) 
        store.load(); 
       } // end button handler 
      }] //end items 
     }] // end dockeditems 
    }); 

    var store = new Ext.data.Store({ 
     model:'job', 
     pageSize:10, 
     autoLoad: false, 
     remoteSort:true, 
     proxy:{ 
      type:'ajax', 
      fields:['job_number', 'account_name', 'account_number', 'contract_year','program','type','version'], 
      url: '', 
      reader:{ 
       totalProperty:'totalCount', 
       type: 'json', 
       root: 'results' 
      }, 
      actionMethods: 'POST' 
     }, 
     sorters:[{ 
      property:'version', 
      direction:'ASC' 
     }] 
    }); 

    var columns = [ 
     {xtype: 'rownumberer',sortable: true}, 
     {text: 'School Name', sortable:true,dataIndex:'account_name'}, 
     {text: 'Acct Number', sortable:true,dataIndex:'account_number'}, 
     {text: 'Job Number',sortable:true,dataIndex:'job_number'}, 
     {text: 'Version',sortable:true,dataIndex:'version'}, 
     {text: 'Contract Year',sortable:true,dataIndex:'contract_year'}, 
     {text: 'Program', sortable:true,dataIndex:'program'}, 
     {text: 'Job Type', sortable:true,dataIndex:'type'} 
    ]; // end columns 

    var userGrid = new Ext.grid.Panel({ 
     id: 'userGrid', 
     multiSelect: false, 
     store: store, 
     columns: columns, 
     stripeRows: true, 
     title: 'Results', 
     renderTo: 'results', 
     dockedItems: [{ 
      xtype: 'pagingtoolbar', 
      store: store, 
      dock: 'bottom', 
      displayInfo: true 
     }], 
    }) 
}); 
관련 문제