From e2b55d3ea31c612cde9b4cdc1e5134ea1604a99f Mon Sep 17 00:00:00 2001 From: Fred Cox Date: Sat, 25 Jun 2011 23:45:12 +0300 Subject: [PATCH 1/2] Added update function to htmltable and changed push to use update - Added documentation for new htmltable.update function - Add tests for htmltable.update --- Docs/Interface/HtmlTable.md | 19 ++++++++++ Source/Interface/HtmlTable.js | 47 ++++++++++++++++--------- Tests/Interface/HtmlTable_(update).html | 43 ++++++++++++++++++++++ Tests/Interface/HtmlTable_(update).yml | 2 ++ 4 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 Tests/Interface/HtmlTable_(update).html create mode 100644 Tests/Interface/HtmlTable_(update).yml 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..c1edb250 100644 --- a/Source/Interface/HtmlTable.js +++ b/Source/Interface/HtmlTable.js @@ -11,6 +11,7 @@ license: MIT-style license authors: - Aaron Newton + - Fred Cox requires: - Core/Options @@ -105,37 +106,51 @@ 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') - }; - } - - var tds = row.map(function(data){ - var td = new Element(tag || 'td', data ? data.properties : {}), + update: function(tr, row, tag){ + var tds = tr.getChildren(tag || 'td'); + 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', 'array', 'collection', 'elements'].contains(type)){ + td.empty(); + td.adopt(content); + } else td.set('html', content); - - return td; }); + + if (tds.length > row.count) for(var i = tds.length - 1; i >= row.count; i--){ + tds[i].destroy(); + tds.erase(tds[i]); + } 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/Interface/HtmlTable_(update).html b/Tests/Interface/HtmlTable_(update).html new file mode 100644 index 00000000..30bf0f68 --- /dev/null +++ b/Tests/Interface/HtmlTable_(update).html @@ -0,0 +1,43 @@ + +

+ You should see a table below with two columns and the words "I work" in it. +

+ + + diff --git a/Tests/Interface/HtmlTable_(update).yml b/Tests/Interface/HtmlTable_(update).yml new file mode 100644 index 00000000..cf01a226 --- /dev/null +++ b/Tests/Interface/HtmlTable_(update).yml @@ -0,0 +1,2 @@ +js-references: + - "More/HtmlTable" \ No newline at end of file From 5c07d5efe411ccfc3878b3968b125d7bd3e4956e Mon Sep 17 00:00:00 2001 From: Arian Date: Sun, 31 Jul 2011 15:07:53 +0100 Subject: [PATCH 2/2] Fix HtmlTable:update so the HtmlTable:push specs pass --- Source/Interface/HtmlTable.js | 22 ++++--------- Tests/Interface/HtmlTable_(update).html | 43 ------------------------- Tests/Interface/HtmlTable_(update).yml | 2 -- Tests/Specs/1.3/Interface/HtmlTable.js | 11 +++++++ 4 files changed, 18 insertions(+), 60 deletions(-) delete mode 100644 Tests/Interface/HtmlTable_(update).html delete mode 100644 Tests/Interface/HtmlTable_(update).yml diff --git a/Source/Interface/HtmlTable.js b/Source/Interface/HtmlTable.js index c1edb250..90309817 100644 --- a/Source/Interface/HtmlTable.js +++ b/Source/Interface/HtmlTable.js @@ -11,7 +11,6 @@ license: MIT-style license authors: - Aaron Newton - - Fred Cox requires: - Core/Options @@ -107,27 +106,20 @@ var HtmlTable = new Class({ }, update: function(tr, row, tag){ - var tds = tr.getChildren(tag || 'td'); + var tds = tr.getChildren(tag || 'td'), last = tds.length - 1; + row.each(function(data, index){ var td = tds[index] || new Element(tag || 'td').inject(tr), content = (data ? data.content : '') || data, type = typeOf(content); - if (data && data.properties){ - td.set(data.properties); - } - - if (['element', 'array', 'collection', 'elements'].contains(type)){ - td.empty(); - 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); + + if (index > last) tds.push(td); + else tds[index] = td; }); - - if (tds.length > row.count) for(var i = tds.length - 1; i >= row.count; i--){ - tds[i].destroy(); - tds.erase(tds[i]); - } return { tr: tr, diff --git a/Tests/Interface/HtmlTable_(update).html b/Tests/Interface/HtmlTable_(update).html deleted file mode 100644 index 30bf0f68..00000000 --- a/Tests/Interface/HtmlTable_(update).html +++ /dev/null @@ -1,43 +0,0 @@ - -

- You should see a table below with two columns and the words "I work" in it. -

- - - diff --git a/Tests/Interface/HtmlTable_(update).yml b/Tests/Interface/HtmlTable_(update).yml deleted file mode 100644 index cf01a226..00000000 --- a/Tests/Interface/HtmlTable_(update).yml +++ /dev/null @@ -1,2 +0,0 @@ -js-references: - - "More/HtmlTable" \ No newline at end of file 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({