-1
var x=ko.observableArray([....]);
x.removeAll();

Situation is 'x' is holding the selected values from dropdowns which are dynamically generated using knockout. I want to clear all the selections made by the user.

The above code is not working in my situation, so i have to 'undefine' each element in the array using a for loop - which is a very bad way(i feel). i want to know if there any better way of doing this?

I also tried this:

x([]);

Which didn't work either.

My code Looks something like this

 var data_Main = [
{[a,b,c] },{[d,e,f]},{[g,h,i]}
 ];
 var selectedKoArray= ko.observableArray([]);
 var Lst=ko.observableArray([
 {Txt: 'aaa', Val: 'a'},
 {Txt: 'bbb', Val: 'b'},
 {Txt: 'ccc', Val: 'c'}
 ]);

 var dataArray = ko.observableArray([Lst:Lst(), Lst:Lst1(), Lst:Lst2()]);

 var selectedHFilters = ko.observableArray([]);

 for (var i = 0; i < dataArray.length - 1; i++) {
                    if (selectedKoArray[i])
                        selectedKoArray[i] = undefined;
                }

-----------------------------------------------------HTML-------------------------------
<div data-bind="foreach:dataArrayKO ">
<select  class="select2-offscreen filter-select" data-bind="options:Lst, optionsText: 'Txt', optionsValue: 'Val', optionsCaption: '-Select-', value:$root.selectedHFilters[$index()]"></select>             
</div>
nikhil
  • 203
  • 1
  • 10
  • [removeAll() maybe?](http://stackoverflow.com/questions/18638507/how-to-clear-contents-of-an-observablearray-that-was-populated-from-previous-vis) – Sterling Archer May 01 '15 at 16:09
  • i tried both 'remove' and 'removeAll' – nikhil May 01 '15 at 16:10
  • 4
    Can you provide enough (*minimal*) HTML and JavaScript to reproduce the problem in your question? – David Thomas May 01 '15 at 16:13
  • In the script which you're developing, could you add `console.log(typeof x);` to ensure that `x` *is* an observableArray at the point where you're calling `removeAll`? Perhaps it gets modified between the declaration and the call to `removeAll`. Also, how do you know that it's not working? Are there still elements in the observable array or is there an exception? – awj May 01 '15 at 18:03
  • @David i know you need that but it is very difficult to create all the same environment – nikhil May 01 '15 at 18:32
  • @awj I traced each and every step in debugger... still can't find any error. – nikhil May 01 '15 at 18:33
  • As a last resort, you can always just re-initialise the observable: `x = ko.observableArray([...]);` – awj May 01 '15 at 19:10

1 Answers1

1

This is not valid Javascript and will error, which may be the root of your problem:

var dataArray = ko.observableArray([Lst:Lst(), Lst:Lst1(), Lst:Lst2()]);

I suspect you wanted something like this (though Lst(), Lst1() and Lst2() are not defined in the code you've supplied, again this will error).

var dataArray = ko.observableArray([Lst.Lst(), Lst.Lst1(), Lst.Lst2()]);

The KO code for removing elements you have supplied is fine.

garryp
  • 5,508
  • 1
  • 29
  • 41