Search code examples
javascriptextjsextjs3

Ext.data.Store getTotalCount alway returns 0


I have a problem getting total number of features (land parcels) from GeoExt.data.FeatureStore/Ext.data.Store which is populated from JSON retrieved using PHP. Total number is used as a condition to display either an error message (if no features are returned) or window containing search result (map). I tried to use getTotalCount but it returns 0 no matter if retrieved JSON contains features or no. Ext JS version 3.4.1. Code:

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"
})

Any help will be much appreciated...


Solution

  • Managed to solve the problem by getting number of features directly from JSON not the store populated afterwards and adding json = Ext.util.JSON.decode(action.response.priv._object.responseText); to be executed when search button is pressed. Also added search button to the initial definition of the form and defined some parts of the code as separate functions for better usability.

    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);
                }
            }
        });