aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/grid/tests/test_mysql_edit.html
blob: dd6a7622ae890550ceb96e077a2e694410cd7e88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html debug="true">
<head>
	<title>dojox.Grid Test: Mysql Table Editing</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
	<style>
		@import "../_grid/tundraGrid.css";
		@import "../../../dojo/resources/dojo.css";
		@import "../../../dijit/themes/tundra/tundra.css";
		@import "../../../dijit/tests/css/dijitTests.css";

		.grid {
			height: 30em;
		}
	</style>

	<script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug:true, parseOnLoad: true"></script>
	<script type="text/javascript" src="../../../dijit/tests/_testCommon.js"></script>
	<!--<script type="text/javascript">
		dojo.require("dojox.grid.Grid");
		dojo.require("dojox.grid.tests.databaseModel");
		dojo.require("dojo.parser");
	</script>-->
	<!-- Debugging -->
	<script type="text/javascript" src="../_grid/lib.js"></script>
	<script type="text/javascript" src="../_grid/drag.js"></script>
	<script type="text/javascript" src="../_grid/scroller.js"></script>
	<script type="text/javascript" src="../_grid/builder.js"></script>
	<script type="text/javascript" src="../_grid/cell.js"></script>
	<script type="text/javascript" src="../_grid/layout.js"></script>
	<script type="text/javascript" src="../_grid/rows.js"></script>
	<script type="text/javascript" src="../_grid/focus.js"></script>
	<script type="text/javascript" src="../_grid/selection.js"></script>
	<script type="text/javascript" src="../_grid/edit.js"></script>
	<script type="text/javascript" src="../_grid/view.js"></script>
	<script type="text/javascript" src="../_grid/views.js"></script>
	<script type="text/javascript" src="../_grid/rowbar.js"></script>
	<script type="text/javascript" src="../_grid/publicEvents.js"></script>
	<script type="text/javascript" src="../VirtualGrid.js"></script>
	<script type="text/javascript" src="../_data/fields.js"></script>
	<script type="text/javascript" src="../_data/model.js"></script>
	<script type="text/javascript" src="../_data/editors.js"></script>
	<script type="text/javascript" src="../_data/dijitEditors.js"></script>
	<script type="text/javascript" src="../Grid.js"></script>
	<script type="text/javascript" src="databaseModel.js"></script>
	<script type="text/javascript">
		var model = new dojox.grid.data.DbTable(null, null, 'support/data.php', "test", "testtbl");
		// simple display of row info; based on model observing.
		modelChange = function() {
			dojo.byId('rowCount').innerHTML = model.count + ' row(s)';
		}
		model.observer(this);
		
		// yay, let's deal with MySql date types, at least a little bit...
		// NOTE: supports only default date formatting YYYY-MM-DD HH:MM:SS or YY-MM-DD HH:MM:SS
		mysqlDateToJsDate = function(inMysqlDateTime, inDateDelim, inTimeDelim) {
			var dt = inMysqlDateTime.split(' '), d = dt[0], t = dt[1], r;
			d = d&&d.split(inDateDelim||'-');
			t = t&&t.split(inTimeDelim||':');
			if (d && d.length == 3) {
				r = new Date();
				r.setYear(d[0]);
				r.setMonth(parseInt(d[1])-1);
				r.setDate(d[2]);
			}
			if (t && t.length == 3) {
				r = r || new Date();
				r.setHours(t[0]);
				r.setMinutes(t[1]);
				r.setSeconds(t[2]);
			}
			return r || new Date(inMysqlDateTime);
		}
		
		jsDateToMySqlDate = function(inDate) {
			var
				d = new Date(inDate),
				y = d.getFullYear(),
				m = dojo.string.pad(d.getMonth() + 1),
				dd = dojo.string.pad(d.getDate())
			return dojo.string.substitute("${0}-${1}-${2}",[y, m, dd]);
		};
		
		// custom simple MySql date formatter
		formatMySqlDate = function(inDatum) {
			return inDatum != dojox.grid.na ? dojo.date.locale.format(mysqlDateToJsDate(inDatum), this.constraint) : dojox.grid.na;
		}
		
		// custom simple MySql date editor
		dojo.declare("mySqlDateEditor", dojox.grid.editors.DateTextBox, {
			format: function(inDatum, inRowIndex){
				inDatum = mysqlDateToJsDate(inDatum);
				return this.inherited(arguments, [inDatum, inRowIndex]);
			},
			getValue: function(inRowIndex){
				var v = this.editor.getValue(), fv = jsDateToMySqlDate(v);
				return fv;
			}
		});
		
		var gridLayout = [ 
			{ type: "dojox.GridRowView", width: "20px" },
			{
			defaultCell: { width: 6, editor: dojox.grid.editors.Dijit }, 
			cells: [[
				{ name: 'Id', styles: 'text-align: right;', editorClass: "dijit.form.NumberTextBox" }, 
				{ name: 'Name', width: 20},
				{ name: 'Message', styles: 'text-align: right;'},
				{ name: 'Date', 
					editor: mySqlDateEditor, 
					formatter: formatMySqlDate, 
					constraint: {selector: "date"}, 
					width: 10, 
					styles: 'text-align:right;'}
			]]}
		];

		function waitMessage() {
			alert('Edit in progress, please wait.');
		}
		
		function getDefaultRow() {
			return ['', '', '', jsDateToMySqlDate(new Date())];
		}
		function addRow() {
			if(model.canModify()){
				grid.addRow(getDefaultRow());
			}else{
				waitMessage();
			}
		}
		
		function removeSelected(){
			if(model.canModify()){
				grid.removeSelectedRows();
			}else{
				waitMessage();
			}
		}
	</script>
</head>
<body class="tundra">
	<h1>dojox.Grid Test: Mysql Table Editing</h1>
	<br>
	<button onclick="addRow()">Add Row</button>&nbsp;&nbsp;
	<button onclick="removeSelected()">Remove Selected</button>&nbsp;&nbsp;
	<button onclick="grid.edit.apply()">Apply Edit</button>&nbsp;&nbsp;
	<button onclick="grid.edit.cancel()">Cancel Edit</button>&nbsp;&nbsp;
	<button onclick="grid.refresh()">Refresh</button>
	<br><br>
	<div jsId="grid" class="grid" structure="gridLayout" dojoType="dojox.Grid" model="model" singleClickEdit="true" autoWidth="true"></div>
	<div id="rowCount"></div>
	<p>Note: This test requires MySql and PHP and works with the database table available in support/testtbl.sql.</p>
</body>
</html>