简体   繁体   中英

JQGrid POST a column's value instead of ID

I'm trying to edit a record but instead of sending the value of the ID column, it keeps sending the ID for the table, which I do not care about. Here's the code:

$("#list1").jqGrid({
                datatype: 'clientSide',             
                colNames: ["Id", "Id prodotto", "Nome prodotto", "Quantità",
                           "Pista", "Politica", "Attivazione", "Bundle",
                           "Scontato", "Valore sconto", "Twin card", "DN",
                           "ISDN", "Disabilitato"],
                colModel: [
                    { name: "id", hidden:true, key:true},
                    { name: "idprodotto", hidden:true},             
                    { name: "prodotto"},
                    { name: "quantita"},
                    { name: "pista"},
                    { name: "politica"},
                    { name: "attivazione", formatter: "checkbox"},
                    { name: "bundle", formatter: "checkbox"},
                    { name: "sconti", formatter: "checkbox"},
                    { name: "scontival"},
                    { name: "card", formatter: "checkbox"},
                    { name: "numero"},
                    { name: "isdn", formatter: "checkbox"},
                    { name: "disabilitato", width:100, sortable: false, resizable:false, hidden:true }
                ],  
            sortname: "prodotto",
            sortorder: "asc",
            pager: "#pager1"
            })          
            .navGrid('#pager1', {add: false, del: false});

It may help to say that in this page there are two tables, here's the first one:

$("#list").jqGrid({
                datatype: 'clientSide',
                colNames: [
                           "Id",
// "Id cliente",
                         "Numero",  "RagSociale cliente", "Anno",
                           "Data di ordine", "Id agente", "Nome agente", "Cognome agente",
                           "Id agenzia", "RagSociale agenzia", "Id utente", "Nome utente", 
                           "Data d'inserimento", "Note", "Disabilitato",""],
                colModel: [
                    { name: "id", hidden:true, key:true },
//                      { name: "idCliente"},   
                    { name: "numero"},  
                    { name: "ragSocCliente"},
                    { name: "anno"},
                    { name: "dataOrdine"},              
                    { name: "idAgente"},
                    { name: "nomeAgente"},
                    { name: "cognomeAgente"},
                    { name: "idAgenzia"},
                    { name: "ragSocAgenzia"},
                    { name: "idUser"},
                    { name: "nomeUser"},                                            
                    { name: "dataInserimento"},             
                    { name: "note"},                
                    { name: "disabilitato", width:100, sortable: false, resizable:false, hidden: true},
                    { name: "dataModifica", width:100, hidden: false}
                ],

                sortname: "dataModifica",
                sortorder: "desc",
                pager: "#pager",
                onSelectRow: function (id) {                    
                    sessionStorage.setItem("idEdit", $("#list").jqGrid('getCell', id, 'id'));
                    $.get("getCorpoByNumero.do", {                          
                        id: sessionStorage.getItem("idEdit")
                    },  function (data) {
                            $("#list1").clearGridData();
                            for (var i = 0; i < data.length; i++) {                     
                                var corpo = data[i];                                                
                                $("#list1").addRowData((i+1), corpo);

                            }
                    });

                }
            })

It's unclear which "column's value" you want to POST instead of ID? Which grid have the problem? You use navGrid for the grid #list1 without specifying and editurl which looks incorrect.

The filling of the second grid using addRowData is very bad! Especially bad would be to use values 0 , .. data.length as the rowids instead of ids returned from the server (from "getCorpoByNumero.do" ). It's much more effective to change data parameter of the grid and to reload it by .trigger("reloadGrid") :

onSelectRow: function (id) {                    
    sessionStorage.setItem("idEdit", id);
    $.get("getCorpoByNumero.do", {                          
        id: id
    }, function (data) {
        var $grid1 = $("#list1"), p = $grid1.jqGrid("getGridParam");

        p.data = data;
        $grid1.trigger("reloadGrid");
    });
}

Moreover the usage of idPrefix is strictly recommended if you create more as one grid on the page. The standard behavior of jqGrid is setting id attribute of all <tr> elements of the grid (setting the rowid) to the value of id property of input data. It can produce id dupplicates if both grids would have the same id value. The usage of idPrefix: "g1_" for example in the #list1 grid will force adding "g1_" prefix to all id values of the grid. It fixes possible id duplicates. The prefix will be automatically stripped during sending the data to the server.

The last remark. I'd recommend you to remove unneeded { name: "id", hidden:true, key:true } column from the grids. The rowid will use id property of input data automatically. Instead of $("#list").jqGrid('getCell', id, 'id') you can use just id in the onSelectRow callback and to have the same data. In the same way you can remove other hidden columns ( disabilitato and idprodotto ). The internal data will still have all input properties and you can use getLocalRow to access the data.

I recommend to replace datatype: 'clientSide' to more common datatype: 'local' and verify which version of jqGrid you use. I recommend you to use the latest version (4.13.1) of free jqGrid . It's the for of jqGrid, which I develop.

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