EXT JS 4 使用模型关联渲染网格显示值

2024-01-09

Details

我有一个用于显示发票信息的网格。使用发票存储填充网格,发票存储使用发票模型,发票模型与 InvoiceStatus 模型具有“有一个”关联,主键为“id”,外键为“invoice_status_id”。

Problem

我不确定如何使发票网格的“状态”列的显示值使用插入的invoice_status_id 的关联模型“名称”。我知道我需要创建一个渲染器来执行此操作,但我仍然得到一个空值。 Invoice 和 InvoiceStatus 存储都填充了正确的值。

状态栏渲染

renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
    return record.getStatus().get('name');
},

发票商店

Ext.define('MyApp.store.Invoice', {
    extend: 'Ext.data.Store',

    requires: [
        'MyApp.model.InvoiceModel'
    ],

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            autoLoad: true,
            autoSync: true,
            model: 'MyApp.model.InvoiceModel',
            remoteSort: true,
            storeId: 'StoreInvoce',
            proxy: {
                type: 'rest',
                url: '/api/invoice',
                reader: {
                    type: 'json',
                    root: 'data'
                }
            }
        }, cfg)]);
    }
});

发票状态存储

Ext.define('MyApp.store.InvoiceStatus', {
    extend: 'Ext.data.Store',
    alias: 'store.InvoiceStatus',

    requires: [
        'MyApp.model.InvoiceStatus'
    ],

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            autoLoad: true,
            autoSync: true,
            model: 'MyApp.model.InvoiceStatus',
            remoteSort: true,
            storeId: 'MyJsonStore1',
            proxy: {
                type: 'rest',
                url: '/api/invoice_status',
                reader: {
                    type: 'json',
                    root: 'data'
                }
            }
        }, cfg)]);
    }
});

发票型号

Ext.define('MyApp.model.InvoiceModel', {
    extend: 'Ext.data.Model',
 
    uses: [
        'MyApp.model.InvoiceStatus'
    ],
 
    fields: [
        {
            mapping: 'id',
            name: 'id',
            type: 'int'
        },
        {
            mapping: 'client_id',
            name: 'client_id',
            type: 'int'
        },
        {
            mapping: 'client_name',
            name: 'client_name',
            type: 'string'
        },
        {
            dateFormat: 'Y-m-d',
            dateReadFormat: '',
            mapping: 'issue_date',
            name: 'issue_date',
            sortType: 'asDate',
            type: 'date'
        },
        {
            dateFormat: 'Y-m-d',
            mapping: 'due_date',
            name: 'due_date',
            sortType: 'asDate',
            type: 'date'
        },
        {
            mapping: 'payment_date',
            name: 'payment_date',
            sortType: 'asDate',
            type: 'date',
            useNull: true
        },
        {
            name: 'amount'
        },
        {
            mapping: 'invoice_status_id',
            name: 'invoice_status_id',
            sortType: 'asInt',
            type: 'int'
        }
    ],
 
    hasOne: {
        model: 'MyApp.model.InvoiceStatus',
        foreignKey: 'invoice_status_id',
        getterName: 'getStatus'
    }
});

发票状态模型

Ext.define('MyApp.model.InvoiceStatus', {
    extend: 'Ext.data.Model',

    fields: [
        {
            mapping: 'id',
            name: 'id',
            type: 'int'
        },
        {
            mapping: 'name',
            name: 'name',
            type: 'string'
        }
    ]
});

发票网格

Ext.define('MyApp.view.ApplicationViewport', {
    extend: 'Ext.container.Viewport',

    requires: [
        'MyApp.view.ClearTriggerField'
    ],

    layout: {
        type: 'border'
    },

    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            items: [
                {
                    xtype: 'header',
                    region: 'north',
                    height: 100,
                    items: [
                        {
                            xtype: 'image',
                            height: 100,
                            width: 250,
                            alt: 'Logo',
                            src: 'images/logo.gif',
                            title: 'Logo'
                        }
                    ]
                },
                {
                    xtype: 'container',
                    region: 'center',
                    layout: {
                        type: 'card'
                    },
                    items: [
                        {
                            xtype: 'container',
                            width: 150,
                            layout: {
                                type: 'border'
                            },
                            items: [
                                {
                                    xtype: 'gridpanel',
                                    collapseMode: 'mini',
                                    region: 'west',
                                    split: true,
                                    autoRender: false,
                                    maxWidth: 300,
                                    width: 250,
                                    bodyBorder: false,
                                    animCollapse: false,
                                    collapsed: false,
                                    collapsible: true,
                                    hideCollapseTool: true,
                                    overlapHeader: false,
                                    titleCollapse: true,
                                    allowDeselect: true,
                                    columnLines: false,
                                    forceFit: true,
                                    store: 'ClientDataStor',
                                    dockedItems: [
                                        {
                                            xtype: 'toolbar',
                                            dock: 'top',
                                            items: [
                                                {
                                                    xtype: 'cleartrigger'
                                                },
                                                {
                                                    xtype: 'tbfill'
                                                },
                                                {
                                                    xtype: 'button',
                                                    icon: '/images/settings.png'
                                                }
                                            ]
                                        }
                                    ],
                                    columns: [
                                        {
                                            xtype: 'templatecolumn',
                                            tpl: [
                                                '<img class="pull-left client-menu-image" src="/images/{type}.png"><div class="client-menu-name">{name}</div><div class="client-menu-type">{type}</div>'
                                            ],
                                            dataIndex: 'id',
                                            text: 'Client'
                                        }
                                    ],
                                    selModel: Ext.create('Ext.selection.RowModel', {

                                    }),
                                    plugins: [
                                        Ext.create('Ext.grid.plugin.BufferedRenderer', {

                                        })
                                    ]
                                },
                                {
                                    xtype: 'gridpanel',
                                    region: 'center',
                                    title: 'Invoices',
                                    titleCollapse: false,
                                    forceFit: true,
                                    store: 'Invoice',
                                    columns: [
                                        {
                                            xtype: 'numbercolumn',
                                            maxWidth: 120,
                                            minWidth: 50,
                                            dataIndex: 'id',
                                            groupable: false,
                                            lockable: true,
                                            text: 'ID',
                                            tooltip: 'Invoice ID',
                                            format: '0'
                                        },
                                        {
                                            xtype: 'numbercolumn',
                                            hidden: true,
                                            maxWidth: 120,
                                            minWidth: 50,
                                            dataIndex: 'client_id',
                                            groupable: true,
                                            text: 'Client ID',
                                            format: '0'
                                        },
                                        {
                                            xtype: 'gridcolumn',
                                            renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
                                                return record.getStatus().get('name');
                                            },
                                            maxWidth: 200,
                                            minWidth: 100,
                                            dataIndex: 'invoice_status_id',
                                            text: 'Status'
                                        },
                                        {
                                            xtype: 'datecolumn',
                                            maxWidth: 200,
                                            minWidth: 100,
                                            dataIndex: 'issue_date',
                                            text: 'Issue Date',
                                            format: 'd M Y'
                                        },
                                        {
                                            xtype: 'datecolumn',
                                            maxWidth: 200,
                                            minWidth: 100,
                                            dataIndex: 'due_date',
                                            text: 'Due Date',
                                            format: 'd M Y'
                                        },
                                        {
                                            xtype: 'datecolumn',
                                            maxWidth: 200,
                                            minWidth: 100,
                                            dataIndex: 'payment_date',
                                            text: 'Payment Date',
                                            format: 'd M Y'
                                        },
                                        {
                                            xtype: 'templatecolumn',
                                            summaryType: 'sum',
                                            maxWidth: 150,
                                            minWidth: 50,
                                            tpl: [
                                                '${amount}'
                                            ],
                                            defaultWidth: 80,
                                            dataIndex: 'amount',
                                            groupable: true,
                                            text: 'Amount'
                                        }
                                    ],
                                    features: [
                                        {
                                            ftype: 'grouping'
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        });

        me.callParent(arguments);
    }

});

我设法通过使用回调函数来进行关联查找,但发现自己从商店中进行查找要容易得多。

Step One

我将代理从 InvoiceStatus 存储移动到 InvoiceStatus 模型上,并使 InvoiceStatus 存储自动加载。

Step Two

我更改了 Status 列的呈现方法,以像这样从 InvoiceStatus 存储中查找显示名称。

renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
    var store = Ext.data.StoreManager.lookup('InvoiceStatus');
    return store.getById(value).get('name');
},

事实证明这是一个更简单的解决方案。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EXT JS 4 使用模型关联渲染网格显示值 的相关文章

  • 如何将存储值分配给隐藏字段

    我有一个模型和商店 我需要为商店中的隐藏字段分配一个值 Ext define loginUser extend Ext data Model fields name id mapping Provider id name name mapp
  • 如何使用 Sencha Touch 数据模型读取嵌套 JSON 结构?

    我整个晚上都在试图解决这个问题 但没有成功 我有一个 JSON 结构如下 来自另一个系统 所以我无法更改其结构 parents parent parentId 1 children child childId 1 ch
  • Extjs中始终显示Slider的提示文本

    在 Extjs 4 1 1a 中 如何保持tip text滑块始终可见 目前 tip text每当用户拖动滑块栏时就可见 我搜索了docs http docs sencha com ext js 4 0 api Ext slider Sin
  • 带有自定义按钮的 ExtJs 消息框

    如何使用自定义按钮显示 ExtJS 消息框 我想要一个带有自定义消息以及 取消 和 停用 按钮的消息框 请给一些想法 buttons text Cancel handler function Ext MessageBox hide subm
  • ExtJS 中的面包屑导航

    如何在 ExtJS 设计中显示面包屑功能 我正在使用带有边框布局的面板 我想在面板顶部设计碎屑功能 请寄给我一些样品 提前致谢 我想到了两种解决方案 使用面板标题 您将必须操纵面板的标题并在其上创建面包屑 您必须创建面包屑文本 并将其设置为
  • 在 Ext JS 构造函数中将项目推入数组会产生多个项目

    我有一个我定义的 Ext JS 类 在这个班级的constructor 我将一个文本字段推送到我的项目数组上 然后添加到我的测试字符串中 数组和字符串在类定义中都声明为空 但是 如果您尝试创建多个类实例 您将看到项目数组在每个实例之间共享
  • 如何使用 JSON 结果更新 Extjs 进度条?

    我在让进度条从 Json 结果中检索进度并根据每 10 秒的计时器检查更新进度条时遇到一些困难 我可以创建这样的 json 结果 success true progress 0 2 我想总体思路是 我需要一个间隔设置为 10 秒的任务 并让
  • ExtJS 4 用于选择所选值的组合框事件

    由于某种原因 我需要知道用户何时从组合框中选择了值 即使它已经被选择 仅当用户选择未选择的项目时 选择 事件才起作用 我在组合框或选择器的文档中没有看到任何类似 itemclick 的事件 有任何想法吗 ComboBox uses 绑定列表
  • 如何将背景颜色(或自定义 css 类)应用于网格中的列 - ExtJs 4?

    看起来应该很简单 但我根本无法完成此任务 我什至不需要动态完成它 例如 假设我有一个简单的 2 列网格设置 如下所示 columns header USER dataIndex firstName width 70 cls red head
  • 当我尝试使用 jasmine 进行测试时,应用程序文件夹未在 Ext.appliation 中加载

    我正在尝试在我的应用程序 Ext js 5 中实现茉莉花以进行单元测试 为此 我创建了应用程序测试文件 Ext require Ext app Application Ext Loader setConfig enabled true Ex
  • 突出显示 extjs4 折线图的一部分

    在 extjs 4 1 1a 中 下面的代码是折线图的工作示例 现在我需要在给定的最小和最大时间戳上突出显示该图表的一部分 xtype chart store ChartData height 100 width 100 legend po
  • 显示最后一条记录值,并非所有值都循环

    在下面的代码中 在 copyChild 和 insideModelRetrieved 函数中 在控制台 4 个功能上一一打印 但在下一个函数 onInnerModelRetrieved 4 次中打印最后一个功能值 我无法弄清楚为什么会发生这
  • 最小的 ExtJS 包是什么?

    有谁知道 Ext JS 2 2 所需的最少文件吗 我知道 ExtJS 网站有一个功能 build http extjs com products extjs build ExtJS ext js 的小版本 作为 ext all js 的替代
  • ExtJS 4:克隆商店

    我正在尝试找出如何克隆Ext data Store不保留旧的参考 让我用一些代码更好地解释一下 这是源商店 var source Ext create Ext data Store fields name age data name foo
  • 下载 ExtJs 的早期版本

    哪里可以下载 Extjs 的早期版本 具体来说 我想获得 extjs 4 1 1a 顺便说一句 a 是怎么回事 这与 extjs 4 1 1 不同吗 从这个仓库 https github com probonogeek extjs comm
  • 向 ExtJS GridPanel 添加过滤器标题行

    我知道可以在列标题下添加一个过滤器行 因为我已经看到它是用 Coolite 完成的 但由于我是 Sencha ExtJS 的新手 所以我很难找到如何直接在脚本中使用 ExtJS grid GridPanel 来完成此操作 请您用一些样品为我
  • 如何向 Ext.tree.TreePanel 添加复选框?

    我创建了这个简单的树 var children text My Layers children new Ext tree TreeNode text test1 leaf true new Ext tree TreeNode text te
  • ExtJS4 中的自动高度问题

    我们在 ExtJS3 中使用 autoHeight 来创建窗口 但现在在 ExtJS4 中它不起作用 除了这个还有其他选择吗 如果是 请告诉我 Read this http www sencha com forum showthread p
  • 如何在 extjs 4 中设置面板/窗口透明?

    如何将面板或窗口设置为透明 半透明 通过CSS还是设置html代码 extjs版本是4 0 7 谢谢 您可以使用 CSS 来完成此操作 将此配置添加到您的面板中 bodyStyle background transparent or bod
  • 如何在 Ext.form.TextField 中显示/隐藏密码

    您能告诉我如何在单击另一个按钮时显示 隐藏密码字段的输入文本吗 我尝试更改该文本字段的 inputType 属性 但它是在当时渲染的 因此没有影响 另一种方法是创建 2 个文本字段并使其可见 不可见 但我不喜欢这样做 因为它看起来像作弊 预

随机推荐