我有和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_description值,即This employee has received his salary
上面代码中提到的行在控制台面板中清楚地显示了它:

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

最佳答案

newItem.description = collection [i] .code_description;

---在第一个对象条件下将newItem.description替换为newItem.code_description

newItem.hiringCriterion = collection [i] .hiring_criteria;

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

关于javascript - javascript代码未选择第一条记录的code_description值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43689166/

10-10 00:04