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

        Knockout binding iteration

        65 Views Asked by Jayamurugan At 13 July 2016 at 05:23 2025-12-05T23:19:56.986000

        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

        • Sort oberservableArray by multiple parameters
        • change max-height by clicking collapse button
        • J-Query Context Menu updating via Knockout call
        • which function will be consider as view model in knockout js
        • Change CSS of element in after Move event of Knockout sortable
        • How to call a nested function from out side knockout js
        • KO binding is not working in IE10 and IE11
        • Mapping not working with api call
        • Issue with Syncfusion ejGrid and knockout.js when displaying summary rows
        • Using Knockoutjs Mapping Plugin inside viewmodel constructor
        • Visible bind not updating after Observable change with Knockout
        • Knockout computed observable boolean that uses an ajax call not returning boolean on sucess
        • Passing a constant or parameter to the data-bind click function in KO.js
        • How do I access my viewmodel variables in my knockout component template?
        • Loop viewmodel display pattern images on select change

        Related Questions in KNOCKOUT-3.0

        • Loop viewmodel display pattern images on select change
        • How to hide role based section in knockout component using asp.net MVC
        • Knockout performance issues with arrays having dynamic columns
        • Passing $index and $data as arguments to function for click handler
        • knockout foreach add row number to grid
        • Animate a reused view on click
        • How to create a pop up with Knockout?
        • Knockout.js text binding in child node
        • Knockout How can I subscribe to another view model's properties?
        • knockoutjs make selectable items onclick within foreach
        • Knockout: Change CSS visibility based on observable value
        • knockoutjs 3.4 cannot apply binding to the same element again
        • knockout.js, knockout-sortable, and default text
        • Typeahead.js not working in Knockout 3 foreach binding
        • How might I cancel a change to an observable array w/ Knockout 3.0?

        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