Skip to content

Commit

Permalink
Merge pull request #175 from mminer/copy-html
Browse files Browse the repository at this point in the history
Add option to copy HTML
  • Loading branch information
Globex Designs, Inc committed Dec 19, 2014
2 parents b832780 + 24a91b8 commit 5d4ae4c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
25 changes: 24 additions & 1 deletion src/classes/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,29 @@ Range.prototype.toJSON = function () {
};


/**
* Converts the cell range values to an HTML table
* @method toHTML
* @memberof Range
*
* @returns {string}
*/
Range.prototype.toHTML = function () {
var json = this.toJSON();

var rows = json.map(function (row) {
var columns = row.map(function (cell) {
return '\t\t<td>' + cell + '</td>';
});

return Array.prototype.concat('\t<tr>', columns, '\t</tr>').join('\n');
});

var html = Array.prototype.concat('<table>', rows, '</table>').join('\n');
return html;
};


/**
* Converts the cell range values to a list of selected row objects
* @method toRows
Expand Down Expand Up @@ -319,4 +342,4 @@ Range.prototype.toString = function () {
}
};

module.exports = Range;
module.exports = Range;
16 changes: 12 additions & 4 deletions src/doby-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -1703,10 +1703,18 @@ var DobyGrid = function (options) {
// Are the multiple selections?
if (self.selection.length > 1) return alert('Sorry, you cannot copy multiple selections.');

if (self.options.clipboard == 'csv') {
result = self.selection[0].toCSV();
} else if (self.options.clipboard == 'json') {
result = JSON.stringify(self.selection[0].toJSON());
switch (self.options.clipboard) {
case 'csv':
result = self.selection[0].toCSV();
break;

case 'json':
result = JSON.stringify(self.selection[0].toJSON());
break;

case 'html':
result = self.selection[0].toHTML();
break;
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/gridoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ describe("Grid Options", function () {


describe("options.clipboard", function () {
it("should be able to convert selected data to CSV and JSON", function () {
it("should be able to convert selected data to CSV and JSON and HTML", function () {
// Prepare for test
var grid = resetGrid(defaultData());

Expand All @@ -577,6 +577,10 @@ describe("Grid Options", function () {
// Convert selection to CSV
var csv = grid.selection[0].toCSV();
expect(csv).toEqual('"189","test"\n"289","test2"');

// Convert selection to HTML (minus newlines and tabs, for simpler comparison)
var html = grid.selection[0].toHTML().replace(/\n|\t/g, '')
expect(html).toEqual('<table><tr><td>189</td><td>test</td></tr><tr><td>289</td><td>test2</td></tr></table>');
});


Expand Down

0 comments on commit 5d4ae4c

Please sign in to comment.