简体   繁体   中英

jqGrid - How to calculated column to jqgrid?

I am working on an ASP MVC 2 application.It has a jqGrid and data in the grid is populated from the JSON object returned by the controller. My grid has 3 columns 'UnitNo','Area' and 'Rate per sqm'. These column values are retrived from the database. Now what i need is to add a 4th column 'Unit Price' which is calculated by multiplying 'Area' and 'Rate per sqm'. How can i accomplish this? Is there any way to add a calculated column in jqGrid? Or can i do the calculation in my controller and add it as a new element of row cell?

Here is my controller code:

public JsonResult GetERVList()
    {
        var ervRep=new ERVRepository();
        IList<ERVMaster> list = ervRep.ListERVData();
        int pageSize = 50;
        int totalRecords = list.Count();
        var totalPages = (int)Math.Ceiling(totalRecords / (float)pageSize);
        var jsonData = new
        {
            total = totalPages,
            pageSize,
            records = totalRecords,
            rows = (from ervdata in list
                    select new
                    {
                        i = ervdata.Id,
                        cell = new[]
                            {
                                ervdata.UnitNo,
                                ervdata.Area, 
                                ervdata.RatePerSQM


                            }

                    }).ToArray()
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);

}

and my jqGrid code is like this

<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        url: '/ERV/GetERVList/',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Unit', 'Area', 'Rate per SQM'],
        colModel: [ { name: 'UnitNo', index: 'UnitNo' },
                    { name: 'Area', index: 'Area' },
                    { name: 'RatePerSQM', index: 'RatePerSQM' }],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'Id',
        sortorder: "Id",
        viewrecords: true,
        caption: 'My first grid'
    });
});

Thanks in advance, Ancy

I know , I am late but this will help anybody who is looking for how to calculate derived column

Following should work in jQgrid v4.0

change the colNames and colModel config as per following

colNames: ['Unit', 'Area', 'Rate per SQM', 'Unit Price'],
colModel: [ { name: 'UnitNo', index: 'UnitNo' },
            { name: 'Area', index: 'Area' },
            { name: 'RatePerSQM', index: 'RatePerSQM' },
            { name: 'RatePerSQM', index: 'RatePerSQM',
              formatter: function (cellvalue, options, rowObject) 
                         {
                             return rowObject["Area"] * cellvalue
                          }
            }],

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