|
| 1 | +<!DOCTYPE HTML> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
| 5 | + <link rel="shortcut icon" type="image/ico" href="favicon.ico" /> |
| 6 | + <title>SlickGrid example: Spreadsheet with Excel compatible cut and paste</title> |
| 7 | + <link rel="stylesheet" href="../slick.grid.css" type="text/css"/> |
| 8 | + <link rel="stylesheet" href="../css/smoothness/jquery-ui-1.11.3.custom.css" type="text/css"/> |
| 9 | + <link rel="stylesheet" href="examples.css" type="text/css"/> |
| 10 | + <style> |
| 11 | + .slick-cell.copied { |
| 12 | + background: blue; |
| 13 | + background: rgba(0, 0, 255, 0.2); |
| 14 | + -webkit-transition: 0.5s background; |
| 15 | + } |
| 16 | + </style> |
| 17 | +</head> |
| 18 | +<body> |
| 19 | +<div style="position:relative"> |
| 20 | + <div style="width:600px;"> |
| 21 | + <div id="myGrid" style="width:100%;height:300px;"></div> |
| 22 | + </div> |
| 23 | + <br/> |
| 24 | + <div style="width:600px;"> |
| 25 | + <div id="myGrid2" style="width:100%;height:300px;" class="example-grid"></div> |
| 26 | + </div> |
| 27 | + |
| 28 | + <div class="options-panel"> |
| 29 | + <h2>Excel compatible copy/paste manager using DataView</h2> |
| 30 | + <div> |
| 31 | + <strong>Thanks to <a href="https://github.com/Celebio/SlickGrid">Celebio</a>! <a href="http://labs.nereo.com/slick.html">(link to site)</a></strong><br /><br /> |
| 32 | + This is basically the same example than <a href="example-spreadsheet.html">example-spreadsheet.html</a>, |
| 33 | + with the support of external excel-compatible software clipboard<br /> |
| 34 | + </div> |
| 35 | + <h2>Paste from Excel-compatible:</h2> |
| 36 | + <ul> |
| 37 | + <li>Copy a range of cells to clipboard in Excel</li> |
| 38 | + <li>Select a cell on slickgrid</li> |
| 39 | + <li>Use Ctrl-V keyboard shortcut to paste from the clipboard</li> |
| 40 | + <li>Adds rows to bottom of grid if paste overflows</li> |
| 41 | + </ul> |
| 42 | + <h2>Copy for Excel-compatible:</h2> |
| 43 | + <ul> |
| 44 | + <li>Select a range of cells with a mouse</li> |
| 45 | + <li>Use Ctrl-C shortcut to copy cells</li> |
| 46 | + <li>You can paste the tabular data into Excel</li> |
| 47 | + </ul> |
| 48 | + |
| 49 | + <h2>Undo/redo support :</h2> |
| 50 | + <ul> |
| 51 | + <li>Use buttons to undo/redo copy/paste</li> |
| 52 | + </ul> |
| 53 | + <button onclick="undoRedoBuffer.undo()"><img src="../images/arrow_undo.png" align="absmiddle"> Undo</button> |
| 54 | + <button onclick="undoRedoBuffer.redo()"><img src="../images/arrow_redo.png" align="absmiddle"> Redo</button> |
| 55 | + <h2>View Source:</h2> |
| 56 | + <ul> |
| 57 | + <li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example-excel-compatible-spreadsheet.html" target="_sourcewindow"> View the source for this example on Github</a></li> |
| 58 | + </ul> |
| 59 | + </div> |
| 60 | +</div> |
| 61 | + |
| 62 | +<script src="../lib/firebugx.js"></script> |
| 63 | + |
| 64 | +<script src="../lib/jquery-1.11.2.min.js"></script> |
| 65 | +<script src="../lib/jquery-ui-1.11.3.min.js"></script> |
| 66 | +<script src="../lib/jquery.event.drag-2.3.0.js"></script> |
| 67 | + |
| 68 | +<script src="../slick.core.js"></script> |
| 69 | +<script src="../plugins/slick.autotooltips.js"></script> |
| 70 | +<script src="../plugins/slick.cellrangedecorator.js"></script> |
| 71 | +<script src="../plugins/slick.cellrangeselector.js"></script> |
| 72 | +<script src="../plugins/slick.cellexternalcopymanager.js"></script> |
| 73 | +<script src="../plugins/slick.cellselectionmodel.js"></script> |
| 74 | +<script src="../slick.editors.js"></script> |
| 75 | +<script src="../slick.formatters.js"></script> |
| 76 | +<script src="../slick.grid.js"></script> |
| 77 | +<script src="../slick.dataview.js"></script> |
| 78 | + |
| 79 | +<script> |
| 80 | + var grid; |
| 81 | + var grid2; |
| 82 | + var data = []; |
| 83 | + var data2 = []; |
| 84 | + |
| 85 | + function getVal(item, columnDef) { |
| 86 | + //return dataView.getItemById(item.id)[columnDef.field]; |
| 87 | + return item[columnDef.field]; |
| 88 | + } |
| 89 | + |
| 90 | + function setVal(item, columnDef, value) { |
| 91 | + item[columnDef.field] = value; |
| 92 | +// dataView.updateItem(item.id, item); |
| 93 | + } |
| 94 | + |
| 95 | + var options = { |
| 96 | + editable: true, |
| 97 | + enableAddRow: true, |
| 98 | + enableCellNavigation: true, |
| 99 | + asyncEditorLoading: false, |
| 100 | + autoEdit: false, |
| 101 | + dataItemColumnValueExtractor: getVal, |
| 102 | + dataItemColumnValueSetter: setVal |
| 103 | + }; |
| 104 | + |
| 105 | + var undoRedoBuffer = { |
| 106 | + commandQueue : [], |
| 107 | + commandCtr : 0, |
| 108 | + |
| 109 | + queueAndExecuteCommand : function(editCommand) { |
| 110 | + this.commandQueue[this.commandCtr] = editCommand; |
| 111 | + this.commandCtr++; |
| 112 | + editCommand.execute(); |
| 113 | + }, |
| 114 | + |
| 115 | + undo : function() { |
| 116 | + if (this.commandCtr == 0) { return; } |
| 117 | + |
| 118 | + this.commandCtr--; |
| 119 | + var command = this.commandQueue[this.commandCtr]; |
| 120 | + |
| 121 | + if (command && Slick.GlobalEditorLock.cancelCurrentEdit()) { |
| 122 | + command.undo(); |
| 123 | + } |
| 124 | + }, |
| 125 | + redo : function() { |
| 126 | + if (this.commandCtr >= this.commandQueue.length) { return; } |
| 127 | + var command = this.commandQueue[this.commandCtr]; |
| 128 | + this.commandCtr++; |
| 129 | + if (command && Slick.GlobalEditorLock.cancelCurrentEdit()) { |
| 130 | + command.execute(); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // undo shortcut |
| 136 | + $(document).keydown(function(e) |
| 137 | + { |
| 138 | + if (e.which == 90 && (e.ctrlKey || e.metaKey)) { // CTRL + (shift) + Z |
| 139 | + if (e.shiftKey){ |
| 140 | + undoRedoBuffer.redo(); |
| 141 | + } else { |
| 142 | + undoRedoBuffer.undo(); |
| 143 | + } |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + var newRowIds = 0; |
| 148 | + |
| 149 | + var pluginOptions = { |
| 150 | + clipboardCommandHandler: function(editCommand){ undoRedoBuffer.queueAndExecuteCommand.call(undoRedoBuffer,editCommand); }, |
| 151 | + readOnlyMode : false, |
| 152 | + includeHeaderWhenCopying : false, |
| 153 | + newRowCreator: function(count) { |
| 154 | + for (var i = 0; i < count; i++) { |
| 155 | + var item = { |
| 156 | + id: "newRow_" + newRowIds++ |
| 157 | + } |
| 158 | + grid.getData().addItem(item); |
| 159 | + } |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + var columns = [ |
| 164 | + { |
| 165 | + id: "selector", |
| 166 | + name: "", |
| 167 | + field: "num", |
| 168 | + width: 30 |
| 169 | + } |
| 170 | + ]; |
| 171 | + |
| 172 | + for (var i = 0; i < 26; i++) { |
| 173 | + columns.push({ |
| 174 | + id: i, |
| 175 | + name: String.fromCharCode("A".charCodeAt(0) + i), |
| 176 | + field: i, |
| 177 | + width: 60//, |
| 178 | + //editor: Slick.Editors.Text |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + columns[4] = {id: "%", name: "% Complete", field: "percentComplete", width: 80, resizable: false, formatter: Slick.Formatters.PercentCompleteBar, editor: Slick.Editors.PercentComplete}; |
| 183 | + columns[5] = {id: "start", name: "Start", field: "start", minWidth: 60, editor: Slick.Editors.Date}; |
| 184 | + |
| 185 | + |
| 186 | + $(function () { |
| 187 | + for (var i = 0; i < 100; i++) { |
| 188 | + var d = (data[i] = {}); |
| 189 | + d["id"] = i; |
| 190 | + d["num"] = i; |
| 191 | + for (var j = 0; j < 26; j++) { |
| 192 | + d[j] = j+i; |
| 193 | + } |
| 194 | + d["percentComplete"] = Math.round(Math.random() * 100); |
| 195 | + d["start"] = new Date(Math.round(Math.random() * 1000000000)); |
| 196 | + d["weekCalendar"] = [true, true, true, true, true, true, true, true, true, true, false, false, false, false]; |
| 197 | + } |
| 198 | + |
| 199 | + dataView = new Slick.Data.DataView(); |
| 200 | + dataView.setItems(data); |
| 201 | + grid = new Slick.Grid("#myGrid", dataView, columns, options); |
| 202 | + grid.setSelectionModel(new Slick.CellSelectionModel()); |
| 203 | + grid.registerPlugin(new Slick.AutoTooltips()); |
| 204 | + |
| 205 | + // set keyboard focus on the grid |
| 206 | + grid.getCanvasNode().focus(); |
| 207 | + |
| 208 | + grid.registerPlugin(new Slick.CellExternalCopyManager(pluginOptions)); |
| 209 | + |
| 210 | + grid.onCellChange.subscribe(function (e, args) { |
| 211 | + dataView.updateItem(args.item.id, args.item); |
| 212 | + }); |
| 213 | + |
| 214 | + grid.onAddNewRow.subscribe(function (e, args) { |
| 215 | + var item = args.item; |
| 216 | + var column = args.column; |
| 217 | + grid.invalidateRow(data.length); |
| 218 | + data.push(item); |
| 219 | + grid.updateRowCount(); |
| 220 | + grid.render(); |
| 221 | + }); |
| 222 | + |
| 223 | + grid2 = new Slick.Grid("#myGrid2", data, columns, options); |
| 224 | + grid2.setSelectionModel(new Slick.CellSelectionModel()); |
| 225 | + |
| 226 | + grid2.registerPlugin(new Slick.CellExternalCopyManager(pluginOptions)); |
| 227 | + |
| 228 | + }) |
| 229 | +</script> |
| 230 | +</body> |
| 231 | +</html> |
0 commit comments