Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit c45275c

Browse files
committed
v1.0.2 candidate
1 parent 062f036 commit c45275c

7 files changed

+145
-96
lines changed

Gruntfile.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = function(grunt) {
22

3+
4+
35
grunt.initConfig({
46

57
// Import package manifest
@@ -48,12 +50,8 @@ module.exports = function(grunt) {
4850
},
4951

5052
});
51-
52-
grunt.loadNpmTasks("grunt-contrib-concat");
53-
grunt.loadNpmTasks("grunt-contrib-jshint");
54-
grunt.loadNpmTasks("grunt-contrib-uglify");
55-
53+
54+
require('load-grunt-tasks')(grunt);
5655
grunt.registerTask("default", ["jshint", "concat", "uglify"]);
57-
grunt.registerTask("travis", ["jshint"]);
5856

5957
};

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-table2excel",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"homepage": "https://github.com/jquery-table2excel/",
55
"authors": [
66
"rainabba <[email protected]>"

dist/jquery.table2excel.js

+105-58
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery table2excel - v1.0.1
2+
* jQuery table2excel - v1.0.2
33
* jQuery plugin to export an .xls file in browser from an HTML table
44
* https://github.com/rainabba/jquery-table2excel
55
*
@@ -8,67 +8,114 @@
88
*/
99
//table2excel.js
1010
;(function ( $, window, document, undefined ) {
11-
var pluginName = "table2excel",
12-
defaults = {
13-
exclude: ".noExl",
14-
name: "Table2Excel"
15-
};
16-
17-
// The actual plugin constructor
18-
function Plugin ( element, options ) {
19-
this.element = element;
20-
// jQuery has an extend method which merges the contents of two or
21-
// more objects, storing the result in the first object. The first object
22-
// is generally empty as we don't want to alter the default options for
23-
// future instances of the plugin
24-
this.settings = $.extend( {}, defaults, options );
25-
this._defaults = defaults;
26-
this._name = pluginName;
27-
this.init();
28-
}
11+
var pluginName = "table2excel",
12+
13+
defaults = {
14+
exclude: ".noExl",
15+
name: "Table2Excel"
16+
};
17+
18+
// The actual plugin constructor
19+
function Plugin ( element, options ) {
20+
this.element = element;
21+
// jQuery has an extend method which merges the contents of two or
22+
// more objects, storing the result in the first object. The first object
23+
// is generally empty as we don't want to alter the default options for
24+
// future instances of the plugin
25+
//
26+
this.settings = $.extend( {}, defaults, options );
27+
this._defaults = defaults;
28+
this._name = pluginName;
29+
this.init();
30+
}
31+
32+
Plugin.prototype = {
33+
init: function () {
34+
var e = this;
35+
36+
e.template = {
37+
head: "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
38+
sheet: {
39+
head: "<x:ExcelWorksheet><x:Name>",
40+
tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
41+
},
42+
mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
43+
table: {
44+
head: "<table>",
45+
tail: "</table>"
46+
},
47+
foot: "</body></html>"
48+
};
49+
50+
e.tableRows = [];
2951

30-
Plugin.prototype = {
31-
init: function () {
32-
var e = this;
33-
e.template = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><!--[if gte mso 9]><xml>";
34-
e.template += "<x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>";
35-
e.template += "<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>";
36-
e.tableRows = "";
37-
38-
// get contents of table except for exclude
39-
$(e.element).find("tr").not(this.settings.exclude).each(function (i,o) {
40-
e.tableRows += "<tr>" + $(o).html() + "</tr>";
52+
// get contents of table except for exclude
53+
$(e.element).each( function(i,o) {
54+
var tempRows = "";
55+
$(o).find("tr").not(e.settings.exclude).each(function (i,o) {
56+
tempRows += "<tr>" + $(o).html() + "</tr>";
4157
});
42-
this.tableToExcel(this.tableRows, this.settings.name);
43-
},
44-
tableToExcel: function (table, name) {
45-
var e = this;
46-
e.uri = "data:application/vnd.ms-excel;base64,";
47-
e.base64 = function (s) {
48-
return window.btoa(unescape(encodeURIComponent(s)));
49-
};
50-
e.format = function (s, c) {
51-
return s.replace(/{(\w+)}/g, function (m, p) {
52-
return c[p];
53-
});
54-
};
55-
e.ctx = {
56-
worksheet: name || "Worksheet",
57-
table: table
58-
};
59-
window.location.href = e.uri + e.base64(e.format(e.template, e.ctx));
60-
}
61-
};
58+
e.tableRows.push(tempRows);
59+
});
60+
61+
e.tableToExcel(e.tableRows, e.settings.name);
62+
},
6263

63-
$.fn[ pluginName ] = function ( options ) {
64-
this.each(function() {
65-
if ( !$.data( this, "plugin_" + pluginName ) ) {
66-
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
67-
}
64+
tableToExcel: function (table, name) {
65+
var e = this, fullTemplate="", i;
66+
67+
e.uri = "data:application/vnd.ms-excel;base64,";
68+
e.base64 = function (s) {
69+
return window.btoa(unescape(encodeURIComponent(s)));
70+
};
71+
e.format = function (s, c) {
72+
return s.replace(/{(\w+)}/g, function (m, p) {
73+
return c[p];
6874
});
75+
};
76+
e.ctx = {
77+
worksheet: name || "Worksheet",
78+
table: table
79+
};
80+
81+
fullTemplate= e.template.head;
82+
83+
if ( $.isArray(table) ) {
84+
for (i in table) {
85+
//fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
86+
fullTemplate += e.template.sheet.head + "Table" + i + "" + e.template.sheet.tail;
87+
}
88+
}
89+
90+
fullTemplate += e.template.mid;
91+
92+
if ( $.isArray(table) ) {
93+
for (i in table) {
94+
fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
95+
}
96+
}
97+
98+
fullTemplate += e.template.foot;
99+
100+
for (i in table) {
101+
e.ctx["table" + i] = table[i];
102+
}
103+
delete e.ctx.table;
104+
105+
window.location.href = e.uri + e.base64(e.format(fullTemplate, e.ctx));
106+
}
107+
};
108+
109+
$.fn[ pluginName ] = function ( options ) {
110+
var e = this;
111+
e.each(function() {
112+
if ( !$.data( e, "plugin_" + pluginName ) ) {
113+
$.data( e, "plugin_" + pluginName, new Plugin( this, options ) );
114+
}
115+
});
69116

70-
// chain jQuery functions
71-
return this;
72-
};
117+
// chain jQuery functions
118+
return e;
119+
};
73120

74121
})( jQuery, window, document );

dist/jquery.table2excel.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
"url": "http://github.com/rainabba/jquery-table2excel.git"
99
},
1010
"homepage": "https://github.com/rainabba/jquery-table2excel/",
11-
"version": "1.0.1",
11+
"version": "1.0.2",
1212
"devDependencies": {
13-
"grunt": "~0.4.1",
13+
"grunt": "~0.4.5",
1414
"grunt-cli": "~0.1.13",
15-
"grunt-contrib-jshint": "~0.8.0",
16-
"grunt-contrib-concat": "~0.3.0",
17-
"grunt-contrib-uglify": "~0.3.2"
15+
"grunt-contrib-jshint": "~0.11.1",
16+
"grunt-contrib-concat": "~0.5.1",
17+
"grunt-contrib-uglify": "~0.9.0"
1818
},
1919
"scripts": {
20-
"test": "grunt travis --verbose"
20+
"test": "grunt --verbose"
2121
}
2222
}

src/jquery.table2excel.js

+26-22
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424
Plugin.prototype = {
2525
init: function () {
2626
var e = this;
27+
2728
e.template = {
28-
head: "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>"
29-
,sheet: {
30-
head: "<x:ExcelWorksheet><x:Name>" //{worksheet}
31-
,tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
32-
}
33-
,mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>"
34-
,table: {
35-
head: "<table>" //{table}
36-
,tail: "</table>"
37-
}
38-
,foot: "</body></html>"
39-
}
29+
head: "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
30+
sheet: {
31+
head: "<x:ExcelWorksheet><x:Name>",
32+
tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
33+
},
34+
mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
35+
table: {
36+
head: "<table>",
37+
tail: "</table>"
38+
},
39+
foot: "</body></html>"
40+
};
41+
4042
e.tableRows = [];
4143

4244
// get contents of table except for exclude
@@ -48,11 +50,12 @@
4850
e.tableRows.push(tempRows);
4951
});
5052

51-
5253
e.tableToExcel(e.tableRows, e.settings.name);
53-
},
54+
},
55+
5456
tableToExcel: function (table, name) {
55-
var e = this, fullTemplate="";
57+
var e = this, fullTemplate="", i;
58+
5659
e.uri = "data:application/vnd.ms-excel;base64,";
5760
e.base64 = function (s) {
5861
return window.btoa(unescape(encodeURIComponent(s)));
@@ -70,7 +73,7 @@
7073
fullTemplate= e.template.head;
7174

7275
if ( $.isArray(table) ) {
73-
for (var i in table) {
76+
for (i in table) {
7477
//fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
7578
fullTemplate += e.template.sheet.head + "Table" + i + "" + e.template.sheet.tail;
7679
}
@@ -79,14 +82,14 @@
7982
fullTemplate += e.template.mid;
8083

8184
if ( $.isArray(table) ) {
82-
for (var i in table) {
85+
for (i in table) {
8386
fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
8487
}
8588
}
8689

8790
fullTemplate += e.template.foot;
8891

89-
for (var i in table) {
92+
for (i in table) {
9093
e.ctx["table" + i] = table[i];
9194
}
9295
delete e.ctx.table;
@@ -96,14 +99,15 @@
9699
};
97100

98101
$.fn[ pluginName ] = function ( options ) {
99-
this.each(function() {
100-
if ( !$.data( this, "plugin_" + pluginName ) ) {
101-
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
102+
var e = this;
103+
e.each(function() {
104+
if ( !$.data( e, "plugin_" + pluginName ) ) {
105+
$.data( e, "plugin_" + pluginName, new Plugin( this, options ) );
102106
}
103107
});
104108

105109
// chain jQuery functions
106-
return this;
110+
return e;
107111
};
108112

109113
})( jQuery, window, document );

table2excel.jquery.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"Table",
1010
"Excel"
1111
],
12-
"version": "1.0.1",
12+
"version": "1.0.2",
1313
"author": {
1414
"name": "rainabba",
1515
"email": "[email protected]",

0 commit comments

Comments
 (0)