2013-05-22 72 views
0

我從GeoExt.data.FeatureStore/Ext.data.Store獲取使用PHP檢索的JSON填充的功能(地塊)的總數有問題。總數用作顯示錯誤消息(如果不返回任何功能)或包含搜索結果(地圖)的窗口的條件。我試圖使用getTotalCount,但無論檢索到的JSON是否包含特徵,都會返回0。 Ext JS版本3.4.1。代碼:Ext.data.Store getTotalCount總是返回0

var store = new GeoExt.data.FeatureStore({ 
    layer: parcels, 
    fields: [{ 
      name: "name" 
     } 
    ], 
    proxy: new GeoExt.data.ProtocolProxy({ 
     protocol: new OpenLayers.Protocol.HTTP({ 
      url: "php/DB2GeoJSON.php", 
      format: new OpenLayers.Format.GeoJSON() 
     }) 
    }) 
}); 

var formPanel = new GeoExt.form.FormPanel({ 
    frame: true, 
    title: "Search for parcels", 
    protocol: new OpenLayers.Protocol.HTTP({ 
     url: "php/DB2GeoJSON.php", 
     format: new OpenLayers.Format.GeoJSON() 
    }), 
    items: [{ 
      xtype: "textfield", 
      name: "name", 
      fieldLabel: "Parcel ID", 
      value: "" 
     } 
    ], 
    renderTo: "parcel_search", 
    listeners: { 
     actioncomplete: function (form, action) { 
      json = Ext.util.JSON.decode(action.response.priv._object.responseText); 
      features = action.response.features; 
      store.loadData(features); 
     } 
    } 
}); 

formPanel.addButton({ 
    text: "Search", 
    handler: function() { 
     this.search(); 
     var totalCount = store.getTotalCount(); 
     if (totalCount = "0") { 
      Ext.Msg.alert("Alert", "No such parcel ID!"); 
     } else { 
      new Ext.Window({ 
       title: "Search result" 
       height: 400, 
       width: 600, 
       modal: true, 
       layout: "border", 
       draggable: false, 
       resizable: false, 
       closeAction: "hide", 
       items: [{ 
         region: "center", 
         id: "mappanel", 
         xtype: "gx_mappanel", 
         map: map, 
         split: true 
        } 
       ] 
      }).show(); 
     } 
    }, 
    scope: formPanel, 
    renderTo: "parcel_search" 
}) 

任何幫助將非常感激......

回答

0

通過從JSON直接獲取特徵數量而不是之後填充的商店來解決問題,並添加json = Ext.util.JSON.decode(action.response.priv._object.responseText);以在搜索按鈕被按下時執行。還將搜索按鈕添加到表單的初始定義中,並將代碼的某些部分定義爲單獨的函數以提高可用性。

function mapDisplay() { 
    var totalCount = json.totalCount; 
    if (totalCount === 0) { 
     Ext.Msg.alert("Alert", "No such parcel ID!"); 
    } else { 
     new Ext.Window({ 
       title: "Search result", 
       height: 400, 
       width: 600, 
       modal: true, 
       layout: "border", 
       draggable: false, 
       resizable: false, 
       closeAction: "hide", 
       items: [{ 
         region: "center", 
         id: "mappanel", 
         xtype: "gx_mappanel", 
         map: map, 
         split: true 
        } 
       ] 
      }).show(); 
    } 
}; 

function searchParcel() { 
    formPanel.search({ 
      success: function (form, action) { 
       json = Ext.util.JSON.decode(action.response.priv._object.responseText); 
       mapDisplay(); 
      } 
     }); 
}; 

var formPanel = new GeoExt.form.FormPanel({ 
     frame: true, 
     title: "Search for parcels", 
     protocol: new OpenLayers.Protocol.HTTP({ 
       url: "php/parcel.php", 
       format: new OpenLayers.Format.GeoJSON() 
      }), 
     items: [{ 
       xtype: "textfield", 
       name: "name", 
       fieldLabel: "Parcel ID", 
       value: "" 
      } 
     ], 
     buttons: [{ 
       text: "Search", 
       handler: searchParcel 
      } 
     ], 
     renderTo: "parcel_search", 
     listeners: { 
      actioncomplete: function (form, action) { 
       features = action.response.features; 
       store.loadData(features); 
      } 
     } 
    }); 
0

你如果是錯誤的說法:

if (totalCount = "0") { 

應該是:

if (totalCount === 0) { 

a=b套等於b
a==b檢查是否是有點等於B(2=="2" - >真)
a==b檢查是否a等於B還a和b的類型(2==="2" - >假)我猜測


store.getTotalCount()將返回一個數字。如果不是你的if語句應該是:

if (totalCount === "0") { 
+0

感謝您的回覆!是的,它返回的數字,所以我改變了===,沒有引號(是的,我的JavaScript「知識」是非常有限的...),但它仍然總是返回0 ... – mitahito

0

我看你打電話this.search(),我不能在你的代碼中找到。據推測,這是對服務器的異步調用,服務器立即返回,並在數據加載到商店之前。如果我是正確的,則需要在異步調用或商店負載上的事件偵聽器上設置回調。

+0

是的,你是正確的關於這個。搜索()'。但是我怎麼實現它的回調函數呢?我知道有很多關於回調函數的例子,但是當我需要將這個想法適應於我的代碼時,我被卡住了...... – mitahito

+0

所以,也許發佈你的搜索方法,或者更好,創建一個小提琴 –

+0

管理解決問題,看我自己的答案。還刪除了中間註釋,其中包含對尚未運行的代碼的引用。 – mitahito