diff --git a/data/grid/grid.js b/data/grid/grid.js
index 3aa83c5..e071bb8 100644
--- a/data/grid/grid.js
+++ b/data/grid/grid.js
@@ -46,7 +46,10 @@ $.Controller.extend("Mxui.Data.Grid",{
defaults: {
columns: {},
params: new Mxui.Data,
- row : null,
+ initTemplate: '//mxui/data/grid/views/init.ejs',
+ listTemplate: '//mxui/data/grid/views/list.ejs',
+ titleTemplate: '//mxui/data/grid/views/th.ejs',
+ rowTemplate : null,
model : null,
noItems : "No Items",
// if true, can sort by multiple columns at a time
@@ -60,7 +63,23 @@ $.Controller.extend("Mxui.Data.Grid",{
// immediately uses the model to request items for the grid
loadImmediate: true,
- selectable : true
+ selectable : true,
+
+ // default crud manipulations
+
+ refresh: function(tbody, elt, newElt){
+ elt.replaceWith(newElt)
+ tbody.resize()
+ },
+ append: function(tbody, newElt){
+ tbody.prepend(newElt)
+ tbody.resize()
+ },
+ remove: function(tbody, elt){
+ elt.remove()
+ tbody.resize()
+ }
+
},
listensTo : ["select","deselect"]
},
@@ -78,7 +97,7 @@ $.Controller.extend("Mxui.Data.Grid",{
for(var name in this.options.columns){
count++;
}
- this.element.append( this.view({columns: this.options.columns, count: count}) )
+ this.element.append( this.view(this.options.initTemplate,{titleTemplate: this.options.titleTemplate, columns: this.options.columns, count: count}) )
this.element.children('table').mxui_layout_table_scroll({
filler: this.options.filler
@@ -124,15 +143,15 @@ $.Controller.extend("Mxui.Data.Grid",{
this.options.params.attr('updating', false);
- var trs = $(this.view('list',{
- row : this.options.row,
+ var trs = $(this.view(this.options.listTemplate,{
+ rowTemplate : this.options.rowTemplate,
items: items
}));
if(clear){
this.empty();
}
-
+
this.append(trs);
// update the items
this.options.params.attr('count',items.count)
@@ -141,50 +160,47 @@ $.Controller.extend("Mxui.Data.Grid",{
if(attr !== 'count' && attr !== 'updating'){
//want to throttle for rapid updates
params.attr('updating', true)
- clearTimeout(this.newRequestTimer,100)
- this.newRequestTimer = setTimeout(this.callback('newRequest', attr, val))
+ clearTimeout(this.newRequestTimer)
+ this.newRequestTimer = setTimeout(this.callback('newRequest', attr, val), 100)
}
},
newRequest : function(attr, val){
- var clear = true;
+ var clear = true;
if(!this.options.offsetEmpties && attr == "offset"){ // if offset changes and we have offsetEmpties false
clear = false;
}
- this.options.model.findAll(this.options.params.attrs(), this.callback('list', clear))
+ this.makeRequest(clear)
},
+ makeRequest: function(clear){
+ this.options.model.findAll(this.options.params.attrs(), this.callback('list', clear))
+ },
+ _getRows: function(viewTemplateOption, items){
+ items = ( $.isArray(items) || items instanceof $.Model.List ) ? items : [items]
+ return $(this.view(this.options.listTemplate,{
+ rowTemplate : this.options[viewTemplateOption],
+ items: items
+ }, { columns: this.options.columns, renderer: this.options.renderer } ));
+ },
+
/**
* Listen for updates and replace the text of the list
* @param {Object} called
* @param {Object} item
*/
"{model} updated" : function(model, ev, item){
- var el = item.elements(this.element).html(this.options.row, item);
- if(this.options.updated){
- this.options.updated(this.element, el, item)
+ var el = item.elements(this.element)
+ if (el.length > 0){
+ var newElt= this._getRows('rowTemplate', item)
+ this.options.refresh(this.$.tbody, el, newElt)
}
- this.element.resize()
},
"{model} created" : function(model, ev, item){
- var newEl = $($.View("//mxui/data/grid/views/list",{
- items : [item],
- row: this.options.row
- }))
- if(this.options.append){
- this.options.append(this.element, newEl, item)
- }else{
- this.append(newEl)
- //newEl.appendTo(this.element).slideDown();
- }
+ var newEl = this._getRows('rowTemplate', item)
+ this.options.append(this.$.tbody, newEl)
},
"{model} destroyed" : function(model, ev, item){
var el = item.elements(this.element)
- if(this.options.remove){
- this.options.remove(this.element,el, item)
- }else{
- el.slideUp( function(){
- el.remove();
- })
- }
+ this.options.remove(this.$.tbody, el)
},
/**
* Insert rows into the table
@@ -202,8 +218,9 @@ $.Controller.extend("Mxui.Data.Grid",{
* Remove all children from the table
*/
empty: function(){
- this.element.children(":first").mxui_layout_table_scroll("empty")
+ this.element.children(".mxui_layout_table_scroll").mxui_layout_table_scroll("empty")
},
+
"select" : function(el, ev){
ev.preventDefault();
},
@@ -213,4 +230,4 @@ $.Controller.extend("Mxui.Data.Grid",{
});
-})
\ No newline at end of file
+})
diff --git a/data/grid/views/init.ejs b/data/grid/views/init.ejs
index ae0ca07..70d3bc3 100644
--- a/data/grid/views/init.ejs
+++ b/data/grid/views/init.ejs
@@ -2,15 +2,11 @@
<%for(var col in columns){%>
- <%== view('//mxui/data/grid/views/th', {name: col, title: columns[col]}) %>
+ <%== $.View('//mxui/data/grid/views/th', {name: col, title: columns[col]}) %>
<%}%>