javascript代码没有为第一条记录获取code_description值

时间:2017-04-28 21:31:03

标签: javascript json jqxwidgets jqxcombobox

我有类似JSFiddle mentioned here的内容。 JSFiddle运行正常,但是,我在代码中看到了一个奇怪的行为。第一个描述 尽管我在Web服务的返回数据(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_descriptionThis employee has received his salary,而以下 上面代码中提到的行清楚地在控制台面板中显示:

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

1 个答案:

答案 0 :(得分:1)

newItem.description = collection [i] .code_description;

---在第一个对象条件

中将newItem.description替换为newItem.code_description

newItem.hiringCriterion = collection [i] .hiring_criteria;

---在第一个对象条件中将newItem.hiringCriterion替换为newItem.hiring_criteria