admin管理员组文章数量:1289889
I try to search by name in observable array. Here's my code:
<input class="form-control" data-bind="value: Query, valueUpdate: 'keyup'" autoplete="off">
And my code in ViewModel
viewModel.Query = ko.observable('');
viewModel.search = function(value) {
viewModel.TestList.removeAll();
for (var x in viewModel.TestList) {
if (viewModel.TestList[x].Name.toLowerCase().indexOf(value.toLowerCase()) >= 0) {
viewModel.TestList.push(viewModel.TestList[x]);
}
}
}
viewModel.Query.subscribe(viewModel.search);
First: I would like to search by name string. Two: Is there any other sollutions to not remove all elements from the view? I mean when query string is empty, there should be all list again.
Now I have error message:
TypeError: viewModel.TestList[x].Name is undefined
I try to search by name in observable array. Here's my code:
<input class="form-control" data-bind="value: Query, valueUpdate: 'keyup'" autoplete="off">
And my code in ViewModel
viewModel.Query = ko.observable('');
viewModel.search = function(value) {
viewModel.TestList.removeAll();
for (var x in viewModel.TestList) {
if (viewModel.TestList[x].Name.toLowerCase().indexOf(value.toLowerCase()) >= 0) {
viewModel.TestList.push(viewModel.TestList[x]);
}
}
}
viewModel.Query.subscribe(viewModel.search);
First: I would like to search by name string. Two: Is there any other sollutions to not remove all elements from the view? I mean when query string is empty, there should be all list again.
Now I have error message:
TypeError: viewModel.TestList[x].Name is undefined
Share
Improve this question
asked Apr 16, 2015 at 6:40
mskuratowskimskuratowski
4,12418 gold badges65 silver badges116 bronze badges
5
-
you need an
if(value) removeAll()
guard – dandavis Commented Apr 16, 2015 at 6:44 -
this should work try this
viewModel.TestList()[x].Name
– super cool Commented Apr 16, 2015 at 6:48 - try this which works fine jsfiddle/LkqTU/23731 . cheers – super cool Commented Apr 16, 2015 at 7:20
- @cheers I have an error mesage: TypeError: item.name is undefined if (item.name.toLoweCase().indexOf(value.trim().toLowerCase()) > -1) { – mskuratowski Commented Apr 16, 2015 at 7:41
- 1 i see nothing can you share how you reproduced it ? – super cool Commented Apr 16, 2015 at 8:39
1 Answer
Reset to default 8Use a puted observable array to show search results, along these lines:
var viewModel = {
items: [ { Name: "Apple part" }, { Name: "Apple sauce" }, { Name: "Apple juice" }, { Name: "Pear juice" }, { Name: "Pear mush" }, { Name: "Something different" } ]
};
viewModel.Query = ko.observable('');
viewModel.searchResults = ko.puted(function() {
var q = viewModel.Query();
return viewModel.items.filter(function(i) {
return i.Name.toLowerCase().indexOf(q) >= 0;
});
});
ko.applyBindings(viewModel);
<script src="https://cdnjs.cloudflare./ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input class="form-control" data-bind="value: Query, valueUpdate: 'keyup'" autoplete="off">
<h3>Search results:</h3>
<ul data-bind="foreach: searchResults">
<li data-bind="text: Name"></li>
</ul>
<h3>All items:</h3>
<ul data-bind="foreach: items">
<li data-bind="text: Name"></li>
</ul>
This also removes the need for a subscription or seperate function.
This example utilizes:
- A regular
observableArray
for storing allitems
(this list is always the same, regardless of your search query); - A read-only puted observable which returns a filtered array of items that match your query;
- The array
filter
method (you call it on theobservableArray
, but KO just forwards it to the underlying array);
As you can see in the example, items
will always contain all objects, and searchResults
is just a filtered read-only view on that array.
本文标签: javascriptKnockout search in observable arrayStack Overflow
版权声明:本文标题:javascript - Knockout search in observable array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741404003a2376835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论