Joh*_*ker 4 javascript json extjs store extjs4
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);
}
})
Run Code Online (Sandbox Code Playgroud)
这是获得返回值的示例
http://localhost/GPS/examples/tabs/get-googlemarker.php?mainid=1
Run Code Online (Sandbox Code Playgroud)
返回
[{"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"}]
Run Code Online (Sandbox Code Playgroud)
这是get-googlemarker.php的返回值,我想在lat变量中保存Latitude值,在longt变量中保存Longitude.像这样的东西:
找到MainID为1的行,并获取列名称纬度值.
var lati,longi;
var record = MarkerStore.findRecord('MainID',data.MainID);
if(record) {
lati = record.get('Latitude');
longi = record.get('Longitude');
}
Run Code Online (Sandbox Code Playgroud)
记录返回为null,找不到MainID = 1?为什么?data.MainID值为1.
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',
}
}
});
Run Code Online (Sandbox Code Playgroud)
我添加了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);
}
})
Run Code Online (Sandbox Code Playgroud)
仍然无法获得lati值.任何的想法?
如何确定MarkerStore里面有数据?我认为MarkerStore是空的.怎么检查这个?
var lati,longi;
var recordPos = MarkerStore.findRecord('MainID', '1');
lati = recordPos.get('Latitude');
longi = recordPos.get('Longitude');
Run Code Online (Sandbox Code Playgroud)
仍然无法正常工作,recordPos为null.在FIREBUG上发现错误
TypeError: recordPos is null
[Break On This Error]
lati = recordPos.get('Latitude');
Run Code Online (Sandbox Code Playgroud)
我认为是JSON商店保存PHP数据的问题
我可以肯定JSON存储有数据,因为我尝试使用此代码打印所有JSON存储数据
MarkerStore.on('load', function(store, records) {
for (var i = 0; i < records.length; i++) {
console.log(records[i].get('Latitude'));
};
});
Run Code Online (Sandbox Code Playgroud)
它在控制台上打印数据
一些提示:JSON支持对象,数组,字符串,布尔值,浮点数,但你只使用字符串?另外还有一个自动字段配置,所以所有都会保留字符串 接下来,您既没有idProperty为您的模型也没有为您的读者定义.根据你的JSON字符串,我猜这是ID.你应该添加这一行
idProperty:'ID'
Run Code Online (Sandbox Code Playgroud)
到模型和阅读器并将字段添加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');
}
Run Code Online (Sandbox Code Playgroud)
如果没有返回任何内容,请检查商店中是否有数据,如果有,请提供有关商店如何设置该记录的更多信息.
| 归档时间: |
|
| 查看次数: |
15887 次 |
| 最近记录: |