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 8514613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:55:06+00:00 2026-06-11T04:55:06+00:00

I’m sure this will be an easy answer for somebody. I have the following

  • 0

I’m sure this will be an easy answer for somebody. I have the following ViewModel:

@{
    var initialData = new JavaScriptSerializer().Serialize(Model);
}
var data = @Html.Raw(initialData);
function ViewModel(data) {
    var self = this;
    self.Name = ko.observable(data.Name);
    self.Items = ko.observableArray(data.Items);
    self.addItem = function() { self.Items.push(""); };
    self.removeItem = function(data) { self.Items.remove(data); }
}
$(document).ready(function() {ko.applyBindings(new ViewModel(data)); });

And the following View:

<div>
    Name: <span data-bind="text: Name"></span>
</div>
<div>
    Items: <button data-bind="click: addItem">Add Item</button>
</div>
<div>
    <table>
        <tbody data-bind="template: { name: 'itemTemplate', foreach: Items }"></tbody>
    </table>
</div>
<script type="text/html" id="itemTemplate">
    <tr>
        <td>
            <input data-bind="value: $data" />
            <a href="#" data-bind="click: function() {$parent.removeItem($data)}">Remove Item</a>
        </td>
    </tr>
</script>

Everything seems to work correctly except for removeItem. When new rows have been added, and “Remove Item” is clicked on an empty new row, all of the new rows will be removed with it. I’ve looked at tons of knockout tutorials trying to get this to work, and my method seems to be a valid attempt, but obviously… I must be missing something. Any suggestions?

  • 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-11T04:55:07+00:00Added an answer on June 11, 2026 at 4:55 am

    The remove function of an observableArray loops through the array and deletes any items that match the value passed to it. In your case, you are just dealing with strings and it will see that all of the new ones (with no value) will match “”.

    There are a couple ways to handle it:

    • you can deal with objects instead of just strings like { value: ko.observable("") }. Then, when you pass $data it will only delete the actual item that matches that object reference. If your value is not observable and is the data itself (not a property), then your writes won’t actually make it back to your view model.

    • if that does not work for your scenario, then you could delete items based on the index ($index) using splice.

    I would probably do it like: http://jsfiddle.net/rniemeyer/N3JaW/

    Also note that the event (click is a wrapper to event) binding will pass the current data as the first argument to the handler, so you can simplify your binding to click: $parent.removeItem.

    Update: here are several ways that you can control how your object is converted to JSON:

    • ko.toJSON passes its 2nd and 3rd arguments on to JSON.stringify. The second arg let’s you run a function to potentially replace values as described here. Here is a sample that checks to see if the key is a number (array item) and it has a value property. If so, it just returns the string rather than the object. http://jsfiddle.net/rniemeyer/N3JaW/1/

    • If you use a constructor function for your objects, then you can override the toJSON function as described here. Here is your sample with this functionality: http://jsfiddle.net/rniemeyer/N3JaW/2/

    • Another technique that can be used is to maintain a computed observable that has the “good” value. Here is sample: http://jsfiddle.net/rniemeyer/N3JaW/3/. In this one Items is a computed observable that returns the clean value. Items.asObjects contains the object version of your values. When converted to JSON, the asObjects part is naturally dropped when Items is converted to JSON. If you only need this “good” array when converting to JSON, then the other options are better for performance (they only are calculated when you want to send it).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.