简体   繁体   English

显示来自两个连续相关数据库表的jqGrid数据

[英]Show jqGrid data from two consecutive related database tables

I have this scenario: 我有这种情况:

One database table A , that shows all its data in a jqGrid . 一个数据库表A ,将其所有数据显示在jqGrid For one column of this table, I have a foreign key that references to another database table B . 对于此表的一列,我有一个引用另一个数据库表B的外键。
I've setted relation between A and B and it's showed perfectly in jqGrid too. 我已经设定了AB之间A关系,它在jqGrid也很完美。 Everything work fine. 一切都很好。

The problem is: 问题是:
I have other column that is a foreign key referenced to table B . 我有其他列是引用表B的外键。 But the thing that I need is to show another foreign key that is in B referencing to other table C . 但是我需要做的是显示B引用另一个表C另一个外键。 I have been able to show that results in jqGrid using (badly) formatter: 'select' and creating custom arrays from PHP to trick the solution. 我已经能够使用(严重) formatter: 'select'并通过PHP创建自定义数组来欺骗解决方案,从而在jqGrid显示结果。

The problem is that I can see data, but I can't filter this column because of the bad implementation. 问题是我可以看到数据,但是由于执行不正确,所以无法过滤此列。

I pass to jqGrid , arrays from PHP with Twig. 我传递给jqGrid ,PHP与Twig的数组。 I've needed to create two auxiliary arrays, one to have a list of ids of table A , and other to have values from table C . 我需要创建两个辅助数组,一个要具有表A的ID列表,另一个要具有表C值。 I related both tables by this way. 我通过这种方式关联了两个表。

This is my code: 这是我的代码:

// colModel
{name:'<%identificator%>', 
    index:'table_A_id', 
    jsonmap:'table_A_id',
    editable:true,
    editrules:{ edithidden:true, required:true },
    formoptions:{ elmsuffix:' (*)' }, 
    edittype: 'select', 
    stype:'select',
    formatter: 'select',
    editoptions:{
                    value:":<%repeat%>;<%table_A_id%>:<%table_C_value%><%/repeat%>"
                }

With this code, I have right results inside each cell, but not right in select list for filtering. 使用此代码,我在每个单元格中都有正确的结果,但在选择列表中没有正确的过滤。

Resuming: I need to show a value that is in table C from table A , but both tables haven't got relation, only through table B . 继续:我需要显示表A中表C的值,但是两个表都没有关系,仅通过表B

Is there any solution for that? 那有什么解决方案吗?

I use 4.0.0 version of jqGrid , I don't use loadonce attribute. 我使用4.0.0版本的jqGrid ,我不使用loadonce属性。

Finally, the solution was so particular I think: 最后,我认为解决方案非常特别:

I passed the data to jqGrid through JSON by the following way: 我通过以下方式通过JSON将数据传递给jqGrid:

    // This is data from Table A(id, name, b_id)
    {
        "page":"1",
        "total":122,
        "records":3635,
        "rows":[
        {
            "id":1,
            "name":"example",
            "b_id":8
        },
        { 
            "id":2,
            "name":"example2",
            "b_id":19
        }]
    }

So I wasn't able to relate table A and C, but finally I did it in PHP using model relations: 因此,我无法关联表A和C,但最终我在PHP中使用模型关系实现了它:

// This is data from Table A(id, name, b_id) with relations
{
    "page":"1",
    "total":122,
    "records":3635,
    "rows":[
    {
        "id":1,
        "name":"example",
        "b_id":8,
        "relations":{
            "relation":{
                "id":200,"name":"Pepe", "id_c":22
            }
        }
    },
    { 
        "id":2,
        "name":"example2",
        "b_id":19,
        "relations":{
            "relation":{
                "id":356,"name":"Jose", "id_c":45
            }
        }
    }]
}

And jqGrid automatically understood it, putting right values into index and jsonmap : 并且jqGrid自动理解它,将正确的值放入indexjsonmap

    colModel:[ ...
, {
    name:'Title',
    index:'relation.id_c',
    jsonmap:'relations.relation.id_c',
    width: 40, editable:false
    stype:'select', formatter:'select',
    editoptions:{
        value:"ids_C_table:values_C_table"
    }
}
...]

With this implementation, I get that I need. 有了这个实现,我得到了我需要的东西。 Cells show right data and filters work perfectly. 单元格显示正确的数据,并且过滤器可以完美地工作。

Hope it helps someone! 希望它可以帮到某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM