简体   繁体   中英

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 . For one column of this table, I have a foreign key that references to another database table B .
I've setted relation between A and B and it's showed perfectly in jqGrid too. Everything work fine.

The problem is:
I have other column that is a foreign key referenced to table B . But the thing that I need is to show another foreign key that is in B referencing to other table 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.

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. 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 . 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 .

Is there any solution for that?

I use 4.0.0 version of jqGrid , I don't use loadonce attribute.

Finally, the solution was so particular I think:

I passed the data to jqGrid through JSON by the following way:

    // 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:

// 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 :

    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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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