Last active
November 30, 2016 19:59
-
-
Save earlbread/30f0eeb123759e1d81f32e5c1535da62 to your computer and use it in GitHub Desktop.
Knockout code for search text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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