javascript - 在 ext 网格中显示值,其中值对应于另一个表/模型中的字符串

标签 javascript extjs extjs4

我正在通过构建一个非常简单的“scrum”开发跟踪应用程序来自学 ExtJS。我目前将“Backlog”显示为显示卡片属性(用户故事)的网格面板。

Card.js(卡片模型)

Ext.define('AM.model.Card', {
    extend: 'Ext.data.Model',
    fields: [
                'id',
                'name',
                'priority_id',
                ...
            ]
});

Priority.js(优先模型)

Ext.define('AM.model.Priority', {
    extend: 'Ext.data.Model',
    fields: [
                'id',
                'name',
                'short_name'
            ]
});

所以卡的数据看起来像这样:

backlogcards.json(数据)

{
    success: true,
    backlogcards: [
        {
            id: 1, 
            name: 'ONEs',
            priority_id: 2,
            ...
        },
        {
            id: 2, 
            name: 'TWOs',
            priority_id: 3,
            ...
        }
    ]
}

优先级是这样的:

priorities.json(数据)

{
    success: true,
    priorities: [
        {
            id              : 1, 
            name            : "High",
            short_name      : "H"
        },
        {
            id              : 2, 
            name            : "Medium",
            short_name      : "M"
        },
            {
            id              : 3, 
            name            : "Low",
            short_name      : "L"
        }
    ]
}

理想情况下,我想做的是让网格面板显示相应 priority_id 的短名称。单击项目以进行内联编辑时,将显示一个组合框,其中显示名称属性。我已经完成了一半。

BacklogList.js( View )

Ext.define('AM.view.card.BacklogList', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.backlogcardlist',

    title: 'Backlog',
    store: 'BacklogCards',

    selType: 'cellmodel',
    plugins: [
        Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        })
    ],

    columns: [
        { header: 'ID', dataIndex: 'id' },
        { header: 'Name', dataIndex: 'name', field: 'textfield' },
        {
            header: 'Priority',
            dataIndex: 'priority_id',
            width: 130,
            field: {
                xtype: 'combobox',
                typeAhead: true,
                store: 'Priorities',
                displayField: 'name',
                valueField: 'id',
                listClass: 'x-combo-list-small'
            }
        }
    ]

});

所以我知道“dataIndex”属性是我需要修改以更改显示的内容,但我不确定如何将这两个存储联系在一起。

Row Editing

正如您在上面看到的,优先级显示为数字而不是短名称。

在这种情况下我需要使用关联吗? (我只知道其中一个)Sencha Docs

谢谢!

编辑 1:哦,我意识到我可以“硬编码”一个渲染器属性来执行此更改,但我想避免这种情况,而是使用优先级存储中的值。

    renderer: function(value){
        if (value==3)
        {
            return "L";
        }
        else if (value==2)
        {
            return "M";
        }
        else
        {
            return "H";
        }
    },

Evan 的 EDIT2: 优先商店

Ext.define('AM.store.Priorities', {
    extend: 'Ext.data.Store',
    model: 'AM.model.Priority',
    autoLoad: true,

    proxy: {
        type: 'ajax',
        api: {
            read: 'app/data/priorities.json',
            update: 'app/data/updateUsers.json'
        },
        reader: {
            type: 'json',
            root: 'priorities',
            successProperty: 'success'
        }
    }
});

store.each 指的是这家店吧?如果是这样,我该如何对其执行每个操作?

我尝试将声明行更改为:

var test = Ext.define('AM.store.Priorities', {

然后尝试将您的代码更改为 test.each 但未成功。

再次感谢!

最佳答案

您需要使用渲染器,但是没有什么可以阻止您循环访问优先级存储中的值并进行检查,例如:

renderer: function(value) {
    var display = '';
    store.each(function(rec){
        if (rec.get('id') === value) {
            display = rec.get('name');
            return false;
        }
    });
    return display;
}

关于javascript - 在 ext 网格中显示值,其中值对应于另一个表/模型中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554214/

相关文章:

javascript - Ng-map-show all marker on map-如何计算缩放级别和中心位置

javascript - 如何在 Chart.js 中绘制线性回归线

button - 如何创建 stackoverflow 类型的按钮?

ExtJS 4 检查类是否存在

javascript - Highcharts 如何在 PlotLines 中添加链接?

javascript - 单击滑动面板以外的区域时,如何关闭滑动面板?

extjs - 新记录会获得自动生成的 id 值。如何像 ExtJs 4 中那样获取 0 作为新 id?

android - 如何使用 cordova 和 Sencha 构建一个正常的准备好存储的应用程序

css - Sencha 应用程序构建和图表 CSS

javascript - 在 extjs4 中获取加载的存储