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

  • SEARCH
  • Home
  • 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 8984253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:03:16+00:00 2026-06-15T21:03:16+00:00

I am attempting to provide a mechanism for editing entries in an observable array.

  • 0

I am attempting to provide a mechanism for editing entries in an observable array. The display would have two sections. The first is the array entries displaying a limited number of fields, and the second would allow the user to edit all fields for a selected entry.

To do this, I provided a double click event for each displayed entry of the array, the event would use the index and a computed observable to select a slice of the array. The hope is that I can use this method to edit array entries.

The problem is that the computed does not seem to work, and I cannot find a method that does what I want. I created a fiddle illustrating my sorry attempt to make this work

http://jsfiddle.net/rscidmore/YrsCj/

Your help would be appreciated.

My code looks like this:

var contactModel = function() {
  var self = this;
  self.id = ko.observable();
  self.name = ko.observable();
  self.addresses= ko.observableArray();
  self.selectIndex = ko.observable(0);
  self.selectedAddress = ko.computed(function() {
    return self.addresses.slice(self.selectIndex ());
  });  
};
var addressModel = function(id, type, address) {
  var self = this;
  self.id       = ko.observable(id);
  self.type     = ko.observable(type);
  self.address  = ko.observable(address);
};


var contact = new contactModel();
contact.id = 1;
contact.name = 'John Smith';
var addr = new addressModel('1', 'billing', '123 Your Street')
contact.addresses.push(addr);
addr = new addressModel('2', 'shipping', 'ABC Your Avenue')
contact.addresses.push(addr);
addr = new addressModel('3', 'home', 'XYZ Your Drive')
contact.addresses.push(addr);


ko.applyBindings(contact);

And my html looks like this:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body class='ui-widget'>
    <div class='contactInfo'>
      <span class='id' data-bind="text: id"></span>  :
      <span class='dat1' data-bind="text: name"></span>   
    </div>
    <div class='container'>
       <!-- ko foreach: addresses -->       
         <div class='addrs' data-bind="event: { dblclick: function() {
           $parent.selectIndex($index());}}">
           <span class='id' data-bind="text: id"></span>  :
           <span class='dat1' data-bind="text: type"></span>
           <span class='dat2' data-bind="text: address"></span>
         </div>
       <!-- /ko -->
    </div>
    <div class='contactInfo'>
      <span class='id' data-bind="text: selectIndex"></span>  :
      <input class='dat2' type='text' data-bind="value: selectedAddress.address" />  
    </div>      
  </body>
</html>        
  • 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-15T21:03:18+00:00Added an answer on June 15, 2026 at 9:03 pm

    Typically what you would want to do is represent your “selected” item as an observable. Handlers hooked up through the event / click binding receive the current data item as the first argument. This can be used to populate your “selected” observable directly.

    So, it would be like:

    var ContactModel = function() {
      this.id = ko.observable();
      this.name = ko.observable();
      this.addresses= ko.observableArray();
      this.selectedAddress = ko.observable();    
    };
    

    Then, you can bind against it like:

    <div class='container'> 
       <!-- ko foreach: addresses -->       
         <div class='addrs' data-bind="event: { dblclick: $parent.selectedAddress }">
           <span class='id' data-bind="text: id"></span>  :
           <span class='dat1' data-bind="text: type"></span>
           <span class='dat2' data-bind="text: address"></span>
         </div>
       <!-- /ko -->
    </div>
    

    Note that you could create a function called “selectAddress” on your contact model and populate selectedAddress with the item passed as the first argument. However, since an observable is already a function and populates its value using the first argument passed to it, in the sample above I bound dblclick directly against the observable.

    Sample here: http://jsfiddle.net/rniemeyer/2DmUf/

    Sample with a function rather than bound directly against an observable here: http://jsfiddle.net/rniemeyer/mDKGV/ (just to help make it clear)

    A handy thing to do with a “selected” observable, is to use the with binding around an area, so it will re-render whenever you change to a new selected item and protect against when the item is null.

    <div class='contactInfo' data-bind="with: selectedAddress">
      <input class='dat2' type='text' data-bind="value: address" />  
    </div>    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are attempting to provide a service where two customer devices would need to
I'm attempting to provide a border for a select tag and have been pulling
I am attempting to provide user friendly error messages via my web application for
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files
I'm attempting to provide a confirmation link in my user welcome email and I'm
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
i have been attempting to pass an object into a function that belongs to
I am attempting to build an OS layer in a project, to provide OS-independent
So I'm attempting to do a query on a table that holds two foreign

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.