0
我正在尝试将单个页面应用程序同时进行分面搜索和分页。我对Knockout相当陌生,而我正在努力将这两个概念联系在一起。Knockout JS Paging and Facet Search
网站的发展可以在这里看到http://especial2.egcommerce.com/search
function ProductDimensionsViewModel() {
var self = this;
self.dimensions = ko.observableArray();
//self.dimensions(data);
var baseUri = 'api/product_dimensions.php';
$.getJSON(baseUri, self.dimensions);
//self.dimensions.subscribe(function(updated) {
// alert(updated);
// });
self.filterByBrand = ko.computed(function() {
return ko.utils.arrayFilter(self.dimensions(), function(dimension) { return dimension.type == "BRAND"; })
});
self.filterByArea = ko.computed(function() {
return ko.utils.arrayFilter(self.dimensions(), function(dimension) { return dimension.type == "AREA"; })
});
self.filterByType = ko.computed(function() {
return ko.utils.arrayFilter(self.dimensions(), function(dimension) { return dimension.type == "TYPE"; })
})
self.filterByBrandMenu = ko.computed(function() {
return ko.utils.arrayFilter(self.dimensions(), function(dimension) { return dimension.type == "BRAND" && dimension.menuItem == "YES"})
});
self.filterByAreaMenu = ko.computed(function() {
return ko.utils.arrayFilter(self.dimensions(), function(dimension) { return dimension.type == "AREA" && dimension.menuItem == "YES"})
});
self.filterByTypeMenu = ko.computed(function() {
return ko.utils.arrayFilter(self.dimensions(), function(dimension) { return dimension.type == "TYPE" && dimension.menuItem =="YES" })
});
self.products = ko.observableArray();
$.getJSON("api/products", self.products);
//self.products(product)
//console.log(self.products(data.count));
/*
* Paging functionality
*/
// only for example, used to demonstrate setting the total item count from a service call.
self.SetTotalResults = ko.observable(100);
// holds the total item count
self.TotalResults = ko.observable();
// actual pager, used to bind to the pager's template
// first parameter must be an observable or function which returns the current 'total item count'.
// it is wrapped in a ko.computed inside the pager.
self.Pager = ko.pager(self.TotalResults);
// Subscribe to current page changes.
self.Pager().CurrentPage.subscribe(function() {
self.search();
});
self.search = function() {
// simulate a search
// ie.:
/*
var maximumRows = self.Pager().PageSize(),
searchText = self.SearchText(),
startIndex = (self.Pager().CurrentPage() - 1) * maximumRows;
myService.search(searchText, startIndex, maximumRows)
.done(function(result) {
// set your own results etc...
self.TotalResults(result.totalItemCount);
}
*/
// setting 'total results'. This should be in your result callback
// in this example we grab it from the form.
var totalItemCount = self.SetTotalResults();
self.TotalResults(totalItemCount);
}
ko.applyBindings(新ProductDimensionsViewModel())
正如你可以看到我已经成功地填充的过滤器,并从产品拉阿贾克斯呼吁。
我现在需要一些指导以下内容。
- 如何筛选基于复选框的产品左侧
- 如何分页功能链接到产品的结果。
任何帮助将不胜感激,该网站只有可能有1500个产品,所以我认为我正在尝试实施的解决方案将使导航产品非常光滑。
感谢所有帮助 罗布
感谢您的帮助。我还有一些问题。 1.如何设置复选框的初始状态(参见下面) {“id”:“1”,“type”:“AREA”,“name”:“Fine Dining”,“Description” :“blah blah blah”,“menuItem”:“YES”,“active”:ko.observable(true)} 我的模板现在看起来像这样。 这样做确实可以与我放入的过滤器一起使用地方,但我确信它不正确的必须转储出这样的JSON,使其工作。 –
不确定我是否遵循,但如果您问如何初始填充过滤器数组,您可以使用ko.observableArray作为filters数组,然后用filters.push(...)每个过滤器作为从Ajax获得项目呼叫等“。 – 7zark7