如何使用Ext JS格式化JSON的返回字符串值?

时间:2018-03-12 07:22:32

标签: javascript json string extjs format

我在下方获取此JSON,需要格式化code字段'字符串值到其他一些文本。例如' TOTALPENDING'将呈现为"待定奖金"和' TOTALLEFT'到目前的奖金"。我怎样才能实现这个目标?

{
    "success": true,
    "msg": "OK",
    "count": 5,
    "data": [
        {
            "bookerid": 103083420,
            "code": "TOTALPENDING",
            "totalcount": 1
        },
        {
            "bookerid": 103083420,
            "code": "TOTALLEFT",
            "totalcount": 2
        },

通过ViewModel商店获取数据;

 stores: {
        bookStore: {
            model: 'MyApp.model.base.BookStatModel',
            autoLoad: true,
            session: true,
            proxy: {
                url: MyApp.Globals.getUrl() + '/bonustrans/stat/book',
                type: 'ajax',
                reader: {
                    type: 'json',
                    rootProperty: 'data'
                }
            }
        },

1 个答案:

答案 0 :(得分:2)

为此,您需要在convert内使用model配置。

在此 FIDDLE 中,我使用gridstoremodel创建了一个演示版。我希望这有助于/指导您实现您的要求。

CODE SNIPPET

Ext.application({
    name: 'Fiddle',

    launch: function () {

        Ext.define('Book', {
            extend: 'Ext.data.Model',
            fields: ['bookerid', {
                name: 'code',
                convert: function (v, rec) {
                    switch (v) {
                    case 'TOTALPENDING':
                        v = 'Pending Bonus';
                        break;
                    case 'TOTALLEFT':
                        v = 'Current Bonus';
                        break;
                    default:
                        v = '';
                        break;
                    }
                    return v;
                }
            }, 'totalcount'],
        });

        Ext.define('TestViewModel', {
            extend: 'Ext.app.ViewModel',
            alias: 'viewmodel.test',
            stores: {
                books: {
                    model: 'Book',
                    proxy: {
                        type: 'ajax',
                        url: 'book.json',
                        reader: {
                            type: 'json',
                            rootProperty: 'data',
                            keepRawData: true
                        }
                    },
                    autoLoad: true
                }
            }
        });

        Ext.create({
            xtype: 'container',
            layout: 'vbox',
            fullscreen: true,
            renderTo: Ext.getBody(),

            viewModel: {
                type: 'test'
            },

            items: [{
                xtype: 'container',
                userCls: 'infocardCount',
                margin: 10,
                bind: {
                    html: '<small>If value is 0 then we can use pipes and in that case you need to pass 0 inside of string like this <b> books.data.items.0.totalcount || "0"</b> </small><br><br> <b style="color: #3c3c3c;background: #ccc;padding: 10px;margin: 10px;">{books.data.items.0.totalcount || "0"}</b>'
                }
            }, {
                xtype: 'grid',
                flex: 1,
                width: '100%',
                title: 'Book Data',
                bind: {
                    store: '{books}'
                },
                columns: [{
                    text: 'BOOK ID',
                    flex: 1,
                    dataIndex: 'bookerid'
                }, {
                    text: 'CODE',
                    dataIndex: 'code',
                    flex: 1
                }, {
                    text: 'TOTAL',
                    flex: 1,
                    dataIndex: 'totalcount'
                }]
            }]
        });

    }
});

JSON文件

{
    "success": true,
    "msg": "OK",
    "count": 5,
    "data": [{
        "bookerid": 103083420,
        "code": "TOTALPENDING",
        "totalcount": 0
    }, {
        "bookerid": 103083421,
        "code": "TOTALLEFT",
        "totalcount": 15
    }, {
        "bookerid": 103083422,
        "code": "TOTALPENDING",
        "totalcount": 12
    }, {
        "bookerid": 103083423,
        "code": "TOTALLEFT",
        "totalcount": 10
    }, {
        "bookerid": 103083424,
        "code": "TOTALLEFT",
        "totalcount": 16
    }]
}