javascript - ExtJs 3.4 : Set label text and background color from Json data

标签 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' : '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')); 设置背景颜色时我的 Firebug 控制台显示 getEl() 未定义

我应该怎么做?请帮帮我?

干杯

最佳答案

您需要为商店的 load 事件添加一个监听器。找到对应的标签,修改属性。这是一个例子

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'));
            });
        }
    }

我创建了一个简单的 fiddle 来演示对标签文本和样式属性的有效更改。你可以找到它here该示例是在 ExtJS 3.4 中模拟的。

关于javascript - ExtJs 3.4 : Set label text and background color from Json data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21217761/

相关文章:

javascript - JSON解析多数据

javascript - 选项卡按钮功能

javascript - 如何阅读和理解 ExtJS 源代码?

javascript - Raphael javascript 中的偏移量在哪里

javascript - TestCafe - 在 useRole 完全完成之前执行的测试

java - 使用 directjngine 在 ext-js 中无限滚动

JavaScript JSON 调用只会加载一次

javascript - ExtJS 亚马逊 SDK

JavaScript split() join() 仅适用于精确短语

javascript - 使用 knockout.js 时应该如何定义方法?