0

我从使用 PHP 检索的 JSON 填充的 GeoExt.data.FeatureStore/Ext.data.Store 获取要素(地块)总数时遇到问题。总数用作显示错误消息(如果未返回任何特征)或包含搜索结果(地图)的窗口的条件。我尝试使用 getTotalCount 但无论检索到的 JSON 是否包含功能,它都会返回 0。分机 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"
})

任何帮助都感激不尽...

4

3 回答 3

0

json = Ext.util.JSON.decode(action.response.priv._object.responseText);通过直接从 JSON 而不是之后填充的商店获取功能数量并添加以在按下搜索按钮时执行来管理解决问题。还在表单的初始定义中添加了搜索按钮,并将代码的某些部分定义为单独的函数,以提高可用性。

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);
            }
        }
    });
于 2013-05-31T08:29:09.143 回答
0

我看到你在打电话this.search(),我在你的代码中找不到。推测它是对服务器的异步调用,它立即返回,并且在数据加载到存储之前。如果我是正确的,您将需要在异步调用上设置回调,或者在存储加载时设置事件侦听器。

于 2013-05-22T16:24:24.027 回答
0

您的 if 语句是错误的:

 if (totalCount = "0") {

应该:

if (totalCount === 0) {

a=b设置 a 相等 b
a==b检查 a 是否相等 b ( 2=="2"-> true)
a==b检查 a 是否相等 b 以及 a 和 b 的类型 ( 2==="2"-> false)


我猜这store.getTotalCount()将返回一个数字。如果不是,您的 if 语句应该是:

if (totalCount === "0") {
于 2013-05-22T13:08:19.323 回答