-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRUDGrid.js
executable file
·134 lines (116 loc) · 5.9 KB
/
CRUDGrid.js
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
// This is an example of one possible way to make a reusable component (or 'plugin'), consisting of:
// * A view model class, which gives a way to configure the component and to interact with it (e.g., by exposing currentPageIndex as an observable, external code can change the page index)
// * A custom binding (ko.bindingHandlers.simpleGrid in this example) so a developer can place instances of it into the DOM
// - in this example, the custom binding works by rendering some predefined templates using the ko.jqueryTmplTemplateEngine template engine
//
// There are loads of ways this grid example could be expanded. For example,
// * Letting the developer override the templates used to create the table header, table body, and page links div
// * Adding a "sort by clicking column headers" option
// * Creating some API to fetch table data using Ajax requests
// ... etc
(function () {
// Private function
function getColumnsForScaffolding(data) {
if ((typeof data.length != 'number') || data.length == 0)
return [];
var columns = [];
for (var propertyName in data[0])
columns.push({ headerText: propertyName, rowText: propertyName });
return columns;
}
ko.simpleGrid = {
// Defines a view model class you can use to populate a grid
viewModel: function (configuration) {
this.mostraEditar = configuration.mostraEditar || false;
this.functionAddNewObj = configuration.functionAddNewObj;
this.editarClick= configuration.editarClick;
this.data = configuration.data;
this.currentPageIndex = ko.observable(0);
this.pageSize = configuration.pageSize || 5;
// If you don't specify columns configuration, we'll use scaffolding
this.columns = configuration.columns || getColumnsForScaffolding(ko.utils.unwrapObservable(this.data));
this.itemsOnCurrentPage = ko.dependentObservable(function () {
var startIndex = this.pageSize * this.currentPageIndex();
return this.data.slice(startIndex, startIndex + this.pageSize);
}, this);
this.maxPageIndex = ko.dependentObservable(function () {
return Math.ceil(ko.utils.unwrapObservable(this.data).length / this.pageSize);
}, this);
this.criaNovoObjfromTemplate = function(){
var newObj = {};
for(var key in this.data()[0]){
if(key != 'updateObj'){
newObj[key] = $('#asd'+key).val();
}
}
this.functionAddNewObj(newObj)
}
}
};
// Templates used to render the grid
var templateEngine = new ko.jqueryTmplTemplateEngine();
templateEngine.addTemplate("ko_simpleGrid_grid", "\
<table class=\"ko-grid\" cellspacing=\"0\">\
<thead>\
<tr>\
{{each(i, columnDefinition) columns}}\
<th>${ columnDefinition.headerText }</th>\
{{/each}}\
{{if mostraEditar}}\
<th>Editar</th>\
{{/if}}\
</tr>\
</thead>\
<tbody>\
{{each(i, row) itemsOnCurrentPage()}}\
<tr class=\"${ i % 2 == 0 ? 'even' : 'odd' }\">\
{{each(j, columnDefinition) columns}}\
<td>{{html typeof columnDefinition.rowText == 'function' ? columnDefinition.rowText(row,i) : row[columnDefinition.rowText] }}</td>\
{{/each}}\
{{if mostraEditar}}\
<td><button class='btnEditar' data-bind=\"click: function() { this.editarClick(this.itemsOnCurrentPage()[i]); }\">Editar</button></td>\
{{/if}}\
</tr>\
{{/each}}\
</tbody>\
</table>");
templateEngine.addTemplate("ko_simpleGrid_pageLinks", "\
<div class=\"ko-grid-pageLinks\">\
<span><!--Page:--></span>\
{{each(i) ko.utils.range(1, maxPageIndex)}}\
<a href=\"#\" data-bind=\"click: function() { currentPageIndex(i); }, css: { selected: i == currentPageIndex() }\">\
${ i + 1 }\
</a>\
{{/each}}\
</div>");
templateEngine.addTemplate("form_add_newObj", "\
<form>\
<fieldset>\
{{each(i, objProp) data()[0]}}\
{{if i != 'updateObj'}}\
<label for=\"${ i }\">${ i }</label>\
<input type=\"text\" name=\"${ i }\" id=\"asd${ i }\" class=\"text ui-widget-content ui-corner-all\" />\
<br/>\
{{/if}}\
{{/each}}\
<button class='btnEditar' data-bind=\"click: function() {\
criaNovoObjfromTemplate()\
}\">Adicionar</button>\
</fieldset>\
</form>");
// The "simpleGrid" binding
ko.bindingHandlers.simpleGrid = {
// This method is called to initialize the node, and will also be called again if you change what the grid is bound to
update: function (element, viewModelAccessor) {
var teste = function(){
$('button').button();
};
var viewModel = viewModelAccessor();
element.innerHTML = "";
var gridContainer = element.appendChild(document.createElement("DIV"));
ko.renderTemplate("ko_simpleGrid_grid", viewModel, { templateEngine: templateEngine, afterRender: teste }, gridContainer, "replaceNode");
var pageLinksContainer = element.appendChild(document.createElement("DIV"));
ko.renderTemplate("ko_simpleGrid_pageLinks", viewModel, { templateEngine: templateEngine }, pageLinksContainer, "replaceNode");
}
};
})();