javascriptjsonjqxwidgetsjqxcombobox

javascript code not picking up code_description value for first record


I have something similar to the JSFiddle mentioned here. The JSFiddle works fine, however, I am seeing a strange behaviour in my code. The description of very first cell is always empty even though I am seeing that in the returned data (in JSON format) from the webservice, the description is present for each record. Please take a look at the code below and let me know if I am missing anything:

 this.processEmployees = function (data_, textStatus_, jqXHR_) {

            var collection = data_.employees; 

            // A helper function used for employee codes below.
            var isUsedKey = function (arrayOfObject, key) {
                for (var i = 0; i < arrayOfObject.length; i += 1) {
                    if (arrayOfObject[i].key == key) {
                        return true;
                    }
                }
                return false;
            };


            var employeeCodes = [];

            for (var i = 0; i < collection.length; i++) {
                if (i == 0) {
                    var newItem = {};
                    newItem.key = collection[i].code_value;
                    newItem.dates = [collection[i].code_assignment_date];
                    newItem.description = collection[i].code_description;
                    newItem.hiringCriterion = collection[i].hiring_criteria;
                    newItem.name = collection[i].name;
                    console.log("Would like to check code_description for first item:",collection[i].code_description);
                    //debugger;
                    employeeCodes.push(newItem);
                } else {
                    var item = collection[i];
                    var itemName = item.code_value;
                    var itemDate = item.code_assignment_date;
                    var itemDescription = item.code_description;
                    var hiringCriterion = item.hiring_criteria;
                    var itemCodeName = item.name;

                    if (isUsedKey(employeeCodes, itemName)) {
                        for (var j = 0; j < employeeCodes.length; j++) {
                            if (employeeCodes[j].key == itemName) {
                                var index = employeeCodes[j].dates.length;
                                employeeCodes[j].dates[index] = itemDate;
                            }
                        }
                    } else {
                        var nextNewItem = {};
                        nextNewItem.key = itemName;
                        nextNewItem.dates = [itemDate];
                        nextNewItem.code_description = itemDescription;
                        nextNewItem.hiring_criteria = hiringCriterion;
                        nextNewItem.name = itemCodeName;
                        employeeCodes.push(nextNewItem);
                    }
                }
            }



            var newSource = {
                localdata: employeeCodes,
                datafields: [{
                    name: 'code_value',
                    type: 'string',
                    map: 'key'
                },
                {
                    name: 'code_assignment_date',
                    type: 'date',
                    map: 'dates>0'
                },
                {
                    name: 'name',
                    type: 'string'

                },
                {
                    name: 'code_description',
                    type: 'string'

                },
                {
                    name: 'hiring_criteria',
                    type: 'string'

                }
                ],

                datatype: "array" 

            };

            var newAdapter = new $_.jqx.dataAdapter(newSource);





            var iconrenderer = function (row, columnfield, value, defaulthtml, columnproperties) {
                var icon = '';
                if (employeeCodes[row].dates.length > 1) {
                    icon = '<img src="images/icon-down.png" style="position: absolute; right: 5px;" />';
                 }

                return '<span style="position: relative; width: 100%; margin: 4px; float: ' + columnproperties.cellsalign + ';">' + newAdapter.formatDate(value, 'd') + icon + '</span>';
            };




            $_(self.gridSelector).jqxGrid({
                source: newAdapter,

                editable: true,
                width: '600',
                pageable: true,
                sortable: true,
                autoheight: true,
                theme: 'classic',
                height: '170',
                columnsResize: true,
                columns: [
                {
                    text: 'Employee Name',
                    datafield: 'name',
                    width: 85,

                },
                {
                    text: 'Code Value',
                    datafield: 'code_value',
                    width: 75,
                    editable: false


                },

                {
                    text: 'Latest Date(s)',
                    datafield: 'code_assignment_date',
                    cellsformat: 'd',
                    columntype: 'combobox',
                    width: 100


                    createeditor: function (row, column, editor) {
                        var info = $_(self.gridSelector).jqxGrid('getrowdata', row);

                        console.log("length of info: " + info);
                        var groupName = info.code_value; 
                        console.log("Contents of groupName: " + groupName);
                        var dates = [];
                        for (var i = 0; i < employeeCodes.length; i++) {
                            if (employeeCodes[i].key == groupName) {
                                dates = employeeCodes[i].dates;
                            }
                        }
                        editor.jqxComboBox({ autoDropDownHeight: false, source: dates, promptText: "Previous Date(s):", scrollBarSize: 10 });

                    },

                    initeditor: function (row, column, editor) {
                        var info = $_(self.gridSelector).jqxGrid('getrowdata', row);
                        var groupName = info.code_value;
                        var dates = [];
                        for (var i = 0; i < employeeCodes.length; i++) {
                            if (employeeCodes[i].key == groupName) {
                                dates = employeeCodes[i].dates;
                            }
                        }



                        editor.jqxComboBox({
                            autoDropDownHeight: false,
                            source: dates,
                            promptText: "Dates:",
                            width: 100,
                            scrollBarSize: 10,

                            renderer: function (index_, label_, value_) {
                                return formatDateString(value_);
                            },
                            renderSelectedItem: function (index, item) {
                                var records = editor.jqxComboBox('getItems');
                                var currentRecord = records[index].label;
                                return formatDateString(currentRecord);;
                            }
                        });
                    },
                    cellvaluechanging: function (row, column, columntype, oldvalue, newvalue) {
                        // return the old value, if the new value is empty.
                        if (newvalue == "") return oldvalue;
                    }
                },

                {
                    text: 'Description',
                    datafield: 'code_description'

                },
                {
                    text: 'Hiring Criterion',
                    datafield: 'hiring_criteria',
                    editable: false,
                    hidden: true

                }

                ],
            });

        }; // End of processEmployees

For Example, if my JSON is the following:

{
    "employees": [{

        "code_value": "B1",
        "code_assignment_date": "2016-12-13 23:04:00.0",
        "code_type": 7,
        "code_description": "This employee has received his salary",
        "name": "Peter",
        "hiring_criteria": null

    }, {

        "code_value": "A1",
        "code_assignment_date": "2016-05-20 05:00:00.0",
        "code_type": 7,
        "code_description": "Employee has 5  vacation days left",
        "name": "Jack",
        "hiring_criteria": null

    }],
    "webServiceStatus": {
        "status": "SUCCESS",
        "message": "2 results"
    }
}

I am not seeing the very first code_description value which is This employee has received his salary in the above JSON response whereas the following line mentioned in the above code clearly shows it in the console panel:

console.log("Would like to check code_description for first item:",collection[i].code_description);

Solution

  • newItem.description = collection[i].code_description;

    ---Replace newItem.description to newItem.code_description in First object condition

    newItem.hiringCriterion = collection[i].hiring_criteria;

    ---Replace newItem.hiringCriterion to newItem.hiring_criteria in First object condition