Debian Codegraph @

docs/example-widget-editable.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>jQuery tablesorter 2.0 - Content Editable</title>
6
7 <!-- jQuery -->
8 <script src="js/jquery-latest.min.js"></script>
9
10 <!-- Demo stuff -->
11 <link class="ui-theme" rel="stylesheet" href="css/jquery-ui.min.css">
12 <script src="js/jquery-ui.min.js"></script>
13 <link rel="stylesheet" href="css/jq.css">
14 <link href="css/prettify.css" rel="stylesheet">
15 <script src="js/prettify.js"></script>
16 <script src="js/docs.js"></script>
17
18 <!-- Tablesorter: required -->
19 <link rel="stylesheet" href="../css/theme.blue.css">
20 <link rel="stylesheet" href="../css/theme.ice.css">
21 <script src="../js/jquery.tablesorter.js"></script>
22
23 <script src="../js/widgets/widget-editable.js"></script>
24
25 <style>
26 table.info2.tablesorter-ice { table-layout: auto; }
27 .options th.option { width: 150px; }
28 .info2 th { vertical-align: middle; text-align: center; }
29 .info2 th.col { width: 42%; }
30 .color1 { color: purple; }
31 .color2 { color: blue; }
32 .color3 { color: green; }
33 </style>
34 <style id="css">.tablesorter tbody > tr > td > div[contenteditable=true]:focus {
35 outline: #08f 1px solid;
36 background: #eee;
37 resize: none;
38 }
39 td.no-edit, span.no-edit {
40 background-color: rgba(230,191,153,0.5);
41 }
42 .focused {
43 color: blue;
44 }
45 td.editable_updated {
46 background-color: green;
47 color: white;
48 }</style>
49 <script id="js">$(function() {
50
51 $("#table")
52 .tablesorter({
53 theme : 'blue',
54
55 widgets: ['editable'],
56 widgetOptions: {
57 editable_columns : [0,1,2], // or "0-2" (v2.14.2); point to the columns to make editable (zero-based index)
58 editable_enterToAccept : true, // press enter to accept content, or click outside if false
59 editable_autoAccept : true, // accepts any changes made to the table cell automatically (v2.17.6)
60 editable_autoResort : false, // auto resort after the content has changed.
61 editable_validate : null, // return a valid string: function(text, original, columnIndex) { return text; }
62 editable_focused : function(txt, columnIndex, $element) {
63 // $element is the div, not the td
64 // to get the td, use $element.closest('td')
65 $element.addClass('focused');
66 },
67 editable_blur : function(txt, columnIndex, $element) {
68 // $element is the div, not the td
69 // to get the td, use $element.closest('td')
70 $element.removeClass('focused');
71 },
72 editable_selectAll : function(txt, columnIndex, $element) {
73 // note $element is the div inside of the table cell, so use $element.closest('td') to get the cell
74 // only select everthing within the element when the content starts with the letter "B"
75 return /^b/i.test(txt) && columnIndex === 0;
76 },
77 editable_wrapContent : '<div>', // wrap all editable cell content... makes this widget work in IE, and with autocomplete
78 editable_trimContent : true, // trim content ( removes outer tabs & carriage returns )
79 editable_noEdit : 'no-edit', // class name of cell that is not editable
80 editable_editComplete : 'editComplete' // event fired after the table content has been edited
81 }
82 })
83 // config event variable new in v2.17.6
84 .children('tbody').on('editComplete', 'td', function(event, config) {
85 var $this = $(this),
86 newContent = $this.text(),
87 cellIndex = this.cellIndex, // there shouldn't be any colspans in the tbody
88 rowIndex = $this.closest('tr').attr('id'); // data-row-index stored in row id
89
90 // Do whatever you want here to indicate
91 // that the content was updated
92 $this.addClass( 'editable_updated' ); // green background + white text
93 setTimeout(function() {
94 $this.removeClass( 'editable_updated' );
95 }, 500);
96
97 /*
98 $.post("mysite.php", {
99 "row" : rowIndex,
100 "cell" : cellIndex,
101 "content" : newContent
102 });
103 */
104 });
105
106 });</script>
107 </head>
108 <body>
109 <div id="banner">
110 <h1>table<em>sorter</em></h1>
111 <h2>Content Editable</h2>
112 <h3>Flexible client-side table sorting</h3>
113 <a href="index.html">Back to documentation</a>
114 </div>
115 <div id="main">
116
117 <p></p>
118 <br>
119
120 <div id="root" class="accordion">
121
122 <h3><a href="#">Notes</a></h3>
123 <div>
124 <ul>
125 <li>This widget can not be applied to the original plugin and requires jQuery 1.7+ and a browser that supports <a href="http://caniuse.com/#feat=contenteditable"><code>contenteditable</code> attributes</a> (almost all modern browsers)<br><br></li>
126 <li>In <span class="version">v2.23.0</span>, the <code>editable_columns</code> option will now accept a string with both ranges and single columns, e.g. <code>'1,3-5,7'</code>.</li>
127 <li>In <span class="version">v2.22.2</span>,
128 <ul>
129 <li><kbd>Shift</kbd>+<kbd>Enter</kbd> can now be used to start a new line even if <code>editable_enterToAccept</code> is <code>true</code>.</li>
130 <li>Sorting is now delayed until the editable content has been updated in the cache. This no longer relies on hovering over the table header as this would not be adequate on touch devices.</li>
131 </ul>
132 </li>
133 <li>In <span class="version">v2.22.0</span>,
134 <ul>
135 <li>The cell content now only <em>automatically updates</em> when the user hovers over the table header. Prior to this version, the automatic update would occur when the mouse left the tbody of the table. The reason this is necessary is because initiating a sort would change the row indexing set prior to the cell content being updated, so the cache would end up not matching the table content.</li>
136 <li>Modified the <code>editable_trimContent</code> option to only trim content when set.</li>
137 <li>The widget now works with the contenteditable html (using jQuery's <code>.html()</code>); previously, the widget would only manipulate the text (using jQuery <code>.text()</code>), so any HTML would get stripped out.<br>
138 <span class="label label-info">*Note*</span> Because of this change, if the user presses <kbd>Enter</kbd>, a <code>&lt;div&gt;</code> will be automatically inserted by the browser (not the widget) and any content entered will be added inside this "new line" div. If this is undesirable, use the <code>editable_blur</code> callback to modify the content.</li>
139 <li>If the cell content is already wrapped in a div or span, that element will be made contenteditable. Otherwise any direct children of the table cell will be made contenteditable; prior to this update, all direct child elements (including <code>&lt;br&gt;</code>s were made contenteditable by mistake).</li>
140 </ul>
141 </li>
142 <li>Updated <span class="version">v2.17.6</span>,
143 <ul>
144 <li>Fixed the <code>editable_enterToAccept</code> option to do what it was meant to do, accept when the user presses enter.</li>
145 <li>Added <code>editable_autoAccept</code> option, so that when it is <code>true</code> the original behavior of accepting content changes will be maintained.</li>
146 <li>Added <code>editable_validate</code> option, to allow validating the edited content.</li>
147 <li>Focus is now maintained within the contenteditable element after updating. This makes it easier to tab through the table while editing. This change also fixes <a href="http://stackoverflow.com/q/24947995/145346">this Stackoverflow issue</a>.</li>
148 <li>The <code>editComplete</code> event now passes the table <code>config</code> variable to make it easier to access tablesorter variables.</li>
149 </ul>
150 </li>
151 <li>Updated <span class="version updated">v2.13.2</span>, because of the limitation in IE, if a table cell contains any DIV or SPAN immediately inside the cell, it will be targeted instead of the table cell itself and made content editable. So, if you don't care about IE support, there is no need to include the extra markup.</li>
152 <li>In some browsers, additional CSS is needed to highlight a focused editable table cell. See the CSS block below.</li>
153 <li>Pressing <kbd>Escape</kbd> while editing will cancel any changes.</li>
154 <li>In the demo below, click in any of the first three columns to edit the content, except for the cell containing <span class="no-edit">"Peter"</span>.</li>
155 <li>To prevent a table cell from becoming editable, add the class name <code>"no-edit"</code> to the cell. Set by the <code>editable_noEdit</code> option.</li>
156 </ul>
157 </div>
158
159 <h3><a href="#">When Content Changes are Accepted</a></h3>
160 <div>
161 <table class="info2 tablesorter-ice">
162 <thead>
163 <tr><th style="width:250px" colspan="2" rowspan="2"></th><th colspan="2">editable_enterToAccept</th></tr>
164 <tr><th class="col">true</th><th class="col">false</th></tr>
165 </thead>
166 <tbody>
167 <tr>
168 <th stlye="width:200px" rowspan="2">editable_autoAccept</th>
169 <th style="width:50px">true</th>
170 <td>
171 <ul>
172 <li class="color1">Pressing alt-enter</li>
173 <li class="color2">Pressing enter</li>
174 <li class="color3">Clicking outside of the current editable content.</li>
175 <li class="color3">Moving the mouse outside of the current tbody (this is done because if the content editable is still active when the user clicks on the header to sort the column, all sorts of bad things happen).</li>
176 </ul>
177 </td>
178 <td>
179 <ul>
180 <li class="color1">Pressing alt-enter</li>
181 <li class="color3">Clicking outside of the current editable content.</li>
182 <li class="color3">Moving the mouse outside of the current tbody.</li>
183 </ul>
184 </td>
185 </tr>
186 <tr>
187 <th>false</th>
188 <td>
189 <ul>
190 <li class="color1">Pressing alt-enter</li>
191 <li class="color2">Pressing enter</li>
192 </ul>
193 </td>
194 <td>
195 <ul>
196 <li class="color1">Pressing alt-enter</li>
197 </ul>
198 </td>
199 </tr>
200 </tbody>
201 </table>
202 * <span class="label label-info">Note</span> The content is only accepted when the <code>editable_validation</code> function <em>does not</em> return <code>false</code>.
203 </div>
204
205 <h3><a href="#">Options</a></h3>
206 <div>
207 <h4>Editable widget default options (added inside of tablesorter <code>widgetOptions</code>)</h4>
208 <div>
209 <span class="label label-info">TIP!</span> Click on the link in the function column to reveal full details (or <a href="#" class="toggleAll">toggle</a>|<a href="#" class="showAll">show</a>|<a href="#" class="hideAll">hide</a> all) or double click to update the browser location.
210 </div>
211 <table class="options tablesorter-blue" data-sortlist="[[0,0]]">
212 <thead>
213 <tr><th class="option">Option</th><th class="sorter-false">Description</th></tr>
214 </thead>
215 <tbody>
216
217 <tr id="editable-auto-accept">
218 <td><a href="#" class="permalink">editable_autoAccept</a></td>
219 <td>Accepts any changes made to the table cell automatically <span class="version">v2.17.6</span>
220 <div class="collapsible">
221 <br>
222 If the user clicks outside or tabs out of the edited cell, or moves the mouse out of the current tbody, any changes to the cell will be accepted<br>
223 <br>
224 The only time the content is not accepted is if the user presses the escape key.<br>
225 <br>
226 if <code>false</code>, the acceptance behavior follows the <code>editable_enterToAccept</code> setting.<br>
227 <br>
228 Default value: <code>true</code>
229 </div>
230 </td>
231 </tr>
232
233 <tr id="editable-columns">
234 <td><a href="#" class="permalink">editable_columns</a></td>
235 <td>Contains an array (or range string) of columns numbers you want to have editable content (zero-based index) (<span class="version updated">v2.23.0</span>).
236 <div class="collapsible">
237 <ul>
238 <li>In <span class="version">v2.23.0</span>, this option will now accept both range &amp; comma separated values, e.g. <code>'1,3-5,7'</code>.</li>
239 <li>In tablesorter core <span class="version">v2.14.2</span>, this widget was updated to allow passing a range string in this option, i.e. <code>'0-2'</code> instead of <code>[0,1,2]</code>.</li>
240 <li><code>contenteditable="true"</code> is added to cells within these columns.</li>
241 </ul>
242 Default value: <code>[]</code> (empty array)
243 </div>
244 </td>
245 </tr>
246
247 <tr id="editable-enter-to-accept">
248 <td><a href="#" class="permalink">editable_enterToAccept</a></td>
249 <td>Makes the user press enter to accept the content within the editable table cell
250 <div class="collapsible">
251 <br>
252 if <code>false</code>, clicking outside the cell will accept the content.<br>
253 <br>
254 As of <span class="version">v2.22.2</span>, pressing <kbd>Shift</kbd>+<kbd>Enter</kbd> will add a carriage return and not automatically accept the changes.<br>
255 <br>
256 Default value: <code>true</code>
257 </div>
258 </td>
259 </tr>
260
261 <tr id="editable-auto-resort">
262 <td><a href="#" class="permalink">editable_autoResort</a></td>
263 <td>If <code>true</code>, the column will resort (only if already sorted) after the content has been changed.
264 <div class="collapsible">
265 <br>
266 Default value: <code>false</code>
267 </div>
268 </td>
269 </tr>
270
271 <tr id="editable-no-edit">
272 <td><a href="#" class="permalink">editable_noEdit</a></td>
273 <td>Class name on table cells to search for that are not to become editable.
274 <div class="collapsible">
275 <br>
276 The search is only done within the columns set by the <code>editable_columns</code> option.<br>
277 <br>
278 Default value: <code>'no-edit'</code>
279 </div>
280 </td>
281 </tr>
282
283 <tr id="editable-edit-complete">
284 <td><a href="#" class="permalink">editable_editComplete</a></td>
285 <td>Event fired after the table content has been edited
286 <div class="collapsible">
287 <br>
288 <pre class="prettyprint lang-js">$(function() {
289
290 $('#table')
291 .tablesorter({
292 widgets : ['editable'],
293 widgetOptions : {
294 editable_editComplete : 'giterdone'
295 }
296 })
297 // use delegated event binding
298 .on('giterdone', 'td', function(event, config) {
299 // this = td; the event bubble up
300 console.log( 'new content = ' + $(this).text() );
301 });
302
303 });</pre>
304 Default value: <code>'editComplete'</code>
305 </div>
306 </td>
307 </tr>
308
309 <tr id="editable-focused">
310 <td><a href="#" class="permalink">editable_focused</a></td>
311 <td>Callback function that is executeed when the contenteditable cell is focused (<span class="version">v2.17.8</span>)
312 <div class="collapsible">
313 <br>
314 Set this function to manipulate or adjust the content when the content editable is focused
315 <pre class="prettyprint lang-js">$(function() {
316
317 $('#table').tablesorter({
318 widgets : ['editable'],
319 widgetOptions : {
320 editable_focused : function(txt, columnIndex, $element) {
321 // note $element is the div inside of the table cell, so use $element.closest('td') to get the cell
322 $element.closest('td').addClass('focused');
323 },
324 editable_blur : function(txt, columnIndex, $element) {
325 $element.closest('td').removeClass('focused');
326 }
327 }
328 });
329
330 });</pre>
331 Default value: <code>null</code>
332 </div>
333 </td>
334 </tr>
335
336 <tr id="editable-blur">
337 <td><a href="#" class="permalink">editable_blur</a></td>
338 <td>Callback function that is executeed when the contenteditable cell is no longer focused (<span class="version">v2.17.8</span>)
339 <div class="collapsible">
340 <br>
341 Set this function to manipulate or adjust the content when the content editable is blurred
342 <pre class="prettyprint lang-js">$(function() {
343
344 $('#table').tablesorter({
345 widgets : ['editable'],
346 widgetOptions : {
347 editable_focused : function(txt, columnIndex, $element) {
348 // note $element is the div inside of the table cell, so use $element.closest('td') to get the cell
349 $element.closest('td').addClass('focused');
350 },
351 editable_blur : function(txt, columnIndex, $element) {
352 $element.closest('td').removeClass('focused');
353 }
354 }
355 });
356
357 });</pre>
358 Default value: <code>null</code>
359 </div>
360 </td>
361 </tr>
362
363 <tr id="editable-selectall">
364 <td><a href="#" class="permalink">editable_selectAll</a></td>
365 <td>Set to automatically select all content when the contenteditable cell is focused (<span class="version">v2.17.8</span>)
366 <div class="collapsible">
367 <br>
368 Set this option to a boolean, or a function that returns a boolean:
369 <ul>
370 <li>When <code>true</code>, all of the text within the current contenteditable element will be selected.</li>
371 <li>When <code>false</code>, no selection is made.</li>
372 <li>When this option contains a function, return either <code>true</code> to select all of the text within the element, or <code>false</code> to not select any text.</li>
373 </ul>
374 <pre class="prettyprint lang-js">$(function() {
375
376 $('#table').tablesorter({
377 widgets : ['editable'],
378 widgetOptions : {
379 editable_selectAll : function(txt, columnIndex, $element) {
380 // note $element is the div inside of the table cell, so use $element.closest('td') to get the cell
381 // only select everthing within the element when the content starts with the letter "B"
382 return /^b/i.test(txt) && columnIndex === 0;
383 }
384 }
385 });
386
387 });</pre>
388 Default value: <code>null</code>
389 </div>
390 </td>
391 </tr>
392
393 <tr id="editable-validate">
394 <td><a href="#" class="permalink">editable_validate</a></td>
395 <td>Validate the content change (<span class="version updated">v2.17.8</span>)
396 <div class="collapsible">
397 <br>
398 Use this function to validate and/or modify the content before it is accepted.<br>
399 <br>
400 In <span class="version updated">v2.17.8</span>:
401 <ul>
402 <li>Set this option to be a global function, or an object containing a column index, or class name of the desired column</li>
403 <li>A <code>columnIndex</code> is now included in the function parameters</li>
404 <li>A <code>$element</code> parameter has been included which contains the contenteditable element. To get the table cell, use <code>$element.closest('td');</code></li>
405 </ul>
406 This function must return either a string containing the modified content or <code>false</code> to revert the content back to it's original value. Example:
407 <pre class="prettyprint lang-js">$(function() {
408
409 $('#table1').tablesorter({
410 widgets : ['editable'],
411 widgetOptions : {
412 // global validate function
413 editable_validate : function(txt, orig, columnIndex, $element) {
414 // only allow one word
415 var t = /\s/.test(txt) ? txt.split(/\s/)[0] : txt;
416 return t || false;
417 }
418 }
419 });
420
421 $('#table2').tablesorter({
422 widgets : ['editable'],
423 widgetOptions : {
424 // validate function per column
425 editable_validate : {
426 0 : function(txt, orig, columnIndex, $element) {
427 // allow up to two words
428 var t = txt.split(' ');
429 return t.length > 2 ? t[0] + (t[1] ? ' ' + t[1] : '') : txt;
430 },
431 '.price' : function(txt, orig, columnIndex, $element) {
432 // make sure the price column(s) are using a number
433 return isNaN( txt.replace(/[$,\s]/g, '') ) ? false : txt;
434 }
435 }
436 }
437 });
438
439 });</pre>
440 Default value: <code>null</code>
441 </div>
442 </td>
443 </tr>
444
445 <tr id="editable-trimcontent">
446 <td><a href="#" class="permalink">editable_trimContent</a></td>
447 <td>Trim the content within the editable cell (<span class="version">v2.17.8</span>)
448 <div class="collapsible">
449 <br>
450 When a table cell is formatted as follows:
451 <pre class="prettyprint lang-html">&lt;td&gt;
452 John
453 &lt;/td&gt;</pre>Carriage returns and tab(s) at the beginning &amp; end of the contenteditable table cells will be removed initially and after editing.<br>
454 <br>
455 If this option is <code>true</code>, this widget will trim content <em>upon initialization</em>; this is necessary if you need cleaned it up content before editing, like with an autocomplete script.<br>
456 <br>
457 Use this option as follows:
458 <pre class="prettyprint lang-js">$(function() {
459
460 $('#table').tablesorter({
461 widgets : ['editable'],
462 widgetOptions : {
463 editable_trimContent : true
464 }
465 });
466
467 });</pre>
468 Default value: <code>true</code>
469 </div>
470 </td>
471 </tr>
472
473 <tr id="editable-wrapcontent">
474 <td><a href="#" class="permalink">editable_wrapContent</a></td>
475 <td>Wrap the editable cell content with this element (<span class="version">v2.17.8</span>)
476 <div class="collapsible">
477 <br>
478 Internally, the widget uses jQuery's <code>wrapInner</code> to wrap the cell contents, using the following rules:
479 <ul>
480 <li>If this option is set to <code>false</code> or an empty string (<code>''</code>), then no wrapping will occur. The "contenteditable" attribute will be applied to the table cell.</li>
481 <li>If the table cell has no children, then the content is wrapped using this option, and the wrapping element will have the "contentediable" attribute enabled.</li>
482 <li>If the cell already has children (one or more) immediately inside of the table cell, no wrapping occurs and the child element(s) that don't have the class name set in <code>editable_noEdit</code>, will have the "contenteditable" attribute enabled.</li>
483 </ul>
484 Note: that this option uses jQuery's <code>wrapInner</code>, so this option can be set to a html string or selector, jQuery object, DOM element, or a function (jQuery 1.4+)
485 <pre class="prettyprint lang-js">$(function() {
486
487 $('#table').tablesorter({
488 widgets : ['editable'],
489 widgetOptions : {
490 editable_wrap : '&lt;div&gt;'
491 }
492 });
493
494 });</pre>
495 Default value: <code>'&lt;div&gt;'</code>
496 </div>
497 </td>
498 </tr>
499
500 </tbody>
501 </table>
502 </div>
503
504 </div>
505
506 <p></p>
507 <h1>Demo</h1>
508
509 <button type="button" class="auto">Toggle</button> <code>editable_autoAccept&nbsp;&nbsp;&nbsp;&nbsp;: <span>true</span></code><br>
510 <button type="button" class="enter">Toggle</button> <code>editable_enterToAccept : <span>true</span></code>
511 <br>
512
513 <div id="demo"><table id="table" class="tablesorter">
514 <thead>
515 <tr>
516 <th>First Name</th>
517 <th>Last Name</th>
518 <th>Age</th>
519 <th>Total</th>
520 <th>Discount</th>
521 <th>Date</th>
522 </tr>
523 </thead>
524 <tbody>
525 <tr id="db-row-344">
526 <td class="no-edit">Peter</td>
527 <td>Parker</td>
528 <td>28</td>
529 <td>$9.99</td>
530 <td>20%</td>
531 <td>Jul 6, 2006 8:14 AM</td>
532 </tr>
533 <tr id="db-row-884">
534 <td>John</td>
535 <td>Hood</td>
536 <td>33</td>
537 <td>$19.99</td>
538 <td>25%</td>
539 <td>Dec 10, 2002 5:14 AM</td>
540 </tr>
541 <tr id="db-row-234">
542 <td>Clark</td>
543 <td>Kent</td>
544 <td>18</td>
545 <td>$15.89</td>
546 <td>44%</td>
547 <td>Jan 12, 2003 11:14 AM</td>
548 </tr>
549 <tr id="db-row-756">
550 <td>Bruce</td>
551 <td>Almighty</td>
552 <td>45</td>
553 <td>$153.19</td>
554 <td>44%</td>
555 <td>Jan 18, 2001 9:12 AM</td>
556 </tr>
557 <tr id="db-row-232">
558 <td>Bruce</td>
559 <td>Evans</td>
560 <td>22</td>
561 <td>$13.19</td>
562 <td>11%</td>
563 <td>Jan 18, 2007 9:12 AM</td>
564 </tr>
565 </tbody>
566 </table></div>
567
568 <h1>Javascript</h1>
569 <div id="javascript">
570 <pre class="prettyprint lang-javascript"></pre>
571 </div>
572 <h1>CSS</h1>
573 <div id="css">
574 <pre class="prettyprint lang-css"></pre>
575 </div>
576 <h1>HTML</h1>
577 <div id="html">
578 <pre class="prettyprint lang-html"></pre>
579 </div>
580
581 </div>
582
583 <script>
584 $(function() {
585 var wo = $('#table')[0].config.widgetOptions;
586 $('.enter').click(function() {
587 var t = wo.editable_enterToAccept = !wo.editable_enterToAccept;
588 $(this).next().find('span').html('' + t);
589 });
590 $('.auto').click(function() {
591 var t = wo.editable_autoAccept = !wo.editable_autoAccept;
592 $(this).next().find('span').html('' + t);
593 });
594 });
595 </script>
596
597 </body>
598 </html>