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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:05:01+00:00 2026-06-19T03:05:01+00:00

In my view I am looping through an observableArray (itemGroup) that has one property

  • 0

In my view I am looping through an observableArray (itemGroup) that has one property that is also an observableArray (item). I have a method to remove an entire itemGroup and one to remove an item from and itemGroup but I would like to add in some logic along the lines of it there is only 1 item left in the group removing that item should also remove the itemGroup.

here is an example of the relevant parts of my view model and view.

my JS

var ItemModel = function(item) {
   var self = this;
   self.name = ko.observable(item.name);
   self.price = ko.observable(item.price);
};

var ItemGroupModel = function(itemGroup) {
   var self = this;
   self.order = ko.observable(itemGroup.order);
   self.items = ko.observableArray(ko.utils.arrayMap(itemGroup.items, function(item){
      return new ItemModel(item);
   }));
   self.type = ko.observable(item.type);

   self.removeItem = function(item) {
      self.items.remove(item);
   }
};

var ViewModel = function(data) {
   var self = this;
   self.itemGroups = ko.observableArray(ko.utils.arrayMap(data.itemGroups, function(itemGroup) {
     return new ItemGroupModel(item);
   }));

  // some other properties and methods
   self.removeItemGroup = function(itemGroup) {
     self.itemGroups.remove(itemGroup);
  }
};

My View

<ul data-bind="foreach: {data: VM.itemGroups, as: 'itemGroup'}">
   <li>
       <button data-bind="click: $root.VM.removeItemGroup">X</button>
       <ul data-bind="foreach: {data: itemGroup.items, as: 'item'}">
          <li>
             <!-- ko if: itemGroup.items().length > 1 -->
             <button data-bind="click: itemGroup.removeItem">X</button>
             <!-- /ko -->
             <!-- ko ifnot: itemGroup.items().length > 1 -->
             <button data-bind="click: function () { $root.VM.removeItemGroup($parent) }">X</button>
             <!-- /ko -->
           </li>
       </ul>
   </li>
</ul>

This works but to me it isnt ideal. It is my understanding that knockout should help me get away from using an anonymous function like “function () { $root.VM.removeItemGroup($parent) }” but I am not sure how to do it another way. Also removing the if and ifnot statements would be good to clean up as well.

  • 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-19T03:05:02+00:00Added an answer on June 19, 2026 at 3:05 am

    I would like to give my solution

    send index of itemGroups and items as argument to remove method.

    Hope you know how to send index

    Then check the length of itemGroups

    self.remove(itemGroupsIndex,itemsIndex) {
     var itemGroupsLength = self.itemGroups()[itemGroupsIndex].items().length;
      if(itemGroupsLength = 1) {
    
       self.itemGroups.remove(itemGroupsIndex);
    
      }
      else {
    
       self.itemGroups()[itemGroupsIndex].items.remove(itemsIndex);
    
      }
     };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a partial view that has textfields in it and is bound to
I am looping through a array and adding UIImageViews, the array has a have
In SSJS I'm looping through document in a view and I want to add
I am having a problem while looping through one of my loop constructions. I
I have a very screwed up view system going on. I go through a
I'm looping through two products - on the post view page I pull in
I am looping through an object passed to the View (I'm using ASP.NET MVC
I am looping through the posted values on a form with a view to
I have some javascript on a view that makes an ajax call to my
I have a view in my codeigniter app that gets an array from the

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.