[英]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. 我已经设定了
A
和B
之间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自动理解它,将正确的值放入
index
和jsonmap
:
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.