Javascript Ext.data.Store getTotalCount始终返回0
从GeoExt.data.FeatureStore/Ext.data.Store获取要素(地块)总数时遇到问题,该要素是通过使用PHP检索的JSON填充的。“总数”用作显示错误消息(如果未返回任何特征)或包含搜索结果(地图)的窗口的条件。我尝试使用getTotalCount,但无论检索到的JSON是否包含功能或是否包含Ext JS版本3.4.1,它都返回0。代码:Javascript Ext.data.Store getTotalCount始终返回0,javascript,extjs,extjs3,Javascript,Extjs,Extjs3,从GeoExt.data.FeatureStore/Ext.data.Store获取要素(地块)总数时遇到问题,该要素是通过使用PHP检索的JSON填充的。“总数”用作显示错误消息(如果未返回任何特征)或包含搜索结果(地图)的窗口的条件。我尝试使用getTotalCount,但无论检索到的JSON是否包含功能或是否包含Ext JS版本3.4.1,它都返回0。代码: var store = new GeoExt.data.FeatureStore({ layer: parcels,
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 (totalCount = "0") {
应该是:
if (totalCount === 0) {
a=b
设置相等的ba==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.\u 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);
}
}
});
谢谢回复!是的,它返回数字,所以我改为===并且没有引号(是的,我的JavaScript“知识”非常有限…),但它仍然总是返回0…是的,关于this.search()
,您是正确的。但是如何在它上面实现回调函数呢?我知道有很多关于回调函数的例子,但是当我需要将这个想法应用到我的代码中时,我被卡住了……所以,也许发布你的搜索方法,或者更好,创建一个fiddle来解决这个问题,看看我自己的答案。还删除了包含对尚未运行的代码的引用的中间注释。