How to access other row data from custom cell formatting in JqGrid

I have a data source XMLas shown below

<root>
    <item priceOri = "100" discount = "10"></item>
    <item priceOri = "200" discount = "110"></item>
    .
    .
    .
</root>

And I fill this data into a table using JqGrid. the code looks something like this.

datatype : 'xml',
colModel: [ 
  ... 
  {name:'priceOri', index:'priceOri', width:60,xmlmap : "[priceOri]", align:"center"},
  {name:'discount', index:'discount', width:60,xmlmap : "[discount]", align:"center"},
  {name:'price', index:'price', width:60,xmlmap : "[price]", align:"center", editable: true, formatter:discountFmatter},
  ...
 ]

xmlReader: {
    root: "root",
    row: "item",
    repeatitems: false
},  

The formatter is as follows.

function discountFmatter (cellvalue, options, rowObject)
{
   var price;
   // do calculation based on other cell values in the same column
   //price = priceOri - discount;
   var new_format_value = price;
   return new_format_value
}   

As in the code, I need to access the other values ​​in the tag itemin order to calculate the section price. So basically I want to access other cell values ​​in the same row. How can i do this.

I used the code snippets below, but they were listed as undefined

rowObject[0]  // undefined
rowObject.priceOri //undefined

Can anyone show me the steps to achieve this.

: JqGrid 4.4.0 . , . , JqGrid 4.4.0.

, Oleg rowObject instanceof Element ? $(rowObject).attr("priceOri") : rowObject.priceOri .

2: ( rowObject.rewards )

XML ,
     < item priceOri = "100" discount = "10" rewards = "20" > </item>

,

function discountFmatter (cellvalue, options, rowObject)
{
   var price;
   // do calculation based on other cell values in the same column
   //price = priceOri - discount - rewards;
   var new_format_value = price;
   return new_format_value
}  

, rewards jQgrid. , .

: In case of usage old jqGrid you will have to add new column rewards. You can use hidden: true property in the column. Free jqGrid allow you to use additionalProperties

+4
1

. rowObject . , JSON rowObject[0] rowObject.priceOri, , repeatitems: false . JSON, . $(rowObject).find(">priceOri") $(rowObject).attr("priceOri") ( ).

( ) , loadonce: true . , , , . ( , ) rowObject.priceOri.

jqGrid fork jqGrid , jqGrid (. ), (. ) jqGrid "Guriddo jqGrid JS". jqGrid ( wiki readmes ).

jqGrid rowObject , , , option . rowData . priceOri jqGrid options.rowData.priceOri. ,

function discountFmatter (cellvalue, options, rowObject) {
    return parseFloat(options.rowData.priceOri) -
           parseFloat(options.rowData.discount);
}

loadonce: true, , XML JSON (, ).

jqGrid GitHub

enter image description here

$(function () {
    "use strict";
    function discountFmatter (cellvalue, options, rowObject) {
        return parseFloat(options.rowData.priceOri) -
                parseFloat(options.rowData.discount);
    }

    $("#grid").jqGrid({
        url: "prime.xml",
        datatype: "xml",
        colModel: [ 
            { name: "priceOri", xmlmap: "[priceOri]" },
            { name: "discount", xmlmap: "[discount]" },
            { name: "price", xmlmap: "[price]", editable: true,
                formatter: discountFmatter }
        ],
        cmTemplate: { width: 60, align: "center" },
        iconSet: "fontAwesome",
        xmlReader: {
            root: "root",
            row: "item",
            repeatitems: false
        },
        loadonce: true,
        viewrecords: true,
        rownumbers: true,
        caption: "Qaru Example"
    });
});
+1

Source: https://habr.com/ru/post/1619884/


All Articles