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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:15:56+00:00 2026-06-03T06:15:56+00:00

In my viewmodel, I have a knockoutjs ObserableArray. Just after I initialized the ViewModel,

  • 0

In my viewmodel, I have a knockoutjs ObserableArray. Just after I initialized the ViewModel, it binds the data successfully. Then, what I need to do is to sort the collection.

$.each(vm.searchResults(), function (i, property) {
    console.log(property.Name());
});

vm.searchResults().sort(function (a, b) {
    return a.Name().toLowerCase() > b.Name().toLowerCase() ? 1 : -1;
});

$.each(vm.searchResults(), function (i, property) {
    console.log(property.Name());
});

As you can see, I output the Name of the element to the console to see the order before and after the sorting. The sorting works just fine. The problem is with the UI update. Somehow, the UI is not updated.

Then, try to remove a record from the array with the following code to see if the UI will respond to that or not:

vm.searchResults().shift();

The UI stays the the same and didn’t update again. What would be the problem here?

Edit:

Here is a sample case as well: http://jsfiddle.net/tugberk/KLpwP/

The same problem here as well.

Edit:

I solved the problem as shown in this sample: http://jsfiddle.net/tugberk/KLpwP/16/ But I am still not sure why it worked as I tried at first.

  • 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-03T06:15:58+00:00Added an answer on June 3, 2026 at 6:15 am

    You’re unwrapping observable array when you are going to sort it. This is causes problem, because KO can’t track array was changed. ko.observableArray() has sort function with same signature and it will notify observable’s subscribers that it has been changed. Solution is very simple: remove braces vm.searchResults().sort => vm.searchResults.sort. Checkout my example: http://jsfiddle.net/RbX86/

    Another way to tell KO that array has changes – call valueHasMutated method after manipulations with array. Please review this sample: http://jsfiddle.net/RbX86/1/
    This approach is very good when you need to make many changes in your array and you want to refresh UI only one time.

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

Sidebar

Related Questions

I have just started playing with KnockoutJS and it it fascinating. I successfully created
For instance, if I have: <div data-bind=visible: viewModel.property1() || viewModel.property2() /> ... if viewModel.property1()
I have a knockoutjs viewmodel defined like this: function TestViewModel() { var self =
How can i have model in knockoutjs that gets its data from an sqlite
I have a Knockout Viewmodel that uses the mapping plugin. After mapping the JSON
I have just started with knockout this Model and viewmodel: $(function() { // Class
I have a user control let say UC1 . This user control have viewmodel
I am trying to implement Repository pattern in JavaScript. I have ViewModel which i
Hi people stackoverflow. I'm working with MVVM, I have ViewModel call UserViewModel with a
I have a ViewModel class that contains a list of points, and I am

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.