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

标签 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_description 值,即 This员工已收到他的薪水,而以下 上面代码中提到的行在控制台面板中清楚地显示了它:

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 代码未获取第一条记录的 code_description 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689166/

相关文章:

javascript - d3.js 仪表针随动态数据旋转

javascript - 使用 css3 或 javascript 动画 webkit 渐变的方法?

javascript - 插入名称和值对

javascript - 在标记页面中,如何向 html 标记的 textContent 属性添加内容?

javascript - Jquery/Javascript 从具有最高属性值的数组中删除条目

javascript - 如何防止使用 JavaScript 或 jQuery 向上滚动?

javascript - 如何根据来自 REST 调用的响应形成 json 数组?

ios - 从 Facebook 页面获取事件

javascript - 如何重新加载jqxwindow initContent : function ()?