Skip to content

Commit

Permalink
Add HtmlTable:update to update rows in a table easily
Browse files Browse the repository at this point in the history
  • Loading branch information
Arian committed Jul 31, 2011
2 parents a3d3e64 + 5c07d5e commit 43693f5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
19 changes: 19 additions & 0 deletions Docs/Interface/HtmlTable.md
Expand Up @@ -121,6 +121,25 @@ Note that it can also be an actual *TR* element.
### Example of Object Returned

{tr: theTableRow, tds: [td, td, td]}

HtmlTable Method: update {#HtmlTable:push}
----------------------------------------

Update a table row

### Syntax

myHtmlTable.update(tr, row);

### Arguments

1. tr - (*TR* element) the row to update
2. row - (*array* or *element*) the data for the row or *TR* element. Same as the row data sent to [HtmlTable.push][]

### Returns

* (*object*) an object containing the tr and td tags.



HtmlTable Method: pushMany {#HtmlTable:pushMany}
Expand Down
35 changes: 21 additions & 14 deletions Source/Interface/HtmlTable.js
Expand Up @@ -105,37 +105,44 @@ var HtmlTable = new Class({
return this;
},

push: function(row, rowProperties, target, tag, where){
if (typeOf(row) == 'element' && row.get('tag') == 'tr'){
row.inject(target || this.body, where);
return {
tr: row,
tds: row.getChildren('td')
};
}
update: function(tr, row, tag){
var tds = tr.getChildren(tag || 'td'), last = tds.length - 1;

var tds = row.map(function(data){
var td = new Element(tag || 'td', data ? data.properties : {}),
row.each(function(data, index){
var td = tds[index] || new Element(tag || 'td').inject(tr),
content = (data ? data.content : '') || data,
type = typeOf(content);

if (['element', 'array', 'collection', 'elements'].contains(type)) td.adopt(content);
if (data && data.properties) td.set(data.properties);
if (/(element(s?)|array|collection)/.test(type)) td.empty().adopt(content);
else td.set('html', content);

return td;
if (index > last) tds.push(td);
else tds[index] = td;
});

return {
tr: new Element('tr', rowProperties).inject(target || this.body, where).adopt(tds),
tr: tr,
tds: tds
};
},

push: function(row, rowProperties, target, tag, where){
if (typeOf(row) == 'element' && row.get('tag') == 'tr'){
row.inject(target || this.body, where);
return {
tr: row,
tds: row.getChildren('td')
};
}
return this.update(new Element('tr', rowProperties).inject(target || this.body, where), row, tag);
},

pushMany: function(rows, rowProperties, target, tag, where){
return rows.map(function(row){
return this.push(row, rowProperties, target, tag, where);
}, this);
},
}

});

Expand Down
11 changes: 11 additions & 0 deletions Tests/Specs/1.3/Interface/HtmlTable.js
Expand Up @@ -126,6 +126,17 @@ describe('HtmlTable', function(){

});

describe('HtmlTable:update', function(){

it('should update a table row', function(){
var table = new HtmlTable();
var row = table.push(['hello','world']);
var newrow = table.update(row.tr, ['I','work']);
expect(newrow.tds.get('text')).toEqual(['I', 'work']);
});

});

describe('HtmlTable cloned Element methods', function(){

var t = new HtmlTable({
Expand Down

0 comments on commit 43693f5

Please sign in to comment.