Reputation: 3162
I have Json data as following.
{'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' } ]}
I am retrieving these data from postgresql database.
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'
},
});
And also I have extjs fieldset with eight labels.
{
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'
}
} ]
}
I want to set label text and background color from my json data array. As an example, for my first label (open), need to set #6699FF as background color and 'Open' as text and so on.
Edited Code:
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'));
});
}
}
This set all the label text correctly. But when I try to set background color using label.getEl().setStyle('background', record.get('color')); my firebug console says getEl() is undefined
How should I do that ? Please help me ?
Cheers
Upvotes: 1
Views: 6879
Reputation: 4355
You will need to add a listener to the store's load
event. Find the corresponding label and modify the properties. Here's an example
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'));
});
}
}
I created a simple fiddle to demonstrate a working change to the label's text and style attribute. You can find it here The example is mocked up in ExtJS 3.4.
Upvotes: 2