0
votes

The code below mostly works. The problem is that if I remove an item from the right box and add into left box (or vice versa) then list does not sort alphabetically.

I tried with sort() function. For example, self.catagories.sort(), however it didn't work. It always adds the item at the end. I need help for this solution.

var categoryModel = function(category) {
    this.category = ko.observable(category);
    };

 var viewModel=function(){
 var self=this;
     self.categories=ko.observableArray([new categoryModel("Hello"),new categoryModel("DHFDSHADS"),new categoryModel("yo"),new categoryModel("jai")]);

     self.selectedCategory=ko.observableArray();


 $('#add').click(function() {  
   var x= $('#select1 option:selected');
   if(x.length>0){
       x.each(function(){
           alert($(this).text());
        self.selectedCategory.push(new categoryModel($(this).text()));
        $('#select1 option:selected').remove();
       });
   }
});

 $('#remove').click(function() {  
  var x= $('#select2 option:selected');
   if(x.length>0){
       x.each(function(){
           alert($(this).text());
        self.categories.push(new categoryModel($(this).text()));
        $('#select2 option:selected').remove();
       });
   } 
 }); 

 };


$(document).ready(function() {  
  ko.applyBindings(new viewModel());

});  
.liveExample select[multiple] { width: 20%; height: 8em; }
.liveExample h2 { margin-top: 0.4em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script>
<div class='liveExample'> 
    <p>Your items:</p>
    <select multiple="multiple" id ="select1" width="5" data-bind="options: categories,optionsText:'category'"> </select> 

   <span> 
       <a href="#" id="add"> >> </a>
       <a href="#" id="remove"> << </a>    
    </span>

     <select multiple="multiple" id ="select2" width="5" data-bind="options: selectedCategory,optionsText:'category'"> </select> 
</div>
1

1 Answers

2
votes

You're fighting KO, not using it. I.e. you are removing options from the DOM, instead of removing them from the observableArrays, and you're handling clicks with jQuery instead of KO click bindings. If you change those things your current symptom will most likely disappear. I suggest running through the KO tutorials.

At any rate, here's how I'd tackle your issue:

var categoryModel = function(category) {
  this.category = ko.observable(category);
};

var viewModel = function() {
  var self = this;

  self.availableCategories = ko.observableArray([
    new categoryModel("Hello"),
    new categoryModel("DHFDSHADS"),
    new categoryModel("yo"),
    new categoryModel("jai")
  ]);
  self.usedCategories = ko.observableArray([]);
  self.selectedAvailableCategories = ko.observableArray([]);
  self.selectedUsedCategories = ko.observableArray([]);
  
  self.add = function() {
    self.selectedAvailableCategories().forEach(function(i) {
      self.availableCategories.remove(i);
      self.usedCategories.push(i);
    });
  };

  self.remove = function() {
    self.selectedUsedCategories().forEach(function(i) {
      self.usedCategories.remove(i);
      self.availableCategories.push(i);
    });
  };
};

ko.applyBindings(new viewModel());
pre { background: white; padding: 10px; color: #333; font: 11px consolas; border: 1px solid #ddd; }
select { width: 200px; }
a { display: inline-block; margin: 10px; cursor: pointer; border: 1px solid gold; padding: 5px 10px; text-decoration: none; }
a:hover { background-color: pink; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>

<select multiple="multiple" width="5" data-bind="options: availableCategories, optionsText: 'category', selectedOptions: selectedAvailableCategories"></select>
<span> 
  <a href="#" data-bind="click: add"> &rarr; </a>
  <a href="#" data-bind="click: remove"> &larr; </a>    
</span>
<select multiple="multiple" width="5" data-bind="options: usedCategories, optionsText:'category', selectedOptions: selectedUsedCategories"></select>