javascript 使用列名获取存储值 - EXTJS 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15035246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the store value with column name - EXTJS 4
提问by John Walker
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:
这是get-googlemarker.php的返回值,我想获取保存在lat变量中的纬度值和保存在longt变量中的经度值。像这样的东西:
Find the row where MainID is 1 and get the column name Latitude value.
找到 MainID 为 1 的行并获取列名 Latitude 值。
UPDATE 2
更新 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.
记录返回空,找不到MainID = 1?为什么?data.MainID 值为 1。
UPDATE 3
更新 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.
我已经添加了 idProperty,但仍然无法工作。
ERROR
错误
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?
仍然无法获得 lati 值。任何的想法?
如何确定 MarkerStore 里面有数据?我认为 MarkerStore 是空的。如何检查这个?
UPDATE 4
更新 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
仍然无法工作,recordPos 为空。在 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
我认为是 JSON 存储从 PHP 保存数据的问题
UPDATE 5
更新 5
i can sure that JSON store have data because i try to print all JSON store data with this code
我可以确定 JSON 存储有数据,因为我尝试使用此代码打印所有 JSON 存储数据
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
它正在控制台上打印数据
采纳答案by Omid Shariati
as sra
said you can set idProperty
如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'
}
}
});
then find record by MarkerStore.getById(123)
or 'MarkerStore.findRecord('MainId', 123)'
然后通过MarkerStore.getById(123)
或 '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.
重要提示:似乎 extjs 使用 === 运算符而不是 == 来比较两个变量。然后,如果一个变量具有 int 类型而另一个变量具有 string 类型,则比较可能为假(例如对于“1”=== 1)。那么你应该使用 'MarkerStore.findRecord('MainId', '123')' 代替。
回答by sra
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
一些提示:JSON 支持对象、数组、字符串、布尔值、浮点数,但您只使用字符串?此外还有一个自动字段配置,因此所有内容都将保留字符串。接下来是您没有idProperty
为您的模型和读者定义。根据您的 JSON 字符串,我猜这是 ID。你应该添加行
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)
.
到模型和阅读器并将字段添加ID
到您的模型中。如果你不想使用ID
我猜它会MainID
插入那个。现在,如果您有一个 idProperty,您可以通过调用 id 来获取记录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.
如果这没有返回任何结果,请检查您的商店中是否有数据,如果有,请提供商店如何设置记录的更多信息。