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 使用模型关联渲染网格显示值 的相关文章

  • 如何从 ExtJS 数据存储中获取脏记录?

    除了迭代存储中的记录并检查脏标志之外 还有更干净的方法吗 EDIT 顺便说一句 我正在使用 ExtJS4 这是返回的数据的片段 注意有一个dirty true与modified对象集 实际上是旧数据和data对象包含新数据 data Ext
  • 从 Ext.data.Store 访问 http 状态代码

    我有一个 http API 令人震惊的新技术 它对设置不同响应状态的不同错误做出反应 问题是 当使用 Ext data Store 和一些 XMLHttpRequest inside 代理时 处理这种状态的最佳方法是什么 据我所知 加载 事
  • 如何保存 Extjs4 图表图像以在 pdf 报告中打印?

    我的 extjs4application 仪表板中有几个图表 我想使用这些图表的图像生成 pdf 报告 为此我使用 iTextSharp 有没有办法从图表中获取图像 以便将它们包含在我的报告中 对我来说理想的是像这样与 itextsharp
  • 在文本字段中输入文本时禁用面板水平滚动

    当有人在文本字段中输入文本时 我想禁用面板的水平滚动 第一个问题 目前的问题是 当您在任何文本字段中输入文本并按键盘上的右箭头键 keyCode 39 时 面板水平方向也会向正确的方向移动 我想在文本字段中输入文本时禁用它 第二个问题仅当单
  • 当另一个下拉列表中的值发生变化时加载一个下拉列表的存储

    我有 2 个下拉菜单 根据在一个下拉列表中选择的值 我需要使用 JSON 进行 AJAX 调用来检索值并在其他下拉列表中可用 这需要在 EXTJS 中完成 我尝试了以下代码 FUNCTION NAME Field on select fun
  • extjs 3.3:浮动面板

    我正在尝试在其他预先创建的面板上创建一个浮动面板 我尝试遵循简单的代码 但失败了 var testPanel new Ext Panel id testP width 50 height 100 floating true title Te
  • 如何使 extjs 手风琴垂直滚动

    在这里摆弄 https fiddle sencha com fiddle 5gv 如果手风琴中有很多面板 它们就会在垂直方向上相互碰撞 并且无法扩展 我想让手风琴的总高度等于标题的高度加上一个面板主体的高度 展开 然后父面板应该只有一个滚动
  • Extjs 4(下面有3.4的代码)下载从post请求返回的文件

    我看到了与此略有相关的问题 但没有一个能回答我的问题 我设置了 Ext Ajax request 如下 var paramsStringVar param1 1 param2 two param3 something param4 etc
  • Extjs组合框:隐藏下拉列表中的选定值

    我正在使用 ExtJS 4 并寻找一种可以从组合的下拉列表中隐藏当前选定值的方法 因此 代替这个 当前在组合框中选择 阿拉斯加 我希望值列表如下所示 就我而言 组合框是不可编辑 即您不能输入任意值 我认为两次显示所选值没有多大意义 一次在输
  • 将 Flex 值动态添加到 extjs 中的控制器

    我在 视图 中给出了一些项目 容器 布局为hbox 现在我想给flex通过 控制器 为每个项目赋予值 我怎样才能做到这一点 我已经浏览了文档 但找不到任何类似的方法setFlex EDIT Ext apply Ext getCmp IdHe
  • Extjs 5,数据模型关联和加载嵌套数据

    试图让这项工作 我想在两个对象模型上加载嵌套数据 Ext application name MyApp launch function Ext define MyApp model Address extend Ext data Model
  • 如何使用 Sencha Touch 数据模型读取嵌套 JSON 结构?

    我整个晚上都在试图解决这个问题 但没有成功 我有一个 JSON 结构如下 来自另一个系统 所以我无法更改其结构 parents parent parentId 1 children child childId 1 ch
  • 原生编程对于移动开发有何优势?

    我需要为一家公司在一些主要的移动操作系统上开发应用程序 特别是 iOS Android 和 WP7 我最初计划为三种不同的操作系统编写三个独立的应用程序 每个应用程序都使用本机 SDK 然而 这样做有什么好处吗 有许多可用的跨平台工具 Se
  • 带有自定义按钮的 ExtJs 消息框

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

    如何在 ExtJS 设计中显示面包屑功能 我正在使用带有边框布局的面板 我想在面板顶部设计碎屑功能 请寄给我一些样品 提前致谢 我想到了两种解决方案 使用面板标题 您将必须操纵面板的标题并在其上创建面包屑 您必须创建面包屑文本 并将其设置为
  • Sencha-touch :保存登录名/密码(保存会话,多任务)

    我有一个 Java Web 应用程序 其中移动部分是用 Sencha touch 开发的 当我启动 sencha touch 应用程序时 她询问我的登录名 密码 因为该应用程序的访问受到限制 但是我想保存用户的登录名 密码 sencha t
  • 无法加载所需框架:extjs 中的 ext@null

    设置 extjs 和 sencha 当我运行应用程序时出现错误无法加载所需的框架 root samuel pc Documents code test sencha app watch Sencha Cmd v6 5 0 180 ERR U
  • 如何确定 ExtJS 4 中 Ext.grid.Panel 的选定单元格?

    我如何获取 Ext grid Panel 的选定单元格 在 ExtJS 3 中可以通过以下方式实现 grid getSelectionModel getSelectedCell 在分机4中有 grid getSelectionModel s
  • 如何将组合框放置在选项卡的标题中?

    是否可以在选项卡标题中显示组合框 如果是 extjs 组合则更好 我创造了jsfiddle 上的一个例子 http jsfiddle net andron v4ZzT 但存在这样的问题 1 无法显示Combo的选项列表 鼠标点击不起作用 2
  • ExtJS打开窗口的最大高度

    我试图通过单击按钮 Ext Button 来打开一个窗口 Ext Window 问题是该窗口的尺寸必须为用户屏幕的 80 宽度和 100 高度 即它应该覆盖所有垂直空间 我真的无法创建一个工作示例 我使用的按钮是隐藏 显示这个窗口 它的大小

随机推荐