【问题标题】:ExtJS: Converting model columns to rows in a gridExtJS:将模型列转换为网格中的行
【发布时间】:2019-07-05 01:33:53
【问题描述】:

我是 ExtJS 的新手,想要实现以下目标:

我有一个模型,其数据如下:

{
   "Account_Enabled": true, 
   "Requirements_gathered": true, 
   "work_done": false, 
   "deadlines": {
     "Account_Enabled": true, 
     "Requirements_gathered": false
    }
}

没有多行。仅从数据库返回一行。

我想在一个三列的网格中显示数据 Column1:列的名称 Column2:显示值是真还是假的复选框 Column3:一个复选框,显示列名是否出现在“截止日期”中

例如:

Account_Enabled True True

Requirements_Gathered True False

work_done False 未定义(需要禁用复选框)

基本上,我需要将该单行转换为三列。 此外,当用户选中/取消选中复选框时,我需要更新商店

我可以知道是否有任何方法可以通过 ExtJS 网格实现这一点?还是有更好的主意?

提前致谢

【问题讨论】:

    标签: javascript extjs extjs5


    【解决方案1】:

    没有直接的方法可以将您提到的格式的 json 响应绑定到网格存储。

    您需要做的是操纵匹配网格列所需的响应。

    检查这个工作示例ExtJs Fiddle

    //Your Original Response
    let response = '{"Account_Enabled": true, "Requirements_gathered": true, "work_done": false, "deadlines": {"Account_Enabled": true, "Requirements_gathered": false}}';
    // Convert your response to object
    let jsonDecode = Ext.decode(response);
    
    //This function to convert your response to match grid store
    let dataConvert = function(jsonObj){
        //Returned array object
        let data = [];
        // To get Object of deadlines and know if the property exist or not
        let availableData = jsonObj.deadlines
        //Loop throw object property
        for(var objProperty in jsonObj){
            //Ignore deadlines property
            if(objProperty=="deadlines"){
                continue;
            }
            //Adding the object to the array "objPropery" will return the property name
            //"jsonObj[objProperty]" will return the value of property if it is true or flase
            //"availableData[objProperty]" will return the value if exist in availableData
            data.push({colName:objProperty,isReqGathered:jsonObj[objProperty],isWorkDone:availableData[objProperty]})
        }
        return data
    }
    
    
    let gridStore = Ext.create('Ext.data.Store', {
        storeId: 'gridStoreId',
        fields: ['colName', 'isReqGathered', 'isWorkDone'],
        data: dataConvert(jsonDecode)
    });
    
    Ext.create('Ext.grid.Panel', {
        title: 'Test Store Filter',
        width: 500,
        height: 400,
        renderTo: Ext.getBody(),
        store: gridStore,
        columns: [{
            dataIndex: 'colName',
            text: 'colName'
        }, {
            dataIndex: 'isReqGathered',
            text: 'isReqGathered'
        }, {
            dataIndex: 'isWorkDone',
            text: 'isWorkDone'
        }]
    })
    

    【讨论】:

    • 非常感谢您的回答:)。它真的很有用。显示从商店返回的数据有效。但是,如果有人编辑任何单元格,我该如何更新数据库?有什么建议吗?
    • 对于后端,您应该拥有具有 3 个属性 colName、isReqGathered、isWorkDone 的 bean,这样您就可以将这些值从前端发送到后端,并且您在后端拥有这些数据,您可以编写解决方案那里。所以你需要从服务器端进行数据操作
    • 嗨@moataz Sanad:后端不愿意更改架构/逻辑。所以我通过以下方法解决它:在 checkchange 事件中,我正在格式化模型并在 viewmodel 上调用 notify,这反过来将数据发送回服务器 var property = 'data .deadlines.Accounts_enabled' page.getViewModel().set(property, checked)
    猜你喜欢
    • 1970-01-01
    • 2014-06-30
    • 2011-10-03
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多