2012-06-22 148 views
1

我的網頁現在加載速度非常慢。基本上,我想預先填充我擁有的組合框。現在,它預先填充每一個,然後選擇默認值。這太慢了。用戶在頁面完全加載之前必須等待一分鐘左右。組合框加載速度太慢

我抓住值從服務器填充組合框。預先選擇組合框值的值通過響應變量接收到數組中。我如何加速整個過程?

代碼如下:

EXTJS

    xtype: "combo", 
        width: 250, 
        id: "nameId", 
        name: "comboName", 
        labelStyle: 'width:100px', 
        fieldLabel:"Name", 
        allowBlank: true, 
        displayField: "nameDisplay", 
        valueField: "nameValue", 
        url: "/thelink/toGetAllComboboxValues/fromPHPFile/", 



return { 
    init: function (args) { 

     formPanel.on({ 

      beforerender: function() { 

       Ext.Ajax.request({ 
        url: 'url/to/another/PHP/file/', 
        scope: this, 
        method: 'GET', 
        params: { 
         code_id: 'myUser', 
         number: '12345' 
        }, 

        success: function (response, opts) { 
         var result = Ext.decode(response.responseText); 
       Ext.getCmp('nameId').setValue(result.Name); 

        }, 

       }); 


      }, 

      scope: this 
     }); 

     //add form panel here 
    }, 

    getFormPanel: function() { 

     return formPanel; 
    }, 



    // load parameter values into the form 
    loadParams: function() { 

    }, 
    goHome: function() { 

    }, 


}; 
}(); 

PHP獲取COMBO框中的值

//makes call to server for each individual combo box values 

PHP獲取預選VALUES

//grabs all pre-selected values based on an ID and puts them in an array 
+0

準確地使用了什麼版本的Extjs(直到次要版本,例如:4.1.0)?另外,每家店鋪有多少條記錄?回答這些問題將有助於我們更詳細地瞭解您的情況。 – Reimius

回答

4

如果您的商店每個小於約300條記錄,並且您的網頁上沒有真正'那麼多'商店,您應該做的就是立即從PHP返回所有內容(或者更優選地加載所有商店與頁面加載本身,但它聽起來像你不能這樣做)。所以,不是您一個Ajax調用獲取ComboBox中所選項目的價值,認爲它更像是這樣的:

定義你的模型爲每個門店:

Ext.define("MyModel1", { 
    extend: "Ext.data.Model", 
    fields: [ 
     {name:"field_code", type:"string"}, 
     {name:"field_value",  type:"string"} 
    ] 
}); 

然後定義每個以非常簡單的方式注意到您的商店,請注意,爲了提高性能,我遺漏了您可能正在包含的讀者和代理,請使用Ext.data.ArrayStore,因爲它消除了記錄中每個項目具有命名屬性的必要性(這減少了從服務器返回的文本以及前端的解析時間):

var myStore = Ext.create("Ext.data.ArrayStore", { 
    model: "MyModel1", 
    data: [] 
}); 

現在在你已經定義的ajax請求中,添加來自php的所有數據的響應,作爲返回的json對象中的屬性,並將它們作爲數組數組來確保元素順序與您定義Ext的方式相匹配.data.Model。在我的例子返回的對象會是這個樣子(該數據陣列陣列):

{ 
    my_combobox_value1: "CAD", 
    my_combobox_data1: [ 
     ["CAD","Somthing for display of this record"], 
     ["AN","Another Entry] 
    ] 
} 

然後改變Ajax請求是這個樣子:

Ext.Ajax.request({ 
    url: 'url/to/another/PHP/file/', 
    scope: this, 
    method: 'GET', 
    params: { 
     code_id: 'myUser', 
     number: '12345' 
    }, 

    success: function (response, opts) { 
     var result = Ext.decode(response.responseText); 
     myStore.loadData(result.my_combobox_data1); 
     Ext.getCmp('nameId').setValue(result.my_combobox_value1); 
     ... and rinse and repeat for all stores, make sure you set the value for each combobox after the store has been loaded :) 
    }, 

}); 

這會給你的爲您提供最佳表現。如果這不起作用,您將不得不使用具有處理服務器端所有內容的代理的商店,並根據需要加載數據。

0

首先,我建議不要檢索每個組合的完整值列表,直到用戶需要它(當更改值或單擊觸發器時)。你可以通過給你的組合使用一個代理設置的商店到你的URL來做到這一點。

爲了節省初始的單值顯示,一個選項是將字符串值和id存儲在記錄中(或者可能不會將其存儲到每個記錄中,而是將其發送到客戶端無論如何)。然而,這並不是很好,並且在擺脫完整列表請求之後加載時間可能足夠快。如果不是,請嘗試查看是否可以請求所有單個值顯示在一個請求中。