Skip to content

Instantly share code, notes, and snippets.

@earlbread
Last active November 30, 2016 19:59
Show Gist options
  • Save earlbread/30f0eeb123759e1d81f32e5c1535da62 to your computer and use it in GitHub Desktop.
Save earlbread/30f0eeb123759e1d81f32e5c1535da62 to your computer and use it in GitHub Desktop.
Knockout code for search text
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Search Results</title>
</head>
<body>
<input type="text" data-bind="textInput: searchText">
<div data-bind="foreach: searchResult">
<p data-bind="text: $data"></p>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
<script>
var ViewModel = function() {
var self = this;
this.searchText = ko.observable('');
this.data = ko.observableArray([
'dog',
'duck',
'lion',
'chicken'
]);
this.searchResult = ko.computed(function() {
return ko.utils.arrayFilter(self.data(), function(animal) {
return animal.includes(self.searchText());
});
});
};
ko.applyBindings(new ViewModel());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment