Notes
- This widget can not be applied to the original plugin and requires jQuery 1.7+ and a browser that supports
contenteditable
attributes (almost all modern browsers). - Important: In Internet Explorer, please wrap the cell content with a DIV or SPAN as it is not possible to make table cells directly contenteditable. Wrapping the content in the markup is much more efficient than using javascript to do it for you (especially in IE).
- In v2.21.6,
- The cell content now only automatically updates 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.
- Modified the
editable_trimContent
option to only trim content when set. - The widget now works with the contenteditable html (using jQuery's
.html()
); previously, the widget would only manipulate the text (using jQuery.text()
), so any HTML would get stripped out.
*Note* Because of this change, if the user presses Enter, a<div>
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 theeditable_blur
callback to modify the content.
- Updated v2.17.6,
- Fixed the
editable_enterToAccept
option to do what it was meant to do, accept when the user presses enter. - Added
editable_autoAccept
option, so that when it istrue
the original behavior of accepting content changes will be maintained. - Added
editable_validate
option, to allow validating the edited content. - 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 this Stackoverflow issue.
- The
editComplete
event now passes the tableconfig
variable to make it easier to access tablesorter variables.
- Fixed the
- Updated v2.13.2, 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.
- In some browsers, additional CSS is needed to highlight a focused editable table cell. See the CSS block below.
- Pressing escape while editing will cancel any changes.
- In the demo below, click in any of the first three columns to edit the content, except for the cell containing "Peter".
- To prevent a table cell from becoming editable, add the class name
"no-edit"
to the cell. Set by theeditable_noEdit
option.
When Content Changes are Accepted
editable_enterToAccept | |||
---|---|---|---|
true | false | ||
editable_autoAccept | true |
|
|
false |
|
|
editable_validation
function does not return false
.
Options
Editable widget default options (added inside of tablesorter widgetOptions
)
TIP! Click on the link in the function column to reveal full details (or toggle|show|hide all) or double click to update the browser location.
Option | Description |
---|---|
editable_autoAccept | Accepts any changes made to the table cell automatically v2.17.6
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 The only time the content is not accepted is if the user presses the escape key. if false , the acceptance behavior follows the editable_enterToAccept setting.Default value: true
|
editable_columns | Contains an array (or range string) of columns numbers you want to have editable content (zero-based index).
[] (empty array)
|
editable_enterToAccept | Makes the user press enter to accept the content within the editable table cell
if false , clicking outside the cell will accept the content.Default value: true
|
editable_autoResort | If true , the column will resort (only if already sorted) after the content has been changed.
Default value: false
|
editable_noEdit | Class name on table cells to search for that are not to become editable.
The search is only done within the columns set by the editable_columns option.Default value: 'no-edit'
|
editable_editComplete | Event fired after the table content has been edited
$(function(){ $('#table') .tablesorter({ widgets : ['editable'], widgetOptions : { editable_editComplete : 'giterdone' } }) // use delegated event binding .on('giterdone', 'td', function(event, config){ // this = td; the event bubble up console.log( 'new content = ' + $(this).text() ); }); });Default value: 'editComplete'
|
editable_focused | Callback function that is executeed when the contenteditable cell is focused (v2.17.8)
Set this function to manipulate or adjust the content when the content editable is focused $(function(){ $('#table').tablesorter({ widgets : ['editable'], widgetOptions : { editable_focused : function(txt, columnIndex, $element){ // note $element is the div inside of the table cell, so use $element.closest('td') to get the cell $element.closest('td').addClass('focused'); }, editable_blur : function(txt, columnIndex, $element){ $element.closest('td').removeClass('focused'); } } }); });Default value: null
|
editable_blur | Callback function that is executeed when the contenteditable cell is no longer focused (v2.17.8)
Set this function to manipulate or adjust the content when the content editable is blurred $(function(){ $('#table').tablesorter({ widgets : ['editable'], widgetOptions : { editable_focused : function(txt, columnIndex, $element){ // note $element is the div inside of the table cell, so use $element.closest('td') to get the cell $element.closest('td').addClass('focused'); }, editable_blur : function(txt, columnIndex, $element){ $element.closest('td').removeClass('focused'); } } }); });Default value: null
|
editable_selectAll | Set to automatically select all content when the contenteditable cell is focused (v2.17.8)
Set this option to a boolean, or a function that returns a boolean:
$(function(){ $('#table').tablesorter({ widgets : ['editable'], widgetOptions : { editable_selectAll : function(txt, columnIndex, $element){ // note $element is the div inside of the table cell, so use $element.closest('td') to get the cell // only select everthing within the element when the content starts with the letter "B" return /^b/i.test(txt) && columnIndex === 0; } } }); });Default value: null
|
editable_validate | Validate the content change (v2.17.8)
Use this function to validate and/or modify the content before it is accepted. In v2.17.8:
false to revert the content back to it's original value. Example:
$(function(){ $('#table1').tablesorter({ widgets : ['editable'], widgetOptions : { // global validate function editable_validate : function(txt, orig, columnIndex, $element){ // only allow one word var t = /\s/.test(txt) ? txt.split(/\s/)[0] : txt; return t ? t : false; } } }); $('#table2').tablesorter({ widgets : ['editable'], widgetOptions : { // validate function per column editable_validate : { 0 : function(txt, orig, columnIndex, $element){ // allow up to two words var t = txt.split(' '); return t.length > 2 ? t[0] + (t[1] ? ' ' + t[1] : '') : txt; }, '.price' : function(txt, orig, columnIndex, $element) { // make sure the price column(s) are using a number return isNaN( txt.replace(/[$,\s]/g, '') ) ? false : txt; } } } }); });Default value: null
|
editable_trimContent | Trim the content within the editable cell (v2.17.8)
When a table cell is formatted as follows: <td> John </td>Carriage returns and tab(s) at the beginning & end of the contenteditable table cells will be removed initially and after editing. If this option is true , this widget will trim content upon initialization; this is necessary if you need cleaned it up content before editing, like with an autocomplete script.Use this option as follows: $(function(){ $('#table').tablesorter({ widgets : ['editable'], widgetOptions : { editable_trimContent : true } }); });Default value: true
|
editable_wrapContent | Wrap the editable cell content with this element (v2.17.8)
Internally, the widget uses jQuery's wrapInner to wrap the cell contents, using the following rules:
wrapInner , so this option can be set to a html string or selector, jQuery object, DOM element, or a function (jQuery 1.4+)
$(function(){ $('#table').tablesorter({ widgets : ['editable'], widgetOptions : { editable_wrap : ' |
Demo
editable_autoAccept : true
editable_enterToAccept : true
First Name | Last Name | Age | Total | Discount | Date |
---|---|---|---|---|---|
Peter | Parker | 28 | $9.99 | 20% | Jul 6, 2006 8:14 AM |
John | Hood | 33 | $19.99 | 25% | Dec 10, 2002 5:14 AM |
Clark | Kent | 18 | $15.89 | 44% | Jan 12, 2003 11:14 AM |
Bruce | Almighty | 45 | $153.19 | 44% | Jan 18, 2001 9:12 AM |
Bruce | Evans | 22 | $13.19 | 11% | Jan 18, 2007 9:12 AM |