John Walker
John Walker

Reputation: 1121

Get the store value with column name - EXTJS 4

Ext.define('GoogleMarkerModel', {
        extend: 'Ext.data.Model',
        fields: ['Locating','MainPower','Acc','PowerOff','Alarm','Speed','Direction','Latitude','Longitude','DateTime','MainID', 'DeviceID','IOState','OilState']
    });

    var MarkerStore = Ext.create('Ext.data.JsonStore', {
        model: 'GoogleMarkerModel',
        autoLoad: true,
        proxy: {
            type: 'ajax',
            url: 'get-googlemarker.php',
            baseParams: {  //here you can define params you want to be sent on each request from this store
                        mainid: 'value1'
                        },
            reader: {
                type: 'json',
                root: 'images'
            }
        }
    });

tree.on('checkchange', function(node){
        var data = node.data;
        Ext.MessageBox.show({
        title: 'Changed checkbox status',
        msg: 'MainID: ' + data.MainID + ' <br /> Checkbox status: ' + data.checked,
        icon: Ext.MessageBox.INFO
        });

        if (data.checked == true){
        MarkerStore.load({
                        params: {  //here you can define params on 'per request' basis
                                mainid: data.MainID,
                                }
                        })

        var options = {
        lat:MarkerStore[0].Latitude,
        lng:MarkerStore[0].Longitude,
        marker: {title:"Hello World!"},
        listeners: {
                     click: function(e){

                                         }
                    }     
        }     
        addDoctorLocation(options);           
        }       
    })

And this is an example to get the return value

http://localhost/GPS/examples/tabs/get-googlemarker.php?mainid=1  

return

[{"ID":"1808","Locating":"1","MainPower":"0","Acc":"1","PowerOff":"1","Alarm":"128","Speed":"0","Direction":"293","Latitude":"5.391788482666016","Longitude":"100.29693603515625","DateTime":"2013-02-19 15:44:36","MainID":"1","IOState":"0","OilState":"0"}]

This is the return value of get-googlemarker.php, I want to get the Latitude value save in lat variable and Longitude save in longt variable. Something like this:

Find the row where MainID is 1 and get the column name Latitude value.

UPDATE 2

var lati,longi;
        var record = MarkerStore.findRecord('MainID',data.MainID);
        if(record) {
            lati = record.get('Latitude'); 
        longi = record.get('Longitude'); 
        }

The record return is null, can't find the MainID = 1? Why? data.MainID value is 1.

UPDATE 3

Ext.define('GoogleMarkerModel', {
    extend: 'Ext.data.Model',
    idProperty:'MainID',
    fields: ['ID','Locating','MainPower','Acc','PowerOff','Alarm','Speed','Direction','Latitude','Longitude','DateTime','MainID','IOState','OilState']
});

var MarkerStore = Ext.create('Ext.data.JsonStore', {
    model: 'GoogleMarkerModel',
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: 'get-googlemarker.php',
        baseParams: {  //here you can define params you want to be sent on each request from this store
                    mainid: 'value1'
                    },
        reader: {
            type: 'json',
            idProperty : 'MainID',
        }

    }
});

I have added idProperty, but still can't work.

ERROR
enter image description here

LATEST CODE

tree.on('checkchange', function(node){
        var data = node.data;

        if (data.checked == true){
        MarkerStore.load({
                        params: {  //here you can define params on 'per request' basis
                                mainid: data.MainID,
                                }
                        })


        var lati,longi;
        var recordPos = MarkerStore.findBy(function(rec,id){
             return rec.data.MainID == data.MainID;
        }, this);
        if(recordPos > -1) {
           var record = MarkerStore.getAt(recordPos);
           lati = record.get('Latitude'); 
           longi = record.get('Longitude'); 
        }


        Ext.MessageBox.show({
        title: 'Changed checkbox status',
        msg: 'MainID: ' + data.MainID + ' <br /> Checkbox status: ' + data.checked + ' <br /> lati: ' + lati + ' <br />',
        icon: Ext.MessageBox.INFO
        });

        var options = {
        lat:lati,
        lng:longi,
        marker: {title:"Hello World!"},
        listeners: {
                     click: function(e){

                                         }
                    }     
        }     
        addDoctorLocation(options);           
        }       
    })

Still can't get the lati value. Any idea?
How to sure that MarkerStore having the data inside? I think MarkerStore are empty. How to check this?

UPDATE 4

var lati,longi;
        var recordPos = MarkerStore.findRecord('MainID', '1');
           lati = recordPos.get('Latitude'); 
           longi = recordPos.get('Longitude'); 

still can't work,recordPos is null. ERROR FOUND ON FIREBUG

TypeError: recordPos is null
[Break On This Error]   
lati = recordPos.get('Latitude');

i think is the problem JSON store save the data from PHP

UPDATE 5

i can sure that JSON store have data because i try to print all JSON store data with this code

MarkerStore.on('load', function(store, records) {
    for (var i = 0; i < records.length; i++) {
    console.log(records[i].get('Latitude'));
    };
});

and it is having data print on console

Upvotes: 4

Views: 17020

Answers (2)

Omid Shariati
Omid Shariati

Reputation: 1916

as sra said you can set idProperty

var MarkerStore = Ext.create('Ext.data.JsonStore', {
    model: 'GoogleMarkerModel',
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: 'get-googlemarker.php',
        baseParams: {
            mainid: 'value1'
        },
        reader: {
            type: 'json',
            root: 'images',
            idProperty: 'MainId'
        }
    }
});

then find record by MarkerStore.getById(123) or 'MarkerStore.findRecord('MainId', 123)'

Important: It seems extjs use === operator instead of == for compare two variable. then if a variable has int type and the other has string type then the comparsion may be false ( for example for "1" === 1). then you should use 'MarkerStore.findRecord('MainId', '123')' instead.

Upvotes: 1

sra
sra

Reputation: 23975

Some hints: JSON supports objects, arrays, string, boolean, float but you are using just strings? In addition a auto field configuration so all would stay strings. Next is that you have no idProperty defined for neither your model nor your reader. Based on your JSON string I guess this is ID. You should add the line

idProperty:'ID'

to the model and the reader and add the field ID to your model. If you don't want to use ID I guess it would be MainID so insert that. Now if you have a idProperty you can get the record by it's id by calling store.getById(1234).

You can also do custom searches like this

var lati,longi;
var recordPos = MarkerStore.findBy(function(rec,id){
     return rec.data.MainID == data.MainID;
}, this);
if(recordPos > -1) {
   var record = MarkerStore.getAt(recordPos);
   lati = record.get('Latitude'); 
   longi = record.get('Longitude'); 
}

If this returns nothing check if there is data in your store and if so supply more information how the store setups that record.

Upvotes: 5

Related Questions