Knockout : foreach data not display in HTML

2019-09-21 06:38发布

问题:

I want to display multi dimentional ko observable array data in html. But, i didn't get output.

My code :

<!-- ko if: ($parent.cust_opt_avail() === 1) -->
<!-- ko foreach: $parent.customVal() -->
<div class="product-custom-option-select">
    <p class="options-label" data-bind="text:key"></p>
    <p class="options-label" data-bind="text:custom_option_select_text"></p>
</div>
<!-- /ko -->
<!-- /ko -->

cust_opt_avail() is ko observable variable. customVal is ko observable array.

output of customVal is :

I want to display custom_option_select_text and display key name on first p tag.

How to do it ?

Expected Result :

please help me.

回答1:

From your previous question and comments in this question, I gather you're setting an object to ko.observableArray(). This is not correct. You should set a customVal to a ko.observable(). Then use Object.keys() and use aliasing in your foreach binding.

var viewmodel = function() {
  var self = this;
  self.cust_opt_avail = ko.observable(1);
  
  let customVal = {
    Color: [{'custom_option_select_text': 'Red + $200.00'}, 
            {'custom_option_select_text': 'Green + $250.00'}],
    Size: {'custom_option_select_text': 'XL + $150.00'}
  };
  
  // This should be an observable
  self.customVal = ko.observable(customVal);
};

ko.applyBindings(new viewmodel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<!-- ko if: (cust_opt_avail() === 1) -->
<div data-bind="foreach: { data: Object.keys(customVal()), as: 'key' }">
  <div class="product-custom-option-select">
    <p style="font-weight:bold" data-bind="text:key"></p>

    <!-- ko if: Array.isArray($parent.customVal()[key]) -->
    <!-- ko foreach: $parent.customVal()[key] -->
       <p class="options-label" data-bind="text:custom_option_select_text"></p>
    <!-- /ko -->
    <!-- /ko -->
  
  <!-- ko if: !Array.isArray($parent.customVal()[key]) -->
  <p class="options-label" 
    data-bind="text:$parent.customVal()[key].custom_option_select_text"></p>
  <!-- /ko -->
</div>
</div>
<!-- /ko -->

NOTE:

Since customVal is in a nested context, you might have to add another $parent prefix to all the inner bindings.



回答2:

Interesting question! So you want to do a for loop through customVal(), but customVal() itself has arrays. In this case it is useful to know about Knockout binding context. Particularly $data. You can use it as a reference to the current context that you're in, and not worry about the names like Color and Size.

Once you use $data as a placeholder for Color and Size arrays, do a for loop through them as well. I've created a snippet:

var viewmodel = function(){
  var self = this;
  self.cust_opt_avail = ko.observable(1);
  var Color = [{'custom_option_select_text': 'Red + $200.00'},
                    {'custom_option_select_text': 'Green + $250.00'}];
  var Size = {'custom_option_select_text': 'XL + $150.00'};
  var customValArray = [Color, Size];
  self.customVal = ko.observableArray(customValArray);
};


ko.applyBindings(new viewmodel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<!-- ko if: (cust_opt_avail() === 1) -->
<div data-bind="foreach: customVal()">
  <!-- ko if: Array.isArray($data) -->
    <!-- ko foreach: $data -->
    <div class="product-custom-option-select">
        <p class="options-label" data-bind="text:custom_option_select_text"></p>
    </div>
    <!-- /ko -->
  <!-- /ko -->
  <!-- ko ifnot: Array.isArray($data) -->
    <div class="product-custom-option-select">
        <p class="options-label" data-bind="text:custom_option_select_text"></p>
    </div>
  <!-- /ko -->
</div>
<!-- /ko -->