0

I am using the following condition and its print one value,

if(self.options.nearestlocationsBlock) {
    var firstStore = _.first(locations.items);

    var newContent = '<div class="store"> <div class="zipcode">'+firstStore.address+','+firstStore.city+','+firstStore.state+','+firstStore.zip+'</div></div>';     

    self.storeListIdentifier.html(newContent);
} 

I want to print the next three values, how to do that?

eg: I am print first value, i want to print next three values in second condition, how to do that?

Workout:

I am trying following script, its return but keep page loading,

  if(self.options.nearestlocationsBlock) {
                    var firstStore = locations.items;

                    firstStore.foreach(function(firstStore){
                        var newContent = '<div class="store"> <div class="zipcode">'+firstStore.address+','+firstStore.city+','+firstStore.state+','+firstStore.zip+'</div></div>';                    
                        self.storeListIdentifier.html(newContent);
                    }); 

1 Answer 1

0

You're trying to achieve this in the same way you would with jQuery which is not how it works with KO.

You should do it like so.

Your JS file, you'd pull this from your backend but I'll hardcore to help visualise it:

const stores = [
    {
        address: 'Address here',
        city: 'City here'
    },
    {
        address: 'Address 2 here',
        city: 'City 2 here'
    },
]

Then within your HTML template:

<div data-bind="foreach: stores">
    <div data-bind="text: address"></div>
    <div data-bind="text: city"></div>
</div>

I advise you go through the KO documentation - https://knockoutjs.com/documentation/foreach-binding.html

2
  • I am working on js file, how to add the above thing.
    – zus
    Apr 16, 2021 at 9:55
  • That depends on your backend, I can't answer that one for you.
    – Ben Crook
    Apr 16, 2021 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.