0

The JQgrid I have is generated through JavaScript as I have to show a calendar and corresponding values in it from the JSON. The problem I have is I have to show a count of all the values in the column at the bottom for which I thought Footer row would be a good option.

But the issue is that I am not able to use JavaScript to populate the footer row with the totals of all the values in that column.I want to iterate on the column names and fill each column with its value.

HTML:

<table id="jqGrid"></table>

JavaScript

$(document).ready( function () {


Date.prototype.addDays = function(days) {
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
};
function GenerateColumns(startDate, stopDate, col, columnType, data) {
    var dateArray = col;
    var currentDate = startDate;
    if(columnType === "cn") {
        while (currentDate <= stopDate) {
            dateArray.push(moment(currentDate).format('ddd DD'));
            currentDate = currentDate.addDays(1);
        }
    } else {
        var i = 0;
        while (currentDate <= stopDate) {
            dateArray.push({"name": moment(currentDate).format('ddd DD') , "index" : moment(currentDate).format('ddd DD')});
            currentDate = currentDate.addDays(1);
            i++;
        }
    }
    return dateArray;
}

function countBaseline (columnNames) {
    var actualBaseline = [];

    columnNames.forEach (function (colnm, index) {

        if (index > 3) {

            var mydata = $("#jqGrid").jqGrid("getGridParam", "data"),
                myColumn = $.map(mydata, function (item) {
                    return item[colnm];
                });
            var  i = 0;
            for (var j = 0; j < myColumn.length;j ++ ) {
                if (myColumn[j] !== "NONE") {
                    i++;
                }
            }
            actualBaseline.push(i);
            return;
        }
    });

    return actualBaseline;
}


var $grid = $("#jqGrid");
var cm = [
    {name:'EmployeeID',index:'EmployeeID', width:65, sorttype: "int", editable:false,editoptions:{readonly:true,size:10}  },
    {name:'FirstName', formatter: function (cellvalue, options, rowObject) {
        return cellvalue + ' ' + rowObject.LastName;
    }            },
    {name:'EmployeeType'},
    {name: 'Competencies'}
];


var cn = ['_id','Name', 'type', 'Competencies'];
$.ajax({
    url: 'assets/data/scheduler2.json',
    error: function (err, res) {
        console.log("test");
    },
    success: function (result ) {
        var startDate, days;
        var endDate = moment(result.StartDate).add(result.rows[0].Days.length, 'day');
        var day;
        var columnNames  = GenerateColumns(moment(result.StartDate)._d, endDate._d, cn, "cn"),
            columnModels = GenerateColumns(moment(result.StartDate)._d, endDate._d, cm, "cm", result.rows);

        var width = $grid.closest(".ui-jqgrid").parent().width();
        $grid.jqGrid({
            datatype: 'jsonstring',
            datastr: result,
            jsonReader: {
                repeatitems: false
            },
            colNames: columnNames,
            colModel: columnModels ,
            width: 1880,
            height: 450,
            rowList:[10,20,30],
            pager: '#jqGridPager',
            sortname: 'id',
            viewrecords: true,
            rowNum: 150,
            sortorder: "desc",
            caption: result.ScheduleName,
            footerrow : true,
            userDataOnFooter : true, // use the userData parameter of the JSON response to display data on footer
        });
        var names = columnNames,
            data= result.rows;
        var mydata = [];

        for (var i = 0; i < data.length; i++) {
            mydata[i] = {};
            for (var j = 0; j < data[i].Comp.length - 1; j++) {
                mydata[i][names[j + 4]] = data[i].Comp[j];
                if (names[j + 4].split(" ")[0] === "Sat" || names[j + 4].split(" ")[0] === "Sun")
                    $('#jqGrid').jqGrid('setCell',i, names[j + 4],"",{'background-color':'#AFEEEE', 'background-image':'none'});
            }
        }

        for (var k = 0; k <= mydata.length; k++) {
            $("#jqGrid").jqGrid('setRowData', k + 1, mydata[k], "first");
        }

        var actual = countBaseline(columnNames);
        var i = 0;
        columnNames.forEach (function (colnm, index) {
            if (i > 3) {
                $grid.jqGrid(
                    "footerData",
                    "set",
                       {colnm: actual[index]}
                    //{"Sun 06": actual[index]}
                    // {"Sun 06": 54   }
                );
            }
            i++;
        });
    }

  });
});

The main code is the last one, Where I have baseline which is an array of numbers of which I want to populate each value with a corresponding column. What happening is if you see the commented lines "set footerData" is only writing the value on one column but not writing values on all the columns with forEach.

         var baseline = countBaseline(columnNames);
        var i = 0;
        columnNames.forEach (function (colnm, index) {
            if (i > 3) {
                $grid.jqGrid(
                    "footerData",
                    "set",
                        {colnm: baseline[index]}
                   // {"Sun 06": baseline[index]}
                    // {"Sun 06": 54   }
                );
            }
            i++;
        });

What I want to achieve:

enter image description here

Suleman Mirza
  • 905
  • 1
  • 12
  • 22
  • 1
    I'm not sure that I understand your question correctly. Look at [the old answer](http://stackoverflow.com/a/13703037/315935). Please include in every question the information about the version and the fork of jqGrid ([free jqGrid](https://github.com/free-jqgrid/jqGrid), [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7), which you use. You use for example `jqGrid('setCell'` in the loop after the grid is created. It's very ineffective. If you don't use some retro version of jqGrid the the usage of `cellattr` or `rowattr` is much more better. – Oleg Jan 20 '16 at 09:48
  • Sorry I am using JqGrid 5.0.1 (free). What I want to achieve is show the last two rows which have numbers (6) in it t show in the footer grid.I have the function to set footer data for each column but it is not mapping the array (baseline) on each column (colnm) as I want it to do. – Suleman Mirza Jan 21 '16 at 04:11
  • There are no free jqGrid 5.0.1. You can access to the source code, but you ca find the prices and the license agreements [here](http://guriddo.net/?page_id=103334). – Oleg Jan 21 '16 at 05:49
  • About your main question: it's not important how you generate the `colModel`. It's only important to see the results. Can you include an example of `columnModels`? Which `name` property have every column? `footerData` with `set` can set the *whole* footer. You need to build the object with the value for the footer. Moreover the `name` property of `colModel` should contains **no spaces**, because spaces are not allowed in id of HTML5. – Oleg Jan 21 '16 at 05:58

0 Answers0