How to use knockout to iterate over an object (not array)

John Livermore :

I want to use something similar to the Knockout foreach construct to iterate over the properties of an object. Here is what I am trying to create...

DESIRED RESULT

<table>
    <tr>
        <td>Name 1</td>
        <td>8/5/2012</td>
    </tr>
    <tr>
        <td>Name 2</td>
        <td>2/8/2013</td>
    </tr>
</table>

However, my model looks like this...

JS

function DataModel(){
    this.data = ko.observableArray([{
                        entityId: 1,
                        props: {
                            name: 'Name 1',
                            lastLogin: '8/5/2012'
                        }
                    },
                    {
                        entityId: 2,
                        props: {
                            name: 'Name 2',
                            lastLogin: '2/8/2013'
                        }
                    }]);
}

var dataModel = new DataModel();
ko.applyBindings(dataModel);

Each row has an entityId and props which is an object itself. This template doesn't work, but how would I change it to generate the desired table above?

EDIT: The props in this example are name and lastLogin, but I need a solution that is agnostic to what is contained inside props.

I have this FIDDLE going as well.

HTML

<div data-bind="template: { name: 'template', data: $data }"></div>

<script type="text/html" id="template">
    <table>
        <tr data-bind="foreach: data()">
            <td data-bind="text: entityId"></td>  
        </tr>
    </table> 
</script>
Jeff Mercado :

You could always create a binding handler to handle the transformation.

ko.bindingHandlers.foreachprop = {
  transformObject: function (obj) {
    var properties = [];
    ko.utils.objectForEach(obj, function (key, value) {
      properties.push({ key: key, value: value });
    });
    return properties;
  },
  init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
    var properties = ko.pureComputed(function () {
      var obj = ko.utils.unwrapObservable(valueAccessor());
      return ko.bindingHandlers.foreachprop.transformObject(obj);
    });
    ko.applyBindingsToNode(element, { foreach: properties }, bindingContext);
    return { controlsDescendantBindings: true };
  }
};

Then apply it:

<div data-bind="template: { name: 'template', data: $data }"></div>

<script type="text/html" id="template">
    <table>
        <tbody data-bind="foreach: data">
            <tr data-bind="foreachprop: props">
                <td data-bind="text: value"></td>
            </tr>
        </tbody>
    </table> 
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to iterate over a nested object array in Javascript

How to iterate over array nested in object as props

How to iterate over an array in an object with async pipe

How to iterate over object values in an array?

How to use ngFor to iterate over array of objects

How to use Class to iterate over an array?

Can you use instanceOf to determine if an Object is an array and then iterate over that array?

How to iterate over object

Should I use a for loop to iterate over my object's array?

How to correctly iterate over and object in array thats contained with in an array

Iterate over each object In array

Iterate over rails object and array

Iterate over an object array in JS

Iterate over an array or object with underscore

How can I iterate over an Array object by using For IN or FOR OF

how to iterate over object array based on values, not key?

How do you iterate over an array that is referenced by Object?

How to iterate over an array stored in an object using ng-repeat

How to convert array into object in and iterate over list in angular 5?

How to manually iterate over array/object without `foreach` and without `for` with PHP

How to iterate over an array of object then remove each element in JavaScript

How do I iterate over an array in a nested json object in sqlite?

How to iterate over JSON object collection (not array) in Perl?

How to iterate over an array and constructing an object from the results of a PostgreSQL query

How do you iterate over an array of objects to create a new object?

How to use Promise.all iterate over object keys

How to iterate over a JavaScript object?

How to iterate over this javascript object?

How to iterate over a JSON object?