Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:58:46+00:00 2026-06-05T19:58:46+00:00

I want to build my own listView with selectedItem and Itemsource. I have started

  • 0

I want to build my own listView with selectedItem and Itemsource.

I have started a jsFiddle http://jsfiddle.net/andersb79/53xRL/1/

Can you please help me in the right direction about how I should build this.
I havn’t seen a bindingHanlder that do this.

What I want is something like this.

data-bind="myListView : { items : comments, selectedItem : selectedComment}"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:58:48+00:00Added an answer on June 5, 2026 at 7:58 pm

    In your custom binding you need to:

    1. Create an element for each of the items in the list
    2. Append that element to the parent
    3. Add a click handler to each element that sets the selectedItem on the original view model

    I’ve also highlighted the current selected item for clarity.

    ko.bindingHandlers.myListView = {
        update: function(element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor()),
    
                //get the list of items
                items = value.items(),
                //get a reference to the selected item observable
                selectedItem = value.selectedItem,
                //get a jQuery reference to the element
                $element = $(element),
                //get the currently selected item
                currentSelected = selectedItem();
    
            //clear the parent of any existing children
            $element.html("");
    
            for (var index = 0; index < items.length; index++) {
                (function() {
                    //get the list of items
                    var item = ko.utils.unwrapObservable(items[index]),
    
                        //create a child element with a click handler
                        $childElement = $("<li>")
                        .text(item.id() + " " + item.text())
                        .click(function() {
                            //remove selected class on all siblings
                            $(this).siblings().removeClass("selected");
                            //add selected class on this item
                            $(this).addClass("selected");
                            //set the observable 'selected item' property on the source view model
                            selectedItem(item);
                        });
    
                    //add the selected class if this item is the current selected item
                    if (item == currentSelected) {
                        $childElement.addClass("selected");
                    }
    
                    //add the child to the parent
                    $element.append($childElement);
                })();
            }
    
        }
    };
    

    Here’s a working example

    Note: As I am using the update instead of the init method it will work when updating the list of comments

    Update

    If you want to use the contents of the original DIV as the template for each created item, you need two additional steps:

    1. Grab the original content of the element when initialized and use that in place of the hard-coded content above
    2. Apply bindings to the created element before adding it to the DOM

          ko.bindingHandlers.myListView = {
      init: function(element) {
          var $element = $(element),
              originalContent = $element.html();
      
          $element.data("original-content", originalContent);
          return { controlsDescendantBindings: true }
      },
      update: function(element, valueAccessor) {
          var value = ko.utils.unwrapObservable(valueAccessor()),
      
              //get the list of items
              items = value.items(),
              //get a reference to the selected item observable
              selectedItem = value.selectedItem,
              //get a jQuery reference to the element
              $element = $(element),
              //get the currently selected item
              currentSelected = selectedItem(),
              //get the current content of the element
              elementContent = $element.data("original-content");
      
          $element.html("");
      
          for (var index = 0; index < items.length; index++) {
              (function() {
                  //get the list of items
                  var item = ko.utils.unwrapObservable(items[index]),
      
                      //create a child element with a click handler
                      $childElement = $(elementContent)
                      .click(function() {
                          //remove selected class on all siblings
                          $(this).siblings().removeClass("selected");
                          //add selected class on this item
                          $(this).addClass("selected");
                          //set the observable 'selected item' property on the source view model
                          selectedItem(item);
                      });
      
                  ko.applyBindings(item, $childElement[0]);
      
                  //add the selected class if this item is the current selected item
                  if (item == currentSelected) {
                      $childElement.addClass("selected");
                  }
      
                  //add the child to the parent
                  $element.append($childElement);
              })();
          }
      
      }
      

      };

    Note that we have to prevent the content of the div being processed by knockout by returning { controlsDescendantBindings: true } from our init method.

    Here is an updated example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build my own JVM and can't find any instructions for doing
I want to build flash application that can detect the user eyes color and
I want to build a basic Client-Server application, where my android smartphone can stream
I want to build a few Nagios plugins of my own and wanted to
I want to build my own plugins in Jquery. So i want to know
I want to build my own carousel for learning purposes. But I'm curious how
I want to build an e-store with oscommerce where multiple stores(good sellers I have
I want to build my own menu from the beginning so to be able
I want to build my own web framework, and I want to build it
I want to build my own class similar to a String class that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.