From ee4a0012f437ca92ca9a6c17df79f22ea389f6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haukur=20Stef=C3=A1nsson?= Date: Mon, 24 Mar 2014 16:04:29 +0000 Subject: [PATCH 1/4] Added implementation to export columns in table based on the class 'no-export' --- src/scripts/00-directive.js | 106 +++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 45 deletions(-) diff --git a/src/scripts/00-directive.js b/src/scripts/00-directive.js index 4649845..92fb41a 100644 --- a/src/scripts/00-directive.js +++ b/src/scripts/00-directive.js @@ -1,48 +1,64 @@ -angular.module('ngTableExport', []) +angular.module('ngTableExport', ['ngTable']) .config(['$compileProvider', function($compileProvider) { - // allow data links - $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|data):/); + // allow data links + $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|data):/); }]) .directive('exportCsv', ['$parse', function ($parse) { - return { - restrict: 'A', - scope: false, - link: function(scope, element, attrs) { - var data = ''; - var csv = { - stringify: function(str) { - return '"' + - str.replace(/^\s\s*/, '').replace(/\s*\s$/, '') // trim spaces - .replace(/"/g,'""') + // replace quotes with double quotes - '"'; - }, - generate: function() { - data = ''; - var rows = element.find('tr'); - angular.forEach(rows, function(row, i) { - var tr = angular.element(row), - tds = tr.find('th'), - rowData = ''; - if (tr.hasClass('ng-table-filters')) { - return; - } - if (tds.length == 0) { - tds = tr.find('td'); - } - if (i != 1) { - angular.forEach(tds, function(td, i) { - rowData += csv.stringify(angular.element(td).text()) + ';'; - }); - rowData = rowData.slice(0, rowData.length - 1); //remove last semicolon - } - data += rowData + "\n"; - }); - }, - link: function() { - return 'data:text/csv;charset=UTF-8,' + encodeURIComponent(data); - } - }; - $parse(attrs.exportCsv).assign(scope.$parent, csv); - } - }; -}]); \ No newline at end of file + return { + restrict: 'A', + scope: false, + link: function(scope, element, attrs) { + var data = ''; + var csv = { + stringify: function(str) { + return '"' + + str.replace(/^\s\s*/, '').replace(/\s*\s$/, '') // trim spaces + .replace(/"/g,'""') + // replace quotes with double quotes + '"'; + }, + generate: function() { + //this array hold true or false for each header based on + //whether they have the 'no-export' class or not + var exports = []; + data = ''; + var rows = element.find('tr'); + angular.forEach(rows, function(row, i) { + console.log(i); + var tr = angular.element(row), + tds = tr.find('th'), + rowData = ''; + if (tr.hasClass('ng-table-filters')) { + return; + } + if (tds.length == 0) { + tds = tr.find('td'); + } + else { + //go through the headers and check if they have the + //'no-export' class + for(var i = 0; i < tds.length; i++) { + exports[i] = angular.element(tds[i]).hasClass('no-export'); + } + } + if (i != 1) { + angular.forEach(tds, function(td, i) { + //if the corresponding index in exports[] is false, this + //header does not have the 'no-export' class and is not exported + if(exports[i] == false) { + rowData += csv.stringify(angular.element(td).text()) + ';'; + } + }); + rowData = rowData.slice(0, rowData.length - 1); //remove last semicolon + } + data += rowData + "\n"; + + }); + }, + link: function() { + return 'data:text/csv;charset=UTF-8,' + encodeURIComponent(data); + } + }; + $parse(attrs.exportCsv).assign(scope.$parent, csv); + } + }; +}]); From 7ac277462b40b0188025c0dc0b8226d081d7cb13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haukur=20Stef=C3=A1nsson?= Date: Mon, 24 Mar 2014 16:06:59 +0000 Subject: [PATCH 2/4] Grammar --- src/scripts/00-directive.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts/00-directive.js b/src/scripts/00-directive.js index 92fb41a..fb2d9c9 100644 --- a/src/scripts/00-directive.js +++ b/src/scripts/00-directive.js @@ -17,7 +17,7 @@ angular.module('ngTableExport', ['ngTable']) '"'; }, generate: function() { - //this array hold true or false for each header based on + //this array holds true or false for each header based on //whether they have the 'no-export' class or not var exports = []; data = ''; @@ -43,7 +43,7 @@ angular.module('ngTableExport', ['ngTable']) if (i != 1) { angular.forEach(tds, function(td, i) { //if the corresponding index in exports[] is false, this - //header does not have the 'no-export' class and is not exported + //header does not have the 'no-export' class and is therefore exported if(exports[i] == false) { rowData += csv.stringify(angular.element(td).text()) + ';'; } From 4e0a5b8b8501cf338a0cdee824fc7d95ff0a4043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haukur=20Stef=C3=A1nsson?= Date: Mon, 24 Mar 2014 17:00:23 +0000 Subject: [PATCH 3/4] Removed console.log line --- src/scripts/00-directive.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/scripts/00-directive.js b/src/scripts/00-directive.js index fb2d9c9..1f9e940 100644 --- a/src/scripts/00-directive.js +++ b/src/scripts/00-directive.js @@ -23,7 +23,6 @@ angular.module('ngTableExport', ['ngTable']) data = ''; var rows = element.find('tr'); angular.forEach(rows, function(row, i) { - console.log(i); var tr = angular.element(row), tds = tr.find('th'), rowData = ''; From a1b12edf3e405fb21cd4ff20c882e1369abe897b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haukur=20Stef=C3=A1nsson?= Date: Mon, 24 Mar 2014 17:32:06 +0000 Subject: [PATCH 4/4] Converted tabs to spaces --- src/scripts/00-directive.js | 118 ++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/src/scripts/00-directive.js b/src/scripts/00-directive.js index 1f9e940..1b9cf1c 100644 --- a/src/scripts/00-directive.js +++ b/src/scripts/00-directive.js @@ -1,63 +1,63 @@ -angular.module('ngTableExport', ['ngTable']) +angular.module('ngTableExport', []) .config(['$compileProvider', function($compileProvider) { - // allow data links - $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|data):/); + // allow data links + $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|data):/); }]) .directive('exportCsv', ['$parse', function ($parse) { - return { - restrict: 'A', - scope: false, - link: function(scope, element, attrs) { - var data = ''; - var csv = { - stringify: function(str) { - return '"' + - str.replace(/^\s\s*/, '').replace(/\s*\s$/, '') // trim spaces - .replace(/"/g,'""') + // replace quotes with double quotes - '"'; - }, - generate: function() { - //this array holds true or false for each header based on - //whether they have the 'no-export' class or not - var exports = []; - data = ''; - var rows = element.find('tr'); - angular.forEach(rows, function(row, i) { - var tr = angular.element(row), - tds = tr.find('th'), - rowData = ''; - if (tr.hasClass('ng-table-filters')) { - return; - } - if (tds.length == 0) { - tds = tr.find('td'); - } - else { - //go through the headers and check if they have the - //'no-export' class - for(var i = 0; i < tds.length; i++) { - exports[i] = angular.element(tds[i]).hasClass('no-export'); - } - } - if (i != 1) { - angular.forEach(tds, function(td, i) { - //if the corresponding index in exports[] is false, this - //header does not have the 'no-export' class and is therefore exported - if(exports[i] == false) { - rowData += csv.stringify(angular.element(td).text()) + ';'; - } - }); - rowData = rowData.slice(0, rowData.length - 1); //remove last semicolon - } - data += rowData + "\n"; - - }); - }, - link: function() { - return 'data:text/csv;charset=UTF-8,' + encodeURIComponent(data); - } - }; - $parse(attrs.exportCsv).assign(scope.$parent, csv); - } - }; + return { + restrict: 'A', + scope: false, + link: function(scope, element, attrs) { + var data = ''; + var csv = { + stringify: function(str) { + return '"' + + str.replace(/^\s\s*/, '').replace(/\s*\s$/, '') // trim spaces + .replace(/"/g,'""') + // replace quotes with double quotes + '"'; + }, + generate: function() { + //this array holds true or false for each header based on + //whether they have the 'no-export' class or not + var exports = []; + data = ''; + var rows = element.find('tr'); + angular.forEach(rows, function(row, i) { + var tr = angular.element(row), + tds = tr.find('th'), + rowData = ''; + if (tr.hasClass('ng-table-filters')) { + return; + } + if (tds.length == 0) { + tds = tr.find('td'); + } + else { + //go through the headers and check if they have the + //'no-export' class + for(var i = 0; i < tds.length; i++) { + exports[i] = angular.element(tds[i]).hasClass('no-export'); + } + } + if (i != 1) { + angular.forEach(tds, function(td, i) { + //if the corresponding index in exports[] is false, this + //header does not have the 'no-export' class and is therefore exported + if(exports[i] == false) { + rowData += csv.stringify(angular.element(td).text()) + ';'; + } + }); + rowData = rowData.slice(0, rowData.length - 1); //remove last semicolon + } + data += rowData + "\n"; + + }); + }, + link: function() { + return 'data:text/csv;charset=UTF-8,' + encodeURIComponent(data); + } + }; + $parse(attrs.exportCsv).assign(scope.$parent, csv); + } + }; }]);