[英]Ext.data.Store getTotalCount alway returns 0
我从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"
})
任何帮助都感激不尽...
您的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") {
我看到您正在调用this.search()
,但在您的代码中找不到。 大概是对服务器的异步调用,该调用会在数据加载到存储之前立即返回。 如果我是对的,则需要在异步调用上设置回调,或在商店负载上设置事件监听器。
通过直接从JSON获取许多功能(而不是随后填充的商店)并添加json = Ext.util.JSON.decode(action.response.priv._object.responseText);
来解决该问题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);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.