【发布时间】:2014-02-08 15:56:29
【问题描述】:
我的 Json 数据如下。
{'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 数据数组中设置标签文本和背景颜色。例如,对于我的第一个标签(打开),需要将 #6699FF 设置为背景色,将“打开”设置为文本等。
编辑代码:
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')); 设置背景颜色时我的萤火虫控制台说 getEl() 未定义
我应该怎么做?请帮帮我?
干杯
【问题讨论】:
标签: javascript json extjs label