javascript 代码未获取第一条记录的 code_description 值

javascript code not picking up code_description value for first record

我有类似于JSFiddle mentioned here的东西。 JSFiddle 工作正常,但是,我在我的代码中看到了一个奇怪的行为。最先的解释 单元格始终为空,即使我在网络服务返回的数据(JSON 格式)中看到每条记录都有描述。请 看看下面的代码,如果我遗漏了什么请告诉我:

 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

例如,如果我的 JSON 如下:

{
    "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"
    }
}

我没有在上面的 JSON 响应中看到第一个 code_description 值,即 This employee has received his salary,而下面的 上面代码中提到的行在控制台面板中清楚地显示了它:

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

newItem.description = 集合[i].code_description;

---将第一个对象条件

中的newItem.description替换为newItem.code_description

newItem.hiringCriterion = 集合[i].hiring_criteria;

---将第一个对象条件

中的newItem.hiringCriterion替换为newItem.hiring_criteria