| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
| 2 | "http://www.w3.org/TR/html4/loose.dtd"> |
| 3 | <html debug="true"> |
| 4 | <head> |
| 5 | <title>dojox.grid.Grid Test: Mysql Table Editing</title> |
| 6 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> |
| 7 | <style> |
| 8 | @import "../resources/Grid.css"; |
| 9 | @import "../resources/tundraGrid.css"; |
| 10 | @import "../../../dojo/resources/dojo.css"; |
| 11 | @import "../../../dijit/themes/tundra/tundra.css"; |
| 12 | @import "../../../dijit/tests/css/dijitTests.css"; |
| 13 | |
| 14 | .grid { |
| 15 | height: 30em; |
| 16 | } |
| 17 | </style> |
| 18 | |
| 19 | <script type="text/javascript" src="../../../dojo/dojo.js" |
| 20 | data-dojo-config="isDebug:true, parseOnLoad: true"></script> |
| 21 | <script type="text/javascript"> |
| 22 | dojo.require("dijit.dijit"); |
| 23 | dojo.require("dojox.grid.DataGrid"); |
| 24 | dojo.require("dojox.data.QueryReadStore"); |
| 25 | dojo.require("dojo.parser"); |
| 26 | </script> |
| 27 | <script type="text/javascript"> |
| 28 | dojo.declare("DbStore", dojox.data.QueryReadStore, { |
| 29 | fetch: function(request){ |
| 30 | request.serverQuery = { |
| 31 | database: this.database || '', |
| 32 | table: this.table || '' |
| 33 | } |
| 34 | } |
| 35 | }); |
| 36 | var model = new dojox.grid._data.DbTable(null, null, 'support/data.php', "test", "testtbl"); |
| 37 | // simple display of row info; based on model observing. |
| 38 | modelChange = function() { |
| 39 | dojo.byId('rowCount').innerHTML = model.count + ' row(s)'; |
| 40 | } |
| 41 | model.observer(this); |
| 42 | |
| 43 | // yay, let's deal with MySql date types, at least a little bit... |
| 44 | // NOTE: supports only default date formatting YYYY-MM-DD HH:MM:SS or YY-MM-DD HH:MM:SS |
| 45 | mysqlDateToJsDate = function(inMysqlDateTime, inDateDelim, inTimeDelim) { |
| 46 | var dt = inMysqlDateTime.split(' '), d = dt[0], t = dt[1], r; |
| 47 | d = d&&d.split(inDateDelim||'-'); |
| 48 | t = t&&t.split(inTimeDelim||':'); |
| 49 | if (d && d.length == 3) { |
| 50 | r = new Date(); |
| 51 | r.setYear(d[0]); |
| 52 | r.setMonth(parseInt(d[1])-1); |
| 53 | r.setDate(d[2]); |
| 54 | } |
| 55 | if (t && t.length == 3) { |
| 56 | r = r || new Date(); |
| 57 | r.setHours(t[0]); |
| 58 | r.setMinutes(t[1]); |
| 59 | r.setSeconds(t[2]); |
| 60 | } |
| 61 | return r || new Date(inMysqlDateTime); |
| 62 | } |
| 63 | |
| 64 | jsDateToMySqlDate = function(inDate) { |
| 65 | var |
| 66 | d = new Date(inDate), |
| 67 | y = d.getFullYear(), |
| 68 | m = dojo.string.pad(d.getMonth() + 1), |
| 69 | dd = dojo.string.pad(d.getDate()) |
| 70 | return dojo.string.substitute("${0}-${1}-${2}",[y, m, dd]); |
| 71 | }; |
| 72 | |
| 73 | // custom simple MySql date formatter |
| 74 | formatMySqlDate = function(inDatum) { |
| 75 | return inDatum != dojox.grid.na ? dojo.date.locale.format(mysqlDateToJsDate(inDatum), this.constraint) : dojox.grid.na; |
| 76 | } |
| 77 | |
| 78 | // custom simple MySql date editor |
| 79 | dojo.declare("mySqlDateCell", dojox.grid.cells.DateTextBox, { |
| 80 | format: function(inDatum, inRowIndex){ |
| 81 | inDatum = mysqlDateToJsDate(inDatum); |
| 82 | return this.inherited(arguments, [inDatum, inRowIndex]); |
| 83 | }, |
| 84 | getValue: function(inRowIndex){ |
| 85 | var v = this.editor.getValue(), fv = jsDateToMySqlDate(v); |
| 86 | return fv; |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | var gridLayout = [ |
| 91 | { type: "dojox.grid._RowSelector", width: "20px" }, |
| 92 | { |
| 93 | defaultCell: { width: 6, type: dojox.grid.cells._Widget, editable: true }, |
| 94 | cells: [[ |
| 95 | { name: 'Id', styles: 'text-align: right;', widgetClass: dijit.form.NumberTextBox }, |
| 96 | { name: 'Name', width: 20}, |
| 97 | { name: 'Message', styles: 'text-align: right;'}, |
| 98 | { name: 'Date', |
| 99 | type: mySqlDateEditor, |
| 100 | formatter: formatMySqlDate, |
| 101 | constraint: {selector: "date"}, |
| 102 | width: 10, |
| 103 | styles: 'text-align:right;'} |
| 104 | ]]} |
| 105 | ]; |
| 106 | |
| 107 | function waitMessage() { |
| 108 | alert('Edit in progress, please wait.'); |
| 109 | } |
| 110 | |
| 111 | function getDefaultRow() { |
| 112 | return ['', '', '', jsDateToMySqlDate(new Date())]; |
| 113 | } |
| 114 | function addRow() { |
| 115 | if(model.canModify()){ |
| 116 | grid.addRow(getDefaultRow()); |
| 117 | }else{ |
| 118 | waitMessage(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | function removeSelected(){ |
| 123 | if(model.canModify()){ |
| 124 | grid.removeSelectedRows(); |
| 125 | }else{ |
| 126 | waitMessage(); |
| 127 | } |
| 128 | } |
| 129 | </script> |
| 130 | </head> |
| 131 | <body class="tundra"> |
| 132 | <h1>dojox.grid.Grid Test: Mysql Table Editing</h1> |
| 133 | <br> |
| 134 | <button onclick="addRow()">Add Row</button> |
| 135 | <button onclick="removeSelected()">Remove Selected</button> |
| 136 | <button onclick="grid.edit.apply()">Apply Edit</button> |
| 137 | <button onclick="grid.edit.cancel()">Cancel Edit</button> |
| 138 | <button onclick="grid.render()">Refresh</button> |
| 139 | <br><br> |
| 140 | <div data-dojo-id="grid" class="grid" structure="gridLayout" dojoType="dojox.grid.Grid" |
| 141 | store="model" singleClickEdit="true" autoWidth="true"></div> |
| 142 | <div id="rowCount"></div> |
| 143 | <p>Note: This test requires MySql and PHP and works with the database table available in support/testtbl.sql.</p> |
| 144 | </body> |
| 145 | </html> |