forked from domvm/domvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-table.html
141 lines (121 loc) · 3.54 KB
/
product-table.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Filterable Product Table</title>
<script src="../dist/micro/domvm.micro.js"></script>
</head>
<body>
<script>
var el = domvm.defineElement,
tx = domvm.defineText,
cm = domvm.defineComment,
vw = domvm.defineView,
iv = domvm.injectView,
ie = domvm.injectElement;
function FilterableProductTable(allProducts, filterText, inStockOnly) {
this.allProducts = allProducts;
this.searchBar = new SearchBar(filterText, inStockOnly);
this.productTable = new ProductTable(allProducts);
this.filter();
}
FilterableProductTable.prototype = {
filter: function() {
var bar = this.searchBar;
this.productTable.products = this.allProducts.filter(function(product) {
return !(product.name.indexOf(bar.filterText) === -1 || (!product.stocked && bar.inStockOnly))
});
},
};
function FilterableProductTableView(vm, filtTable) {
vm.on({
filterChange: function() {
filtTable.filter();
vm.redraw();
}
});
return function() {
return el("div", [
vw(SearchBarView, filtTable.searchBar),
vw(ProductTableView, filtTable.productTable),
]);
};
}
function SearchBar(filterText, inStockOnly) {
this.filterText = filterText || "";
this.inStockOnly = inStockOnly || false;
}
function SearchBarView(vm, searchBar) {
function syncAndEmitChange(e) {
searchBar.inStockOnly = vm.refs.inStockOnly.el.checked;
searchBar.filterText = vm.refs.filterText.el.value;
vm.emit("filterChange");
}
return function() {
return el("form", [
el("input", {
_ref: "filterText",
type: "text",
placeholder: "Search...",
onkeyup: syncAndEmitChange,
value: searchBar.filterText
}),
el("br"),
el("input", {
_ref: "inStockOnly",
type: "checkbox",
onchange: syncAndEmitChange,
checked: searchBar.inStockOnly
}),
tx("Only show products in stock")
]);
};
}
function ProductTable(products) {
this.products = products;
}
function ProductTableView(vm, prodTable) {
return function() {
var rows = [];
var lastCategory = null;
prodTable.products.forEach(function(product) {
if (product.category !== lastCategory) {
rows.push(el("tr", [
el("th", {colspan: 2}, product.category)
]));
}
rows.push(el("tr", [
product.stocked
? el("td", product.name)
: el("td", [el("span", {style: {color: "red"}}, product.name)]),
el("td", product.price)
]));
lastCategory = product.category;
});
return el("table", [
el("thead", [
el("tr", [
el("th", "Name"),
el("th", "Price"),
])
]),
el("tbody", rows)
]);
};
}
var products = [
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
{category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
{category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
{category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
{category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
{category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];
// if you want, you can initialize the table with pre-defined filters
var filterText = "",
inStockOnly = false;
var myTable = new FilterableProductTable(products, filterText, inStockOnly);
domvm.createView(FilterableProductTableView, myTable).mount(document.body);
</script>
</body>
</html>