Javascript ExtJs 3.4:从Json数据设置标签文本和背景颜色
我有如下Json数据Javascript ExtJs 3.4:从Json数据设置标签文本和背景颜色,javascript,json,extjs,label,Javascript,Json,Extjs,Label,我有如下Json数据 {'dropPickStatus':[{ 'description' : 'Open', 'status' : '1', 'color' : '#6699FF' } , { 'description' : 'Hold', 'status' : '2', 'color' : '#FF9900' } , { 'description' : 'Cancel', 'status' : '3', 'color' : '#FF0000' } , { 'description' : 'P
{'dropPickStatus':[{ 'description' : 'Open', 'status' : '1', 'color' : '#6699FF' } , { 'description' : 'Hold', 'status' : '2', 'color' : '#FF9900' } , { 'description' : 'Cancel', 'status' : '3', 'color' : '#FF0000' } , { 'description' : 'Parked', 'status' : '4', 'color' : '#C0C0C0' } , { 'description' : 'Arrived', 'status' : '5', 'color' : '#FFFF00' } , { 'description' : 'Started', 'status' : '6', 'color' : '#993300' } , { 'description' : 'Completed', 'status' : '7', 'color' : '#009900' } , { 'description' : 'Night Out', 'status' : '8', 'color' : '#FFFFFF' } ]}
我正在从postgresql数据库检索这些数据
var statusStore = new Ext.data.JsonStore({
fields : [ {
name : 'description'
}, {
name : 'status'
}, {
name : 'color'
} ],
root : 'dropPickStatus',
idProperty : 'status',
// autoDestroy : true,
autoLoad : true,
proxy : new Ext.data.HttpProxy({
url : "http://" + host + ":" + port + "/" + projectName + "/"
+ "DropPickStatus"
}),
reader : {
type : 'json',
root : 'dropPickStatus'
},
});
还有一个extjs字段集,有八个标签
{
xtype : 'fieldset',
layout : 'hbox',
border : false,
frame : false,
defaults : {
layout : 'hbox',
labelAlign : 'top'
},
items : [ {
xtype : 'label',
forId : 'idOpen',
text : 'Open',
width : 53,
height : 25,
// textAlign: 'center',
style : {
background : 'blue',
color : 'black'
}
}, {
xtype : 'label',
text : 'Hold',
forId : 'idHold',
width : 53,
height : 25,
style : {
background : 'orange',
color : 'black'
}
}, {
xtype : 'label',
text : 'Cancel',
forId : 'idCancel',
width : 53,
height : 25,
style : {
background : 'red',
color : 'black'
}
}, {
xtype : 'label',
text : 'Parked',
forId : 'idParked',
width : 53,
height : 25,
style : {
background : 'gray',
color : 'black'
}
}, {
xtype : 'label',
forId : 'idArrived',
text : 'Arrived',
width : 53,
height : 25,
style : {
background : 'yellow',
color : 'black'
}
}, {
xtype : 'label',
text : 'Started',
forId : 'idStarted',
width : 53,
height : 25,
style : {
background : 'purple',
color : 'black'
}
}, {
xtype : 'label',
text : 'Completed',
forId : 'idCompleted',
width : 53,
height : 25,
style : {
background : 'green',
color : 'black'
}
}, {
xtype : 'label',
text : 'Night Out',
forId : 'idNight Out',
width : 53,
height : 25,
style : {
background : 'Brown',
color : 'black'
}
} ]
}
我想从json数据数组中设置标签文本和背景颜色。例如,对于我的第一个标签(open),需要将#6699FF设置为背景色,“open”设置为文本等
编辑的代码:
listeners : {
load : function(me, records) {
Ext.each(records,
function(record) {
status = record.get('status');
if (status == '1') {
// lbOpen = Ext.getCmp('idOpen');
Ext.getCmp('idOpen').setText(record.get('description'));
alert(Ext.getCmp('idOpen').getEl());
} else if (status == '2') {
// lbHold = Ext.getCmp('idHold');
Ext.getCmp('idHold').setText(
record.get('description'));
} else if (status == '3') {
// lbCancel = Ext.getCmp('idCancel');
Ext.getCmp('idCancel').setText(
record.get('description'));
} else if (status == '4') {
// lbParked = Ext.getCmp('idParked');
Ext.getCmp('idParked').setText(
record.get('description'));
} else if (status == '5') {
// lbArrived = Ext.getCmp('idArrived');
Ext.getCmp('idArrived').setText(
record.get('description'));
} else if (status == '6') {
// lbStarted = Ext.getCmp('idStarted');
Ext.getCmp('idStarted').setText(
record.get('description'));
} else if (status == '7') {
// lbCompleted = Ext.getCmp('idCompleted');
Ext.getCmp('idCompleted').setText(
record.get('description'));
} else {
// lbNight = Ext.getCmp('idNightOut');
Ext.getCmp('idNightOut').setText(
record.get('description'));
}
// label.getEl().setStyle('background', record.get('color'));
});
}
}
这将正确设置所有标签文本。但是当我尝试使用label.getEl().setStyle('background',record.get('color'))设置背景色时;我的firebug控制台显示getEl()未定义
我该怎么做?请帮帮我
干杯您需要在商店的
加载
事件中添加一个侦听器。找到相应的标签并修改属性。这里有一个例子
listeners: {
load: function (me, records) {
Ext.each(records, function (record) {
//find the corresponding label with Ext.getCmp(id) or
//container.getComponent(itemId)
label.getEl().setStyle('background', record.get('color'));
label.setText(record.get('description'));
});
}
}
我创建了一个简单的提琴来演示对标签的文本和样式属性的有效更改。你可以找到这个例子是在extjs3.4中模拟的,我应该把这个监听器放在哪里?我应该如何得到每个标签?我应该把这个监听器放在mt存储中。但是现在的问题是,我应该如何得到每个标签?它说getEl()是未定义的?在我的例子中,我有办法选择注释掉的标签(有两个选项)您是否能够查询标签并获取存储加载中的对象?如果需要,我可以添加示例。