Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Knockout.js komapping.toJS不';t映射嵌套可观测阵列_Knockout.js_Knockout Mapping Plugin - Fatal编程技术网

Knockout.js komapping.toJS不';t映射嵌套可观测阵列

Knockout.js komapping.toJS不';t映射嵌套可观测阵列,knockout.js,knockout-mapping-plugin,Knockout.js,Knockout Mapping Plugin,JSON以嵌套数组的形式出现,如下所示 [ { "Amount": 3250, "CustomerAccountNumber": 1, "EntityName": "a", "Id": 1, "IsValidationSuccess": false,

JSON以嵌套数组的形式出现,如下所示

   [
                  {
                    "Amount": 3250,
                    "CustomerAccountNumber": 1,
                    "EntityName": "a",
                    "Id": 1,
                    "IsValidationSuccess": false,
                    "IsWorkInProgress": true,
                    "ValidationErrors": [
                      {
                        "CreationDate": "2017-01-23T00:00:00",
                        "Reason": "Error1"
                      },
                      {
                        "CreationDate": "2017-01-23T00:00:00",
                        "Reason": "error2"
                      }
                    ]
                  },
                  {
                    "Amount": 450,
                    "CustomerAccountNumber": 1,
                    "EntityName": "s",
                    "Id": 4,
                    "IsValidationSuccess": false,
                    "IsWorkInProgress": true,
                    "ValidationErrors": [
                      {
                        "CreationDate": "2017-01-23T00:00:00",
                        "Reason": "error"
                      }
                    ]
                  },
                  {
                    "Amount": 5600,
                    "CustomerAccountNumber": 2,
                    "EntityName": "f",
                    "Id": 2,
                    "IsValidationSuccess": false,
                    "IsWorkInProgress": true,
                    "ValidationErrors": [
                      {
                        "CreationDate": "2017-01-23T00:00:00",
                        "Reason": "error"
                      }
                    ]
                  }
                ]
  • 然后,我按客户编号和EntityName分组,并填充模型可观察数组,使用该数组生成以下JSON

    [ { "Count": 2, "CustomerAccountNumber": 1, "Entities": [ { "Count": 1, "Entity": [ [object Object] ], "EntityName": "a", "Sum": 3250 }, { "Count": 1, "Entity": [ [object Object] ], "EntityName": "b", "Sum": 450 } ], "Sum": 3700 }, { "Count": 1, "CustomerAccountNumber": 2, "Entities": [ { "Count": 1, "Entity": [ [object Object] ], "EntityName": "c", "Sum": 5600 } ], "Sum": 5600 } ] 在AJAX加载之后,我在分组后将对象推送到ObservalArray

            var byAccountNo = groupBy(data, function (item) {
                return [item.CustomerAccountNumber];
            });
    
            _.each(byAccountNo, function (item) {
                var byEntity = groupBy(item, function (i) {
                    return [i.EntityName];
                });
    
                var obj = {
                    CustomerAccountNumber: item[0].CustomerAccountNumber,
                    Entities: ko.observableArray()
                };
    
                _.each(byEntity, function (itemEntity) {
    
                    var objByEntity = {
                        EntityName: itemEntity[0].EntityName,
                        Entity: ko.observableArray()
                    };
    
                    _.each(itemEntity,
                        function (inner) {
                            var result = ko.mapping.fromJS(inner);
                            self.transactionLines.push(result);
                            objByEntity.Entity.push(result);
                        });
    
                    objByEntity.Sum = ko.pureComputed({
                        read: function () {
                            var total = 0;
                            _.each(objByEntity.Entity(), function (item) {
                                total += item.Amount();
                                });
                            return total;
                        },
                        owner: objByEntity
                    });
    
    
                    objByEntity.Count = ko.pureComputed({
                        read: function () {
                            return objByEntity.Entity().length;
                        },
                        owner: objByEntity
                    });
    
                    obj.Entities.push(objByEntity);
                });
    
                obj.Sum = ko.pureComputed({
                    read: function () {
                        var total = 0;
                        _.each(obj.Entities(), function (item) {
                            total += item.Sum();
                        });
                        return total;
                    },
                    owner: obj
                });
    
                obj.Count = ko.computed({
                    read: function () {
                        var total = 0;
                        _.each(obj.Entities(), function (item) {
                            total += item.Count();
                        });
                        return total;
                    },
                    owner: obj
                });
    
    
                self.result.push(obj);
            });
    
        };
    

    看看
    ko.mapping.toJS()
    ,这并不奇怪:

    这将创建一个未映射的对象,该对象仅包含 作为原始JS对象一部分的映射对象。所以在 换句话说,手动添加到的任何属性或函数 您的视图模型将被忽略

    如果要导出对象,我想您可能需要尝试
    ko.toJS()
    ko.toJSON()
    而不是
    ko.mapping.toJS()


    请参阅。

    此对象中的所有内容都是手动创建的,但它仍然映射除实体嵌套数组之外的所有内容。ko.js也不起作用,toJSON将给出我不想要的字符串。@JitendraAanadani您需要提供更多代码。如何修改数据?您的
    viewModel
    是如何定义的?
    viewModel = function () {
        var self = this;
        self.transactionLines = ko.observableArray();
        self.result = ko.observableArray();
    
    }
    
            var byAccountNo = groupBy(data, function (item) {
                return [item.CustomerAccountNumber];
            });
    
            _.each(byAccountNo, function (item) {
                var byEntity = groupBy(item, function (i) {
                    return [i.EntityName];
                });
    
                var obj = {
                    CustomerAccountNumber: item[0].CustomerAccountNumber,
                    Entities: ko.observableArray()
                };
    
                _.each(byEntity, function (itemEntity) {
    
                    var objByEntity = {
                        EntityName: itemEntity[0].EntityName,
                        Entity: ko.observableArray()
                    };
    
                    _.each(itemEntity,
                        function (inner) {
                            var result = ko.mapping.fromJS(inner);
                            self.transactionLines.push(result);
                            objByEntity.Entity.push(result);
                        });
    
                    objByEntity.Sum = ko.pureComputed({
                        read: function () {
                            var total = 0;
                            _.each(objByEntity.Entity(), function (item) {
                                total += item.Amount();
                                });
                            return total;
                        },
                        owner: objByEntity
                    });
    
    
                    objByEntity.Count = ko.pureComputed({
                        read: function () {
                            return objByEntity.Entity().length;
                        },
                        owner: objByEntity
                    });
    
                    obj.Entities.push(objByEntity);
                });
    
                obj.Sum = ko.pureComputed({
                    read: function () {
                        var total = 0;
                        _.each(obj.Entities(), function (item) {
                            total += item.Sum();
                        });
                        return total;
                    },
                    owner: obj
                });
    
                obj.Count = ko.computed({
                    read: function () {
                        var total = 0;
                        _.each(obj.Entities(), function (item) {
                            total += item.Count();
                        });
                        return total;
                    },
                    owner: obj
                });
    
    
                self.result.push(obj);
            });
    
        };