Lindomar Martins
Lindomar Martins

Reputation: 31

How to use the knockoutJS foreach in Magento 2

I am on a great fight with knockoutJS and Magento 2. I am enjoy it because I know i will learning something new and useful. At the moment, I am trying to use the foreach. I got examples on magento core files and from here but I am still missing something. Here is my code:

the file.html

<div class="component-wrapper">
<ul>
<!-- ko foreach: { data: items, as: 'item' } -->
    <li data-bind="text: item.code"></li>
<!-- /ko -->
</ul>


<span data-bind="text: getTitle()"></span>

the file.js:

define([
    'ko',
    'jquery',
    'uiComponent',
    'Magento_Checkout/js/model/quote'
], function(ko, $, Component, quote) {
    'use strict';
    return Component.extend({


        getTitle : function() {
            return "My Function is here";
        },

        items : function(){
           const  items = [
                    {
                        id : '0001',
                        code : '123123'
                    },
                    {
                        id : '0001',
                        code : '123123'
                    }
                ];
            return items;
        },

        initialize: function () {
            console.log(this.items());
            this._super();
        },

        isActive : function(){
            return true;
        },

    });
});

printscreen on the frontend:

enter image description here

I am trying to show the Items on frontend. Someone could you save me here :S :D

thanks

Upvotes: 1

Views: 2956

Answers (1)

Lindomar Martins
Lindomar Martins

Reputation: 31

I got it.

I was missing the "()"

Upvotes: 1

Related Questions