从GeoExt.data.FeatureStore / Ext.data.Store获取功能(地块)总数时遇到问题,该功能是使用PHP检索的JSON填充的。总数用作显示错误消息(如果未返回任何要素)或包含搜索结果(地图)的窗口的条件。我尝试使用getTotalCount,但无论检索到的JSON是否包含要素,它都会返回0。 Ext JS 3.4.1版。代码:
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 :(得分:0)
您的if语句错误:
if (totalCount = "0") {
应该是:
if (totalCount === 0) {
a=b
设置相等的b
a==b
检查a是否有点相等b(2=="2"
- > true)
a==b
检查a是否相等b还有a和b的类型(2==="2"
- > false)
我猜测store.getTotalCount()
会返回一个数字。如果不是,你的if语句应该是:
if (totalCount === "0") {
答案 1 :(得分:0)
我看到你正在调用我在你的代码中找不到的this.search()
。据推测,它是对服务器的异步调用,它立即返回,并且在将数据加载到存储之前。如果我是正确的,你需要在异步调用或商店负载上的事件监听器上设置回调。
答案 2 :(得分: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);
}
}
});