Sorting observableArray with added item Knockout JS

user1535191

I'm trying to get my observableArray to alphabetically sort with the addition of a new item to the list when clicking "sort". Can anyone shed some light on what i'm doing wrong here?

Thank you,

Jason

........................

https://jsfiddle.net/jaloomis111/tssLxbo0/

<input data-bind="value: inputName, valueUpdate: 'afterkeydown'" type="text"/>
<ul data-bind="foreach: myFamily"/>
    <li data-bind="text: name"></li>
</ul>
<button data-bind="click: addItem">add item</button>
<button data-bind="click: sortArray">sort array</button>

$(function(){

var data = [
    {name: 'Jason'}, 
    {name: 'Alexi'},
    {name: 'Gabi'}
]; 


viewModel = {
        inputName: ko.observable(""),
    myFamily : ko.observableArray(data),
    //addItem : function(){
      //this.myFamily.push({name: this.inputName()});
      //this.inputName("");
    //},
    addItem : function() {
            this.myFamily.push({name: this.inputName()});
    },

    removeItem : function(){
        this.myFamily.pop();
    },

    sortArray : function(){
       this.myFamily.sort(function (left, right) { 
            return left.name == right.name ? 0 : (left.name < right.name ? -1 : 1) 
       })
    },

};

    ko.applyBindings(viewModel);

});
Jason Spake

The sort function does not operate on the array that is passed it, but instead returns a new sorted version of the array. So to update your observable with the sorted result you need to set its contents to the sort result.

sortArray : function(){
    this.myFamily(this.myFamily().sort(function (left, right) { 
        return left.name.toLowerCase() == right.name.toLowerCase() ? 0 : (left.name.toLowerCase() < right.name.toLowerCase() ? -1 : 1)
    }));
}

Currently your sort function is case sensitive, and I'm guessing you don't want that either. Capitalized names would always float up above non capitalized names.

Edit: Whoops, you're using knockout's .sort() on the observable not javascript's sort on the array so you can disregard the first part of my answer. It's probably just the casing thing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Knockout observableArray - added item not shown in view

From Dev

Knockout: Update item in an observableArray

From Dev

Knockout move observableArray item down

From Dev

knockout.js: No concat on observableArray

From Dev

Custom filtering knockout js observableArray

From Dev

Knockout.js contenteditable observablearray

From Dev

knockout.js: No concat on observableArray

From Dev

Update ObservableArray - Knockout.js

From Dev

Knockout JS failed to update observableArray

From Dev

Knockout js observableArray is not getting updated

From Dev

cannot remove knockout observableArray item with SingalR

From Dev

Knockout update observableArray item does not reflect in UI?

From Dev

Knockout.js - Registering to change in observableArray

From Dev

Knockout JS - Modifying properties of items in observableArray

From Dev

Knockout.js - Registering to change in observableArray

From Dev

knockout.js observableArray computed value

From Dev

knockout.js observableArray changes not reflecting in UI

From Dev

Knockout removing item from observableArray throwing error in IE8

From Dev

HTML table not updating, after pushing new item to knockout observableArray

From Dev

How to disable a button when an observableArray only has one item in Knockout

From Dev

Knockout-Kendo dropdownlist Ajax observableArray get selected item name

From Dev

Removing new item from knockout observablearray not updating kendo sortable

From Dev

How to disable a button when an observableArray only has one item in Knockout

From Dev

Knockout - How to reflect changes of selected item in an observableArray when manually subscribing

From Dev

Sorting a table in Knockout JS with arrows

From Dev

knockout: Trouble removing a newly added item

From Dev

Can not find index form observablearray knockout.js

From Dev

knockout.js validation on observableArray to require all items (no empty fields)

From Dev

Sorting an observablearray contained within an observablearray

Related Related

  1. 1

    Knockout observableArray - added item not shown in view

  2. 2

    Knockout: Update item in an observableArray

  3. 3

    Knockout move observableArray item down

  4. 4

    knockout.js: No concat on observableArray

  5. 5

    Custom filtering knockout js observableArray

  6. 6

    Knockout.js contenteditable observablearray

  7. 7

    knockout.js: No concat on observableArray

  8. 8

    Update ObservableArray - Knockout.js

  9. 9

    Knockout JS failed to update observableArray

  10. 10

    Knockout js observableArray is not getting updated

  11. 11

    cannot remove knockout observableArray item with SingalR

  12. 12

    Knockout update observableArray item does not reflect in UI?

  13. 13

    Knockout.js - Registering to change in observableArray

  14. 14

    Knockout JS - Modifying properties of items in observableArray

  15. 15

    Knockout.js - Registering to change in observableArray

  16. 16

    knockout.js observableArray computed value

  17. 17

    knockout.js observableArray changes not reflecting in UI

  18. 18

    Knockout removing item from observableArray throwing error in IE8

  19. 19

    HTML table not updating, after pushing new item to knockout observableArray

  20. 20

    How to disable a button when an observableArray only has one item in Knockout

  21. 21

    Knockout-Kendo dropdownlist Ajax observableArray get selected item name

  22. 22

    Removing new item from knockout observablearray not updating kendo sortable

  23. 23

    How to disable a button when an observableArray only has one item in Knockout

  24. 24

    Knockout - How to reflect changes of selected item in an observableArray when manually subscribing

  25. 25

    Sorting a table in Knockout JS with arrows

  26. 26

    knockout: Trouble removing a newly added item

  27. 27

    Can not find index form observablearray knockout.js

  28. 28

    knockout.js validation on observableArray to require all items (no empty fields)

  29. 29

    Sorting an observablearray contained within an observablearray

HotTag

Archive