I am using knockoutjs (very new to it) with jQuery Mobile. I have a listview to which I bind the filtering results. After I upload my data, the first time I have to call
$('ul').listview('refresh');
so that JQM redid my list, this works fine.
However, when I filter my list, it overwrites and loses style again, and I cannot figure out where to call the update again.
My html is as follows:
<p>Filter: <input data-bind="value: filter, valueUpdate: 'afterkeydown'" /></p> <ul data-role="listview" data-theme="g" data-bind="template: {name: 'myTemplate', foreach: filteredItems }" />
My JS knockout:
var car = function (name, make, year) { this.name = name; this.make = make; this.year = year; } var carsViewModel = { cars: ko.observableArray([]), filter: ko.observable() }; //filter the items using the filter text carsViewModel.filteredItems = ko.dependentObservable(function () { var filter = this.filter(); if (!filter) { return this.cars(); } else { return ko.utils.arrayFilter(this.cars(), function (item) { return item.make == filter; }); } }, carsViewModel); function init() { carsViewModel.cars.push(new car("car1", "bmw", 2000)); carsViewModel.cars.push(new car("car2", "bmw", 2000)); carsViewModel.cars.push(new car("car3", "toyota", 2000)); carsViewModel.cars.push(new car("car4", "toyota", 2000)); carsViewModel.cars.push(new car("car5", "toyota", 2000)); ko.applyBindings(carsViewModel); //refresh the list to reapply the styles $('ul').listview('refresh'); }
I'm sure there is something very stupid that I am missing ...
Thank you for your time.
source share