简体   繁体   中英

KnockoutJs - Pushing to an element in an observable array does not work

I have an object (analysisLogData) that I use to generate a table using KnockoutJS. Here's the viewModel containing this object:

function AppViewModel() {
    var self = this;
    self.analysisLogData = ko.observableArray();
    self.analysisLogTitle = ko.observable("Warnings")

    self.changeAnalysisLog = function(title) {
         self.analysisLogTitle(title)   
    }

    var data =

    {
        "Warnings": [
            {
                "number": 3002,
                    "description": "There may be a problem with the device you are using if you use the default profile"
            },

            {
                "number": 3001,
                    "description": "There may be a problem with the device you are using if you don't use the default profile"
            }

            ]

        ,
            "Errors": [
            {


                "number": 1000,
                    "description": "No networks are loaded"
            },

            {
                "number": 1002,
                    "description": "No devices are loaded"
            }]




    }


    self.addLog = function (type, content) {
        self.analysisLogData()[type].push(content);
    }

    self.analysisLogData.push(data)


}

ko.applyBindings(new AppViewModel());

You can see the result here in a JSFiddle: http://jsfiddle.net/etiennenoel/V4r2e/5/

I want to be able to add an error or a warning without losing the warnings or errors already present.

I tried to do the following in the self.addLog function:

self.addLog = function (type, content) {
        self.analysisLogData()[type].push(content);
    } 

but it says that it can't push to an undefined object...

Ok, after playing around in fiddle. I believe that you need to do some changes in how you pushed data in the observable array. But without doing a lot of modification check my solution in this link.

jsfiddle example

self.addLog = function (type, content) {

    self.analysisLogData()[0][type].push({
        "number": 1002,
        "description": content
    });
}

And data object should be

"Warnings": ko.observableArray([........]),
"Errors": ko.observableArray([..........])

I did two things

  1. Modify Warnings & Errors to be an Observable Array
  2. I pushed the data in this self.analysisLogData()[0][type].push instead of self.analysisLogData()[type].push

self.analysisLogData() is an array which contains arrays of Errors/Warnings.

I'm not sure if that's how you want your data structured.

To get the fiddle to work you can replace the addLog function with this:

self.addLog = function (type, content) {
        self.analysisLogData()[0][type].push(content);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM