From 8ddd3c76f2143ef986af5f1e341ce49b0bdf17cb Mon Sep 17 00:00:00 2001 From: Mottie Date: Tue, 26 Aug 2014 14:23:13 -0500 Subject: [PATCH] Editable: update validate function to allow setting it per column --- docs/example-widget-editable.html | 31 ++++++++++++++++++++++++++++--- js/widgets/widget-editable.js | 16 +++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/docs/example-widget-editable.html b/docs/example-widget-editable.html index c04150e0..63e7568a 100644 --- a/docs/example-widget-editable.html +++ b/docs/example-widget-editable.html @@ -243,18 +243,24 @@ td.no-edit, span.no-edit { - Validate the content change. + 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: + This function must return either a string containing the modified content or false to revert the content back to it's original value. Example:
$(function(){
 
-  $('table').tablesorter({
+  $('#table1').tablesorter({
     widgets : ['editable'],
     widgetOptions : {
-      editable_validate : function(txt, orig){
+      // global validate function
+      editable_validate : function(txt, orig, columnIndex){
         // only allow one word
         var t = /\s/.test(txt) ? txt.split(/\s/)[0] : txt;
         return t ? t : false;
@@ -262,6 +268,25 @@ td.no-edit, span.no-edit {
     }
   });
 
+  $('#table2').tablesorter({
+    widgets : ['editable'],
+    widgetOptions : {
+      // validate function per column
+      editable_validate : {
+        0 : function(txt, orig, columnIndex){
+          // 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) {
+          // make sure the price column(s) are using a number
+          return isNaN( txt.replace(/[$,\s]/g, '') ) ? false : txt;
+        }
+      }
+    }
+  });
+
+
 });
Default value: null
diff --git a/js/widgets/widget-editable.js b/js/widgets/widget-editable.js index 48e77e62..387b26df 100644 --- a/js/widgets/widget-editable.js +++ b/js/widgets/widget-editable.js @@ -79,9 +79,10 @@ }) .on('blur focusout keydown '.split(' ').join('.tseditable '), '[contenteditable]', function(e){ if ( !c.$table.data('contentFocused') ) { return; } - var t, + var t, validate, valid = false, - $this = $(e.target); + $this = $(e.target), + column = $this.closest('td').index(); if ( e.which === 27 ) { // user cancelled $this.html( $this.data('original') ).trigger('blur.tseditable'); @@ -91,7 +92,16 @@ t = e.which === 13 && ( wo.editable_enterToAccept || e.altKey ) || wo.editable_autoAccept && e.type !== 'keydown'; // change if new or user hits enter (if option set) if ( t && $this.data('before') !== $this.html() ) { - valid = $.isFunction(wo.editable_validate) ? wo.editable_validate( $this.html(), $this.data('original') ) : $this.html(); + + validate = wo.editable_validate; + valid = $this.html(); + + if (typeof(validate) === "function") { + valid = validate( $this.html(), $this.data('original'), column ); + } else if (typeof (validate = $.tablesorter.getColumnData( table, validate, column )) === 'function') { + valid = validate( $this.html(), $this.data('original'), column ); + } + if ( t && valid !== false ) { c.$table.find('.tseditable-last-edited-cell').removeClass('tseditable-last-edited-cell'); $this