DEVHIDE
        • Home (current)
        • About
        • Contact
        • Cookie
        • Home (current)
        • About
        • Contact
        • Cookie
        • Disclaimer
        • Privacy
        • TOS
        Login Or Sign up

        Knockout binding iteration

        67 Views Asked by Jayamurugan At 13 July 2016 at 05:23 2025-12-13T05:18:07.410720

        I am having a scenario where based on the Observable flag I need to iterate ABC and XYZ

        Current Code

        <div  data-bind="visible: isEnabled">
                   <ul data-bind="foreach: relatedObservalableArray">
                       <li>
                       <!-- Hundred lines of code>
                       </li>
                    </ul>
        </div>
        
        
        <div data-bind="visible: !isEnabled">
                <ul data-bind="foreach: unRelatedObservalableArray">
                       <li>
                        <!-- Same Hundred lines of code>
                       </li>
                    </ul>
           </div>
        

        I am feeling code duplicate.

        Is any way I can group the two html functions to a single one?

        I want to change only in HTML part due to some other ...

        I am new to Knockout. Can someone help me?

        knockout.js knockout-3.0 knockout-binding-handlers
        Original Q&A
        1

        There are 1 best solutions below

        0
        Adrian Adrian On 13 July 2016 at 05:34 BEST ANSWER

        Sure you can use the knockout template binding for this. Read it more here.

        So for the changes it would be something like below but you can do variations basing on what you read from the link I gave.

        <script type="text/html" id="template-name">
           Hundred lines of code...
        </script>
        
        <div  data-bind="visible: isEnabled">
          <ul data-bind="foreach: relatedObservalableArray">
            <li data-bind="template: 'template-name'">
            </li>
          </ul>
        </div>
        
        
        <div data-bind="visible: !isEnabled">
          <ul data-bind="foreach: unRelatedObservalableArray">
             <li data-bind="template: 'template-name'">
             </li>
          </ul>
        </div>
        

        Related Questions in KNOCKOUT.JS

        • Force Magento 2's checkout accordion to be open on first step, closed on second step
        • Unable to process binding "value: function() {return myVar }"
        • I am getting Failed to load resource: the server responded with a status of 500 (Internal Server Error) - intermittently in Knocout/MVC app
        • Knockout Computed field never updates UI
        • Add values in form input in Javascript (Chrome)
        • Magento 2: wrong shipping method saved in order
        • How to add multiple method titles to KnockoutJS ifnot statement in Magento2
        • Uncaught TypeError error in Chrome for multiple file upload
        • Apexcharts <svg> attribute height: Expected length, "NaN" when loading other page
        • Is toString() JavaScript output "guaranteed" to be the same in every browser/implementation?
        • How does the knockout rendering for imagepicker work in SurveyJS Survey Creator
        • Knockout.js - Subscription to observable selected value in cascading select list firing twice
        • Malware in Knockoutjs context debugger
        • Magento 2 Minicart / Knockout.js: Format calculated price / sum
        • Magento 2: How to open a modal for each related product using UI component?

        Related Questions in KNOCKOUT-3.0

        • MathJax and Knockout.js ko.bindingHandlers
        • Using knockout extenders to return data not tied to the underlying observable?
        • How to update object attributes values in knockout.js at once
        • Knockoutjs star rating css
        • Linq EF6 Merging two lists
        • Is there a function in Knockout.js to build an object from a data-bind attribute string?
        • knockout: unable to update the observable array in knockout while binding to table
        • UI is not updated by updating observable from the custom binding method update
        • Knockout JS writable computed value doesn't fire another computed value when writing function is used
        • Is there a way to asynchronous initialize a Viewmodel (KnockoutJS)
        • Behavior Subject Knockout Observable Performance
        • How to pass an item from knockout foreach to partial view as data-bind?
        • Preventing one image misaligning another in HTML/CSS
        • view does not update - css class missing
        • Loading Angular component in a Knockout application - Angular elements

        Related Questions in KNOCKOUT-BINDING-HANDLERS

        • Why did changing this formatting function in this way, cause the Knockout binding to start working?
        • How can I disable/enable a drop down embedded in a table mapped to a knockout observable array
        • Create HTML based on number in array
        • Two-dimensional knockout sortable not updating UI
        • knockout toggle/expand the text on click of text
        • knockout databind jquery datatable to add a row
        • Knockout custom binding update not working
        • KnockoutClassBindingProvider: How to perform a foreach binding
        • knockout optionsText / optionsValue bindings won't work
        • Format phone number as you type (xxx) xxx - xxxx
        • Update chosen-select knockout binding whenever changes are made to another knockout binding
        • Passing an unknown function in a knockout binding (foreach) - is it possible?
        • Knockoutjs custom bindinghandlers methods not firing
        • Knockout binding iteration
        • Knowing where observable lives in root model in foreach/descendant bindings in knockout

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

        Popular # Hahtags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?
        .

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Math
        • Aftereffectstemplates