diff --git a/Docs/Interface/HtmlTable.md b/Docs/Interface/HtmlTable.md index 4c940381..bb7d934a 100644 --- a/Docs/Interface/HtmlTable.md +++ b/Docs/Interface/HtmlTable.md @@ -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} diff --git a/Source/Interface/HtmlTable.js b/Source/Interface/HtmlTable.js index 708b0c9b..90309817 100644 --- a/Source/Interface/HtmlTable.js +++ b/Source/Interface/HtmlTable.js @@ -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); - }, + } }); diff --git a/Tests/Specs/1.3/Interface/HtmlTable.js b/Tests/Specs/1.3/Interface/HtmlTable.js index 8b1c45ed..358cd5b7 100644 --- a/Tests/Specs/1.3/Interface/HtmlTable.js +++ b/Tests/Specs/1.3/Interface/HtmlTable.js @@ -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({