Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In jqxGrid, how do I add a new calculated column from JSON data?

My JSON data has fields baseQuantity and unitCost. I want to add a new field called totalCost which would be baseQuantity * unitCost.

I'm trying to add the data using loadComplete, but it doesn't seem to work.

ANOTHER alternative I could do is to loop through objData and inject a new field with the calculated value. But aside from that, is there any way I could do it via jqxGrid's API?

var jsonString = [{ "baseQuantity":"1", "unitCost":"2"}, { "baseQuantity":"3", "unitCost":"4"}];
var objData = $.parseJSON(jsonString);

var srcData = {
        datatype: "json",
        datafields: [ 
            { name : 'baseQuantity', type : 'number' },
            { name : 'unitCost', type : 'number' }
        ],
        localdata : objData
    };

var adapterData =  new $.jqx.dataAdapter(srcData, {
    loadComplete: function (records) {
        var modifiedDataArray = new Array();
        for (var i = 0; i < records.length; i++) {

            var modifiedData = records[i];
            modifiedData.totalPayment = modifiedData.baseQuantity * modifiedData.unitCost;

            modifiedDataArray.push(programme);
        }
        return modifiedDataArray;
    }
});

$('div#jqxGrid').jqxGrid({
    width: '100%',
    source: adapterData,
    theme: getTheme(),
    pageable: true,
    autoheight: true,
    sortable: true,
    altrows: true,
    columns: [
        { datafield: 'baseQuantity', text: 'Base Qty.', width: 120 }
        { datafield: 'unitCost', text: 'Unit Payment' , width: 120 }
    ]
});
share
add comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.