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

这是一个获取返回值的例子

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

返回

[{"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"}]

这是get-googlemarker.php的返回值,我想把纬度值保存在lat变量中,将经度值保存在longt变量中。像这样的东西:

找到 MainID 为 1 的行,获取列名 Latitude 值。

更新 2

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

记录返回为空,找不到MainID = 1?为什么?data.MainID 值为 1。

更新 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',
        }

    }
});

我添加了 idProperty,但仍然无法工作。

错误
在此处输入图像描述

最新代码

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

仍然无法获得纬度值。任何的想法?
如何确定 MarkerStore 里面有数据?我认为 MarkerStore 是空的。如何检查这个?

更新 4

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

仍然无法工作,recordPos 为空。在 Firebug 上发现错误

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

我认为问题在于 JSON 存储从 PHP 中保存数据

更新 5

我可以确定 JSON 存储有数据,因为我尝试使用此代码打印所有 JSON 存储数据

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

它正在控制台上打印数据

4

2 回答 2

5

一些提示:JSON 支持对象、数组、字符串、布尔值、浮点数,但您只使用字符串?此外还有一个自动字段配置,因此所有内容都将保留字符串。接下来是idProperty您的模型和读者都没有定义。根据您的 JSON 字符串,我猜这是 ID。您应该添加该行

idProperty:'ID'

到模型和阅读器,并将该字段添加ID到您的模型中。如果您不想使用ID我想它会MainID这样插入。现在,如果你有一个 idProperty,你可以通过调用它的 id 来获取记录store.getById(1234)

您也可以像这样进行自定义搜索

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

如果这不返回任何内容,请检查您的商店中是否有数据,如果有,请提供更多信息,以便商店如何设置该记录。

于 2013-02-23T06:17:22.750 回答
1

如前所述,sra您可以设置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'
        }
    }
});

然后通过MarkerStore.getById(123)'MarkerStore.findRecord('MainId', 123)'查找记录

重要提示:似乎 extjs 使用 === 运算符而不是 == 来比较两个变量。那么如果一个变量具有 int 类型而另一个具有 string 类型,则比较可能是错误的(例如对于“1”=== 1)。那么你应该使用 'MarkerStore.findRecord('MainId', '123')' 代替。

于 2013-02-26T14:51:04.747 回答