|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Checkbox select column</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Checkbox select column</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
/* css to counter global site css */ |
|
|
30 |
.example table { |
|
|
31 |
width: auto; |
|
|
32 |
} |
|
|
33 |
|
|
|
34 |
.yui3-skin-sam .yui3-datatable-col-select { |
|
|
35 |
text-align: center; |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
#processed { |
|
|
39 |
margin-top: 2em; |
|
|
40 |
border: 2px inset; |
|
|
41 |
border-radius: 5px; |
|
|
42 |
padding: 1em; |
|
|
43 |
list-style: none; |
|
|
44 |
} |
|
|
45 |
</style> |
|
|
46 |
|
|
|
47 |
<div class="intro"> |
|
|
48 |
<p> |
|
|
49 |
This example shows one method for managing a selection checkbox column |
|
|
50 |
in a DataTable. |
|
|
51 |
</p> |
|
|
52 |
</div> |
|
|
53 |
|
|
|
54 |
<div class="example yui3-skin-sam"> |
|
|
55 |
<div id="dtable"></div> |
|
|
56 |
<button id="btnSelected" class="yui3-button">Process Selections</button> |
|
|
57 |
<button id="btnClearSelected" class="yui3-button">Clear Selections</button> |
|
|
58 |
|
|
|
59 |
<h4>Processed:</h4> |
|
|
60 |
<ul id="processed"> |
|
|
61 |
<li>(None)</li> |
|
|
62 |
</ul> |
|
|
63 |
</div> |
|
|
64 |
<script> |
|
|
65 |
YUI({ filter: 'raw' }).use( "datatable-sort", "datatable-scroll", "cssbutton", function (Y) { |
|
|
66 |
|
|
|
67 |
var ports = [ |
|
|
68 |
{ port:20, pname:'FTP_data',ptitle:'File Transfer Process Data' }, |
|
|
69 |
{ port:21, pname:'FTP', ptitle:'File Transfer Process' }, |
|
|
70 |
{ port:22, pname:'SSH', ptitle:'Secure Shell' }, |
|
|
71 |
{ port:23, pname:'TELNET', ptitle:'Telnet remote communications' }, |
|
|
72 |
{ port:25, pname:'SMTP', ptitle:'Simple Mail Transfer Protocol' }, |
|
|
73 |
{ port:43, pname:'WHOIS', ptitle:'whois Identification' }, |
|
|
74 |
{ port:53, pname:'DNS', ptitle:'Domain Name Service' }, |
|
|
75 |
{ port:68, pname:'DHCP', ptitle:'Dynamic Host Control Protocol' }, |
|
|
76 |
{ port:79, pname:'FINGER', ptitle:'Finger Identification' }, |
|
|
77 |
{ port:80, pname:'HTTP', ptitle:'HyperText Transfer Protocol' }, |
|
|
78 |
{ port:110, pname:'POP3', ptitle:'Post Office Protocol v3' }, |
|
|
79 |
{ port:115, pname:'SFTP', ptitle:'Secure File Transfer Protocol' }, |
|
|
80 |
{ port:119, pname:'NNTP', ptitle:'Network New Transfer Protocol' }, |
|
|
81 |
{ port:123, pname:'NTP', ptitle:'Network Time Protocol' }, |
|
|
82 |
{ port:139, pname:'NetBIOS',ptitle:'NetBIOS Communication' }, |
|
|
83 |
{ port:143, pname:'IMAP', ptitle:'Internet Message Access Protocol' }, |
|
|
84 |
{ port:161, pname:'SNMP', ptitle:'Simple Network Management Protocol' }, |
|
|
85 |
{ port:194, pname:'IRC', ptitle:'Internet Relay Chat' }, |
|
|
86 |
{ port:220, pname:'IMAP3', ptitle:'Internet Message Access Protocol v3' }, |
|
|
87 |
{ port:389, pname:'LDAP', ptitle:'Lightweight Directory Access Protocol' }, |
|
|
88 |
{ port:443, pname:'SSL', ptitle:'Secure Socket Layer' }, |
|
|
89 |
{ port:445, pname:'SMB', ptitle:'NetBIOS over TCP' }, |
|
|
90 |
{ port:993, pname:'SIMAP', ptitle:'Secure IMAP Mail' }, |
|
|
91 |
{ port:995, pname:'SPOP', ptitle:'Secure POP Mail' } |
|
|
92 |
]; |
|
|
93 |
|
|
|
94 |
var table = new Y.DataTable({ |
|
|
95 |
columns : [ |
|
|
96 |
{ key: 'select', |
|
|
97 |
allowHTML: true, // to avoid HTML escaping |
|
|
98 |
label: '<input type="checkbox" class="protocol-select-all" title="Toggle ALL records"/>', |
|
|
99 |
formatter: '<input type="checkbox" checked/>', |
|
|
100 |
emptyCellValue: '<input type="checkbox"/>' |
|
|
101 |
}, |
|
|
102 |
{ key: 'port', label: 'Port No.' }, |
|
|
103 |
{ key: 'pname', label: 'Protocol' }, |
|
|
104 |
{ key: 'ptitle', label: 'Common Name' } |
|
|
105 |
], |
|
|
106 |
data : ports, |
|
|
107 |
scrollable: 'y', |
|
|
108 |
height : '250px', |
|
|
109 |
sortable : ['port','pname'], |
|
|
110 |
sortBy : 'port', |
|
|
111 |
recordType: ['select', 'port', 'pname', 'ptitle'] |
|
|
112 |
}).render("#dtable"); |
|
|
113 |
|
|
|
114 |
// To avoid checkbox click causing harmless error in sorting |
|
|
115 |
// Workaround for bug #2532244 |
|
|
116 |
table.detach('*:change'); |
|
|
117 |
|
|
|
118 |
//---------------- |
|
|
119 |
// "checkbox" Click listeners ... |
|
|
120 |
//---------------- |
|
|
121 |
|
|
|
122 |
// Define a listener on the DT first column for each record's "checkbox", |
|
|
123 |
// to set the value of <code>select</code> to the checkbox setting |
|
|
124 |
table.delegate("click", function(e){ |
|
|
125 |
// undefined to trigger the emptyCellValue |
|
|
126 |
var checked = e.target.get('checked') || undefined; |
|
|
127 |
|
|
|
128 |
this.getRecord(e.target).set('select', checked); |
|
|
129 |
|
|
|
130 |
// Uncheck the header checkbox |
|
|
131 |
this.get('contentBox') |
|
|
132 |
.one('.protocol-select-all').set('checked', false); |
|
|
133 |
}, ".yui3-datatable-data .yui3-datatable-col-select input", table); |
|
|
134 |
|
|
|
135 |
|
|
|
136 |
// Also define a listener on the single TH "checkbox" to |
|
|
137 |
// toggle all of the checkboxes |
|
|
138 |
table.delegate('click', function (e) { |
|
|
139 |
// undefined to trigger the emptyCellValue |
|
|
140 |
var checked = e.target.get('checked') || undefined; |
|
|
141 |
|
|
|
142 |
// Set the selected attribute in all records in the ModelList silently |
|
|
143 |
// to avoid each update triggering a table update |
|
|
144 |
this.data.invoke('set', 'select', checked, { silent: true }); |
|
|
145 |
|
|
|
146 |
// Update the table now that all records have been updated |
|
|
147 |
this.syncUI(); |
|
|
148 |
}, '.protocol-select-all', table); |
|
|
149 |
|
|
|
150 |
//---------------- |
|
|
151 |
// CSS-Button click handlers .... |
|
|
152 |
//---------------- |
|
|
153 |
function process() { |
|
|
154 |
var ml = table.data, |
|
|
155 |
msg = '', |
|
|
156 |
template = '<li>Record index = {index} Data = {port} : {pname}</li>'; |
|
|
157 |
|
|
|
158 |
ml.each(function (item, i) { |
|
|
159 |
var data = item.getAttrs(['select', 'port', 'pname']); |
|
|
160 |
|
|
|
161 |
if (data.select) { |
|
|
162 |
data.index = i; |
|
|
163 |
msg += Y.Lang.sub(template, data); |
|
|
164 |
} |
|
|
165 |
}); |
|
|
166 |
|
|
|
167 |
Y.one("#processed").setHTML(msg || '<li>(None)</li>'); |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
Y.one("#btnSelected").on("click", process); |
|
|
171 |
|
|
|
172 |
Y.one("#btnClearSelected").on("click",function () { |
|
|
173 |
table.data.invoke('set', 'select', undefined); |
|
|
174 |
|
|
|
175 |
// Uncheck the header checkbox |
|
|
176 |
table.get('contentBox') |
|
|
177 |
.one('.protocol-select-all').set('checked', false); |
|
|
178 |
|
|
|
179 |
process(); |
|
|
180 |
}); |
|
|
181 |
|
|
|
182 |
}); |
|
|
183 |
|
|
|
184 |
</script> |
|
|
185 |
|
|
|
186 |
<h2>Sample Data</h2> |
|
|
187 |
|
|
|
188 |
<p> |
|
|
189 |
This example will use a local Javascript array of data that includes some |
|
|
190 |
common Internet port socket numbers and protocol names: |
|
|
191 |
</p> |
|
|
192 |
|
|
|
193 |
<pre class="code prettyprint">var ports = [ |
|
|
194 |
{ port:20, pname:'FTP_data',ptitle:'File Transfer Process Data' }, |
|
|
195 |
{ port:21, pname:'FTP', ptitle:'File Transfer Process' }, |
|
|
196 |
{ port:22, pname:'SSH', ptitle:'Secure Shell' }, |
|
|
197 |
{ port:23, pname:'TELNET', ptitle:'Telnet remote communications' }, |
|
|
198 |
... // data continues |
|
|
199 |
];</pre> |
|
|
200 |
|
|
|
201 |
|
|
|
202 |
<h2>The DataTable</h2> |
|
|
203 |
|
|
|
204 |
<p> |
|
|
205 |
Our DataTable for this example will utilize a custom formatter as the first |
|
|
206 |
column, to display a standard HTML INPUT[type=checkbox] element as an |
|
|
207 |
indication that the record is desired to be "selected" for additional |
|
|
208 |
processing. But that checkbox won't work on its own, because if a "sort" |
|
|
209 |
action happens after the checkbox is clicked the "check" status is lost! |
|
|
210 |
</p> |
|
|
211 |
|
|
|
212 |
<p> |
|
|
213 |
A way to get around this is to create a binding of the checkbox to an |
|
|
214 |
attribute of <b>each record</b> which will remain with the record even upon |
|
|
215 |
sorts, edits, or other modifications to the record. This is accomplished |
|
|
216 |
by defining a custom <code>recordType</code> for the DataTable that incorporates all |
|
|
217 |
of our standard <code>data</code> for our table but also defines a new Attribute |
|
|
218 |
(called <code>select</code> here) that is a boolean value to track whether the record |
|
|
219 |
is selected. |
|
|
220 |
</p> |
|
|
221 |
|
|
|
222 |
<p> |
|
|
223 |
The implementation of these methods is shown below, where we have defined a |
|
|
224 |
custom <code>formatter</code> and <code>emptyCellValue</code> for the "select" column that |
|
|
225 |
creates a checked or unchecked checkbox depending on the state of the |
|
|
226 |
attribute, and defines a custom <code>recordType</code> with our new attribute added. |
|
|
227 |
Additionally, we incorporate (a) scrolling and (b) sorting to demonstrate |
|
|
228 |
that this technique works. |
|
|
229 |
</p> |
|
|
230 |
|
|
|
231 |
<pre class="code prettyprint">var table = new Y.DataTable({ |
|
|
232 |
columns : [ |
|
|
233 |
{ key: 'select', |
|
|
234 |
allowHTML: true, // to avoid HTML escaping |
|
|
235 |
label: '<input type="checkbox" class="protocol-select-all" title="Toggle ALL records"/>', |
|
|
236 |
formatter: '<input type="checkbox" checked/>', |
|
|
237 |
emptyCellValue: '<input type="checkbox"/>' |
|
|
238 |
}, |
|
|
239 |
{ key: 'port', label: 'Port No.' }, |
|
|
240 |
{ key: 'pname', label: 'Protocol' }, |
|
|
241 |
{ key: 'ptitle', label: 'Common Name' } |
|
|
242 |
], |
|
|
243 |
data : ports, |
|
|
244 |
scrollable: 'y', |
|
|
245 |
height : '250px', |
|
|
246 |
sortable : ['port','pname'], |
|
|
247 |
sortBy : 'port', |
|
|
248 |
recordType: ['select', 'port', 'pname', 'ptitle'] |
|
|
249 |
}).render("#dtable");</pre> |
|
|
250 |
|
|
|
251 |
|
|
|
252 |
<h2>The Checkbox Listeners</h2> |
|
|
253 |
|
|
|
254 |
<p> |
|
|
255 |
Having a DataTable with a bunch of checkboxes in it may look cool (or |
|
|
256 |
not!), but we also need to define what to do when they are checked. Since |
|
|
257 |
the column formatter for the first column creates the checkboxes, we |
|
|
258 |
delegate "click" listeners from the DataTable for the two types of |
|
|
259 |
checkboxes—the "check all" checkbox in the header and the individual |
|
|
260 |
checkboxes on each data row. |
|
|
261 |
</p> |
|
|
262 |
|
|
|
263 |
<p> |
|
|
264 |
Be sure to <a href="../event/delegation.html">avoid subscribing to events |
|
|
265 |
directly on elements in each row</a> of a DataTable. |
|
|
266 |
</p> |
|
|
267 |
|
|
|
268 |
<pre class="code prettyprint">// Define a listener on the DT first column for each record's checkbox, |
|
|
269 |
// to set the value of `select` to the checkbox setting |
|
|
270 |
table.delegate("click", function(e){ |
|
|
271 |
// undefined to trigger the emptyCellValue |
|
|
272 |
var checked = e.target.get('checked') || undefined; |
|
|
273 |
|
|
|
274 |
this.getRecord(e.target).set('select', checked); |
|
|
275 |
|
|
|
276 |
// Uncheck the header checkbox |
|
|
277 |
this.get('contentBox') |
|
|
278 |
.one('.protocol-select-all').set('checked', false); |
|
|
279 |
}, ".yui3-datatable-data .yui3-datatable-col-select input", table); |
|
|
280 |
|
|
|
281 |
|
|
|
282 |
// Also define a listener on the single TH checkbox to |
|
|
283 |
// toggle all of the checkboxes |
|
|
284 |
table.delegate('click', function (e) { |
|
|
285 |
// undefined to trigger the emptyCellValue |
|
|
286 |
var checked = e.target.get('checked') || undefined; |
|
|
287 |
|
|
|
288 |
// Set the selected attribute in all records in the ModelList silently |
|
|
289 |
// to avoid each update triggering a table update |
|
|
290 |
this.data.invoke('set', 'select', checked, { silent: true }); |
|
|
291 |
|
|
|
292 |
// Update the table now that all records have been updated |
|
|
293 |
this.syncUI(); |
|
|
294 |
}, '.protocol-select-all', table);</pre> |
|
|
295 |
|
|
|
296 |
|
|
|
297 |
<h2>Button Click Handlers</h2> |
|
|
298 |
|
|
|
299 |
<p> |
|
|
300 |
The bulk of the nitty-gritty is done now. We'll just wire up a button to |
|
|
301 |
process the checked records and another to clear the selection. |
|
|
302 |
</p> |
|
|
303 |
|
|
|
304 |
<pre class="code prettyprint">function process() { |
|
|
305 |
var ml = table.data, |
|
|
306 |
msg = '', |
|
|
307 |
template = '<li>Record index = {index} Data = {port} : {pname}</li>'; |
|
|
308 |
|
|
|
309 |
ml.each(function (item, i) { |
|
|
310 |
var data = item.getAttrs(['select', 'port', 'pname']); |
|
|
311 |
|
|
|
312 |
if (data.select) { |
|
|
313 |
data.index = i; |
|
|
314 |
msg += Y.Lang.sub(template, data); |
|
|
315 |
} |
|
|
316 |
}); |
|
|
317 |
|
|
|
318 |
Y.one("#processed").setHTML(msg || '<li>(None)</li>'); |
|
|
319 |
} |
|
|
320 |
|
|
|
321 |
Y.one("#btnSelected").on("click", process); |
|
|
322 |
|
|
|
323 |
Y.one("#btnClearSelected").on("click",function () { |
|
|
324 |
table.data.invoke('set', 'select', undefined); |
|
|
325 |
|
|
|
326 |
// Uncheck the header checkbox |
|
|
327 |
table.get('contentBox') |
|
|
328 |
.one('.protocol-select-all').set('checked', false); |
|
|
329 |
|
|
|
330 |
process(); |
|
|
331 |
});</pre> |
|
|
332 |
|
|
|
333 |
|
|
|
334 |
<p> |
|
|
335 |
Note that another option for capturing all the checked checkboxes would be |
|
|
336 |
<code>table.get('contentBox').all('.yui3-datatable-col-select input:checked')</code>. |
|
|
337 |
To make sure that was supported across all browsers, we'd then need to |
|
|
338 |
include the <code>selector-css3</code> module in our <code>use()</code> statement. |
|
|
339 |
</p> |
|
|
340 |
|
|
|
341 |
<p> |
|
|
342 |
Another improvement that could be made for HTML5 compliant clients would be |
|
|
343 |
to add in <code>localStorage</code> access to save the checked record data to the |
|
|
344 |
browser environment. You can see how to do this in the |
|
|
345 |
<a href="../app/app-todo.html">App Framework's Todo List example</a>. |
|
|
346 |
</p> |
|
|
347 |
|
|
|
348 |
|
|
|
349 |
<h2>Full Code Listing</h2> |
|
|
350 |
|
|
|
351 |
<h3>CSS</h3> |
|
|
352 |
|
|
|
353 |
<pre class="code prettyprint">.yui3-skin-sam .yui3-datatable-col-select { |
|
|
354 |
text-align: center; |
|
|
355 |
} |
|
|
356 |
|
|
|
357 |
#processed { |
|
|
358 |
margin-top: 2em; |
|
|
359 |
border: 2px inset; |
|
|
360 |
border-radius: 5px; |
|
|
361 |
padding: 1em; |
|
|
362 |
list-style: none; |
|
|
363 |
}</pre> |
|
|
364 |
|
|
|
365 |
|
|
|
366 |
<h3>HTML Markup</h3> |
|
|
367 |
<p> |
|
|
368 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
369 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
370 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
371 |
</p> |
|
|
372 |
<pre class="code prettyprint"><div class="example yui3-skin-sam"> <!-- You need this skin class --> |
|
|
373 |
<div id="dtable"></div> |
|
|
374 |
<button id="btnSelected" class="yui3-button">Process Selections</button> |
|
|
375 |
<button id="btnClearSelected" class="yui3-button">Clear Selections</button> |
|
|
376 |
|
|
|
377 |
<h4>Processed:</h4> |
|
|
378 |
<ul id="processed"> |
|
|
379 |
<li>(None)</li> |
|
|
380 |
</ul> |
|
|
381 |
</div></pre> |
|
|
382 |
|
|
|
383 |
|
|
|
384 |
<h3>Javascript</h3> |
|
|
385 |
|
|
|
386 |
<pre class="code prettyprint">YUI({ filter: 'raw' }).use( "datatable-sort", "datatable-scroll", "cssbutton", function (Y) { |
|
|
387 |
|
|
|
388 |
var ports = [ |
|
|
389 |
{ port:20, pname:'FTP_data',ptitle:'File Transfer Process Data' }, |
|
|
390 |
{ port:21, pname:'FTP', ptitle:'File Transfer Process' }, |
|
|
391 |
{ port:22, pname:'SSH', ptitle:'Secure Shell' }, |
|
|
392 |
{ port:23, pname:'TELNET', ptitle:'Telnet remote communications' }, |
|
|
393 |
{ port:25, pname:'SMTP', ptitle:'Simple Mail Transfer Protocol' }, |
|
|
394 |
{ port:43, pname:'WHOIS', ptitle:'whois Identification' }, |
|
|
395 |
{ port:53, pname:'DNS', ptitle:'Domain Name Service' }, |
|
|
396 |
{ port:68, pname:'DHCP', ptitle:'Dynamic Host Control Protocol' }, |
|
|
397 |
{ port:79, pname:'FINGER', ptitle:'Finger Identification' }, |
|
|
398 |
{ port:80, pname:'HTTP', ptitle:'HyperText Transfer Protocol' }, |
|
|
399 |
{ port:110, pname:'POP3', ptitle:'Post Office Protocol v3' }, |
|
|
400 |
{ port:115, pname:'SFTP', ptitle:'Secure File Transfer Protocol' }, |
|
|
401 |
{ port:119, pname:'NNTP', ptitle:'Network New Transfer Protocol' }, |
|
|
402 |
{ port:123, pname:'NTP', ptitle:'Network Time Protocol' }, |
|
|
403 |
{ port:139, pname:'NetBIOS',ptitle:'NetBIOS Communication' }, |
|
|
404 |
{ port:143, pname:'IMAP', ptitle:'Internet Message Access Protocol' }, |
|
|
405 |
{ port:161, pname:'SNMP', ptitle:'Simple Network Management Protocol' }, |
|
|
406 |
{ port:194, pname:'IRC', ptitle:'Internet Relay Chat' }, |
|
|
407 |
{ port:220, pname:'IMAP3', ptitle:'Internet Message Access Protocol v3' }, |
|
|
408 |
{ port:389, pname:'LDAP', ptitle:'Lightweight Directory Access Protocol' }, |
|
|
409 |
{ port:443, pname:'SSL', ptitle:'Secure Socket Layer' }, |
|
|
410 |
{ port:445, pname:'SMB', ptitle:'NetBIOS over TCP' }, |
|
|
411 |
{ port:993, pname:'SIMAP', ptitle:'Secure IMAP Mail' }, |
|
|
412 |
{ port:995, pname:'SPOP', ptitle:'Secure POP Mail' } |
|
|
413 |
]; |
|
|
414 |
|
|
|
415 |
var table = new Y.DataTable({ |
|
|
416 |
columns : [ |
|
|
417 |
{ key: 'select', |
|
|
418 |
allowHTML: true, // to avoid HTML escaping |
|
|
419 |
label: '<input type="checkbox" class="protocol-select-all" title="Toggle ALL records"/>', |
|
|
420 |
formatter: '<input type="checkbox" checked/>', |
|
|
421 |
emptyCellValue: '<input type="checkbox"/>' |
|
|
422 |
}, |
|
|
423 |
{ key: 'port', label: 'Port No.' }, |
|
|
424 |
{ key: 'pname', label: 'Protocol' }, |
|
|
425 |
{ key: 'ptitle', label: 'Common Name' } |
|
|
426 |
], |
|
|
427 |
data : ports, |
|
|
428 |
scrollable: 'y', |
|
|
429 |
height : '250px', |
|
|
430 |
sortable : ['port','pname'], |
|
|
431 |
sortBy : 'port', |
|
|
432 |
recordType: ['select', 'port', 'pname', 'ptitle'] |
|
|
433 |
}).render("#dtable"); |
|
|
434 |
|
|
|
435 |
// To avoid checkbox click causing harmless error in sorting |
|
|
436 |
// Workaround for bug #2532244 |
|
|
437 |
table.detach('*:change'); |
|
|
438 |
|
|
|
439 |
//---------------- |
|
|
440 |
// "checkbox" Click listeners ... |
|
|
441 |
//---------------- |
|
|
442 |
|
|
|
443 |
// Define a listener on the DT first column for each record's "checkbox", |
|
|
444 |
// to set the value of `select` to the checkbox setting |
|
|
445 |
table.delegate("click", function(e){ |
|
|
446 |
// undefined to trigger the emptyCellValue |
|
|
447 |
var checked = e.target.get('checked') || undefined; |
|
|
448 |
|
|
|
449 |
this.getRecord(e.target).set('select', checked); |
|
|
450 |
|
|
|
451 |
// Uncheck the header checkbox |
|
|
452 |
this.get('contentBox') |
|
|
453 |
.one('.protocol-select-all').set('checked', false); |
|
|
454 |
}, ".yui3-datatable-data .yui3-datatable-col-select input", table); |
|
|
455 |
|
|
|
456 |
|
|
|
457 |
// Also define a listener on the single TH "checkbox" to |
|
|
458 |
// toggle all of the checkboxes |
|
|
459 |
table.delegate('click', function (e) { |
|
|
460 |
// undefined to trigger the emptyCellValue |
|
|
461 |
var checked = e.target.get('checked') || undefined; |
|
|
462 |
|
|
|
463 |
// Set the selected attribute in all records in the ModelList silently |
|
|
464 |
// to avoid each update triggering a table update |
|
|
465 |
this.data.invoke('set', 'select', checked, { silent: true }); |
|
|
466 |
|
|
|
467 |
// Update the table now that all records have been updated |
|
|
468 |
this.syncUI(); |
|
|
469 |
}, '.protocol-select-all', table); |
|
|
470 |
|
|
|
471 |
//---------------- |
|
|
472 |
// CSS-Button click handlers .... |
|
|
473 |
//---------------- |
|
|
474 |
function process() { |
|
|
475 |
var ml = table.data, |
|
|
476 |
msg = '', |
|
|
477 |
template = '<li>Record index = {index} Data = {port} : {pname}</li>'; |
|
|
478 |
|
|
|
479 |
ml.each(function (item, i) { |
|
|
480 |
var data = item.getAttrs(['select', 'port', 'pname']); |
|
|
481 |
|
|
|
482 |
if (data.select) { |
|
|
483 |
data.index = i; |
|
|
484 |
msg += Y.Lang.sub(template, data); |
|
|
485 |
} |
|
|
486 |
}); |
|
|
487 |
|
|
|
488 |
Y.one("#processed").setHTML(msg || '<li>(None)</li>'); |
|
|
489 |
} |
|
|
490 |
|
|
|
491 |
Y.one("#btnSelected").on("click", process); |
|
|
492 |
|
|
|
493 |
Y.one("#btnClearSelected").on("click",function () { |
|
|
494 |
table.data.invoke('set', 'select', undefined); |
|
|
495 |
|
|
|
496 |
// Uncheck the header checkbox |
|
|
497 |
table.get('contentBox') |
|
|
498 |
.one('.protocol-select-all').set('checked', false); |
|
|
499 |
|
|
|
500 |
process(); |
|
|
501 |
}); |
|
|
502 |
|
|
|
503 |
});</pre> |
|
|
504 |
|
|
|
505 |
</div> |
|
|
506 |
</div> |
|
|
507 |
</div> |
|
|
508 |
|
|
|
509 |
<div class="yui3-u-1-4"> |
|
|
510 |
<div class="sidebar"> |
|
|
511 |
|
|
|
512 |
|
|
|
513 |
|
|
|
514 |
<div class="sidebox"> |
|
|
515 |
<div class="hd"> |
|
|
516 |
<h2 class="no-toc">Examples</h2> |
|
|
517 |
</div> |
|
|
518 |
|
|
|
519 |
<div class="bd"> |
|
|
520 |
<ul class="examples"> |
|
|
521 |
|
|
|
522 |
|
|
|
523 |
<li data-description="This example illustrates simple DataTable use cases."> |
|
|
524 |
<a href="datatable-basic.html">Basic DataTable</a> |
|
|
525 |
</li> |
|
|
526 |
|
|
|
527 |
|
|
|
528 |
|
|
|
529 |
<li data-description="DataTable loaded with JSON data from a remote webservice via DataSource.Get"> |
|
|
530 |
<a href="datatable-dsget.html">DataTable + DataSource.Get + JSON Data</a> |
|
|
531 |
</li> |
|
|
532 |
|
|
|
533 |
|
|
|
534 |
|
|
|
535 |
<li data-description="DataTable loaded with XML data from a remote webservice via DataSource.IO."> |
|
|
536 |
<a href="datatable-dsio.html">DataTable + DataSource.IO + XML Data</a> |
|
|
537 |
</li> |
|
|
538 |
|
|
|
539 |
|
|
|
540 |
|
|
|
541 |
<li data-description="Custom format data for display."> |
|
|
542 |
<a href="datatable-formatting.html">Formatting Row Data for Display</a> |
|
|
543 |
</li> |
|
|
544 |
|
|
|
545 |
|
|
|
546 |
|
|
|
547 |
<li data-description="DataTable with nested column headers."> |
|
|
548 |
<a href="datatable-nestedcols.html">Nested Column Headers</a> |
|
|
549 |
</li> |
|
|
550 |
|
|
|
551 |
|
|
|
552 |
|
|
|
553 |
<li data-description="DataTable with column sorting."> |
|
|
554 |
<a href="datatable-sort.html">Column Sorting</a> |
|
|
555 |
</li> |
|
|
556 |
|
|
|
557 |
|
|
|
558 |
|
|
|
559 |
<li data-description="DataTable with vertical and/or horizontal scrolling rows."> |
|
|
560 |
<a href="datatable-scroll.html">Scrolling DataTable</a> |
|
|
561 |
</li> |
|
|
562 |
|
|
|
563 |
|
|
|
564 |
|
|
|
565 |
<li data-description="Using DataTable's recordType attribute to create calculated, sortable columns."> |
|
|
566 |
<a href="datatable-recordtype.html">Sortable generated columns</a> |
|
|
567 |
</li> |
|
|
568 |
|
|
|
569 |
|
|
|
570 |
|
|
|
571 |
<li data-description="Populating one DataTable from details in the data of another."> |
|
|
572 |
<a href="datatable-masterdetail.html">Master and detail tables</a> |
|
|
573 |
</li> |
|
|
574 |
|
|
|
575 |
|
|
|
576 |
|
|
|
577 |
<li data-description="Checkbox column that retains checked state when sorting."> |
|
|
578 |
<a href="datatable-chkboxselect.html">Checkbox select column</a> |
|
|
579 |
</li> |
|
|
580 |
|
|
|
581 |
|
|
|
582 |
|
|
|
583 |
|
|
|
584 |
</ul> |
|
|
585 |
</div> |
|
|
586 |
</div> |
|
|
587 |
|
|
|
588 |
|
|
|
589 |
|
|
|
590 |
<div class="sidebox"> |
|
|
591 |
<div class="hd"> |
|
|
592 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
593 |
</div> |
|
|
594 |
|
|
|
595 |
<div class="bd"> |
|
|
596 |
<ul class="examples"> |
|
|
597 |
|
|
|
598 |
|
|
|
599 |
|
|
|
600 |
|
|
|
601 |
|
|
|
602 |
|
|
|
603 |
|
|
|
604 |
|
|
|
605 |
|
|
|
606 |
|
|
|
607 |
|
|
|
608 |
|
|
|
609 |
|
|
|
610 |
|
|
|
611 |
|
|
|
612 |
|
|
|
613 |
|
|
|
614 |
|
|
|
615 |
|
|
|
616 |
|
|
|
617 |
|
|
|
618 |
|
|
|
619 |
<li data-description="Shows how to instantiate multiple Panel instances, and use nested modality to interact with a Datatable."> |
|
|
620 |
<a href="../panel/panel-form.html">Creating a Modal Form</a> |
|
|
621 |
</li> |
|
|
622 |
|
|
|
623 |
|
|
|
624 |
</ul> |
|
|
625 |
</div> |
|
|
626 |
</div> |
|
|
627 |
|
|
|
628 |
</div> |
|
|
629 |
</div> |
|
|
630 |
</div> |
|
|
631 |
</div> |
|
|
632 |
|
|
|
633 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
634 |
<script>prettyPrint();</script> |
|
|
635 |
|
|
|
636 |
<script> |
|
|
637 |
YUI.Env.Tests = { |
|
|
638 |
examples: [], |
|
|
639 |
project: '../assets', |
|
|
640 |
assets: '../assets/datatable', |
|
|
641 |
name: 'datatable-chkboxselect', |
|
|
642 |
title: 'Checkbox select column', |
|
|
643 |
newWindow: '', |
|
|
644 |
auto: false |
|
|
645 |
}; |
|
|
646 |
YUI.Env.Tests.examples.push('datatable-basic'); |
|
|
647 |
YUI.Env.Tests.examples.push('datatable-dsget'); |
|
|
648 |
YUI.Env.Tests.examples.push('datatable-dsio'); |
|
|
649 |
YUI.Env.Tests.examples.push('datatable-formatting'); |
|
|
650 |
YUI.Env.Tests.examples.push('datatable-nestedcols'); |
|
|
651 |
YUI.Env.Tests.examples.push('datatable-sort'); |
|
|
652 |
YUI.Env.Tests.examples.push('datatable-scroll'); |
|
|
653 |
YUI.Env.Tests.examples.push('datatable-recordtype'); |
|
|
654 |
YUI.Env.Tests.examples.push('datatable-masterdetail'); |
|
|
655 |
YUI.Env.Tests.examples.push('datatable-chkboxselect'); |
|
|
656 |
YUI.Env.Tests.examples.push('panel-form'); |
|
|
657 |
|
|
|
658 |
</script> |
|
|
659 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
660 |
|
|
|
661 |
|
|
|
662 |
|
|
|
663 |
</body> |
|
|
664 |
</html> |