Skip to content

Commit c2dc5ae

Browse files
committed
Grid: Replace spreadsheet demo (utterly incomplete) with CSV editor (somewhat functional)
1 parent ad68dd1 commit c2dc5ae

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

grid-editing/spreadsheet.html renamed to grid-editing/csv-editor.html

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,35 @@
4343
$(function() {
4444
var columns = [],
4545
i, j;
46-
for (i = 65; i <= 90; i++) {
46+
for (i = 65; i <= 70; i++) {
4747
columns.push(String.fromCharCode(i));
4848
}
49-
var source = [];
50-
for (j = 0; j < 50; j++) {
51-
source.push({});
52-
}
49+
var source = [{}];
5350

54-
$( "#spreadsheet" ).grid({
51+
$( "#csvEditor" ).grid({
5552
columns: columns,
5653
source: source
5754
})
5855
.gridEditor()
5956
.navigator()
6057
.grid("refresh");
58+
59+
$( "#addRow" ).click( function() {
60+
$.observable( source ).insert( {} );
61+
});
62+
63+
$( "#export" ).click(function() {
64+
var result = "";
65+
for (j = 0; j < source.length; j++) {
66+
var row = [];
67+
for (i = 0; i < columns.length; i++) {
68+
// TODO proper CSV editor would escape values here
69+
row.push(source[j][columns[i]] || "");
70+
}
71+
result += row.join(",") + "\n";
72+
}
73+
console.log(result);
74+
});
6175
});
6276
</script>
6377
<style>
@@ -67,9 +81,9 @@
6781
</head>
6882
<body>
6983

70-
<h1>Grid based custom spreadsheet (experimental)</h1>
84+
<h1>Grid based custom csv editor (experimental)</h1>
7185

72-
<table id="spreadsheet">
86+
<table id="csvEditor">
7387
<thead>
7488
<tr>
7589
</tr>
@@ -78,5 +92,8 @@ <h1>Grid based custom spreadsheet (experimental)</h1>
7892
</tbody>
7993
</table>
8094

95+
<button id="addRow">Add row</button>
96+
<button id="export">Export as CSV (on console for now)</button>
97+
8198
</body>
8299
</html>

ui/jquery.ui.grid.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ $.widget( "ui.grid", {
4444
});
4545
}
4646
},
47-
47+
4848
_draw: function() {
4949
var totalWidth = 0,
5050
colWidths = this.element.find( "tr:first" ).children().map(function() {
@@ -73,10 +73,10 @@ $.widget( "ui.grid", {
7373
uiGridBodyTable = ( this.uiGridBodyTable = this.element )
7474
.addClass( "ui-grid-body-table" )
7575
.appendTo( uiGridBody ),
76-
76+
7777
uiGridFootTable = ( this.uiGridFootTable = $("<table class='ui-widget-content ui-grid-foot-table'></table>") )
7878
.appendTo( uiGridFoot );
79-
79+
8080
// These are used in refresh when needed for scrollbar padding
8181
this.uiGridHeadAndFoot = this.uiGridHead.add( this.uiGridFoot );
8282
this.uiGridHeadTableAndFootTable = this.uiGridHeadTable.add( this.uiGridFootTable );
@@ -112,7 +112,7 @@ $.widget( "ui.grid", {
112112
// Move table THEAD to grid head for fixed column headers
113113
uiGridBodyTable.find( "thead" )
114114
.appendTo( uiGridHeadTable );
115-
115+
116116
// Move table TFOOT to grid foot for fixed footer
117117
uiGridBodyTable.find( "tfoot" )
118118
.appendTo( uiGridFootTable );

0 commit comments

Comments
 (0)