2015-10-31 16:06:09 +00:00
|
|
|
/*! Parser: input & select - updated 10/31/2015 (v2.24.0) *//*
|
2015-03-26 15:48:43 +00:00
|
|
|
* for jQuery 1.7+ & tablesorter 2.7.11+
|
2013-03-26 21:16:13 +00:00
|
|
|
* Demo: http://mottie.github.com/tablesorter/docs/example-widget-grouping.html
|
|
|
|
*/
|
|
|
|
/*jshint browser: true, jquery:true, unused:false */
|
2015-05-04 04:00:10 +00:00
|
|
|
;( function( $ ) {
|
2015-07-23 04:29:51 +00:00
|
|
|
'use strict';
|
2013-03-26 21:16:13 +00:00
|
|
|
|
2015-05-04 04:00:10 +00:00
|
|
|
var updateServer = function( event, $table, $input ) {
|
2015-01-29 05:27:20 +00:00
|
|
|
// do something here to update your server, if needed
|
|
|
|
// event = change event object
|
|
|
|
// $table = jQuery object of the table that was just updated
|
|
|
|
// $input = jQuery object of the input or select that was modified
|
|
|
|
};
|
2013-03-26 21:16:13 +00:00
|
|
|
|
|
|
|
// Custom parser for parsing input values
|
2015-05-04 04:00:10 +00:00
|
|
|
// updated dynamically using the 'change' function below
|
2013-03-26 21:16:13 +00:00
|
|
|
$.tablesorter.addParser({
|
2015-05-04 04:00:10 +00:00
|
|
|
id : 'inputs',
|
|
|
|
is : function() {
|
2013-03-26 21:16:13 +00:00
|
|
|
return false;
|
|
|
|
},
|
2015-05-04 04:00:10 +00:00
|
|
|
format : function( txt, table, cell ) {
|
2015-05-13 17:12:55 +00:00
|
|
|
var $input = $( cell ).find( 'input' );
|
|
|
|
return $input.length ? $input.val() : txt;
|
2013-03-26 21:16:13 +00:00
|
|
|
},
|
2014-02-19 10:28:45 +00:00
|
|
|
parsed : true, // filter widget flag
|
2015-05-04 04:00:10 +00:00
|
|
|
type : 'text'
|
2013-03-26 21:16:13 +00:00
|
|
|
});
|
|
|
|
|
2015-04-28 22:07:36 +00:00
|
|
|
$.tablesorter.addParser({
|
2015-05-04 04:00:10 +00:00
|
|
|
id : 'inputs-numeric',
|
|
|
|
is : function() {
|
2015-04-28 22:07:36 +00:00
|
|
|
return false;
|
|
|
|
},
|
2015-05-04 04:00:10 +00:00
|
|
|
format : function( txt, table, cell ) {
|
2015-05-13 17:12:55 +00:00
|
|
|
var $input = $( cell ).find( 'input' );
|
|
|
|
var val = $input.length ? $input.val() : txt,
|
2015-05-04 04:00:10 +00:00
|
|
|
num = $.tablesorter.formatFloat( ( val || '' ).replace( /[^\w,. \-()]/g, '' ), table );
|
|
|
|
return txt && typeof num === 'number' ? num :
|
|
|
|
txt ? $.trim( txt && table.config.ignoreCase ? txt.toLocaleLowerCase() : txt ) : txt;
|
2015-04-28 22:07:36 +00:00
|
|
|
},
|
|
|
|
parsed : true, // filter widget flag
|
2015-05-04 04:00:10 +00:00
|
|
|
type : 'numeric'
|
2015-04-28 22:07:36 +00:00
|
|
|
});
|
|
|
|
|
2013-03-26 21:16:13 +00:00
|
|
|
// Custom parser for including checkbox status if using the grouping widget
|
2015-05-04 04:00:10 +00:00
|
|
|
// updated dynamically using the 'change' function below
|
2013-03-26 21:16:13 +00:00
|
|
|
$.tablesorter.addParser({
|
2015-05-04 04:00:10 +00:00
|
|
|
id : 'checkbox',
|
|
|
|
is : function() {
|
2013-03-26 21:16:13 +00:00
|
|
|
return false;
|
|
|
|
},
|
2015-05-04 04:00:10 +00:00
|
|
|
format : function( txt, table, cell, cellIndex ) {
|
|
|
|
var $cell = $( cell ),
|
2015-07-26 17:01:21 +00:00
|
|
|
$row = $cell.closest( 'tr' ),
|
2015-03-05 13:03:08 +00:00
|
|
|
wo = table.config.widgetOptions,
|
2015-06-05 18:16:36 +00:00
|
|
|
checkedClass = table.config.checkboxClass || 'checked',
|
2015-03-05 13:03:08 +00:00
|
|
|
// returning plain language here because this is what is shown in the
|
|
|
|
// group headers - change it as desired
|
2015-05-04 04:00:10 +00:00
|
|
|
status = wo.group_checkbox ? wo.group_checkbox : [ 'checked', 'unchecked' ],
|
|
|
|
$input = $cell.find( 'input[type="checkbox"]' ),
|
|
|
|
isChecked = $input.length ? $input[ 0 ].checked : '';
|
2014-02-19 09:11:52 +00:00
|
|
|
// adding class to row, indicating that a checkbox is checked; includes
|
|
|
|
// a column index in case more than one checkbox happens to be in a row
|
2015-07-26 17:01:21 +00:00
|
|
|
$row.toggleClass( checkedClass + '-' + cellIndex, isChecked );
|
|
|
|
if ( isChecked ) {
|
|
|
|
$row.addClass( checkedClass );
|
2015-08-14 22:07:56 +00:00
|
|
|
} else if ( $row.length && !( $row[0].className || '' ).match( checkedClass + '-' ) ) {
|
2015-07-26 17:01:21 +00:00
|
|
|
// don't remove checked class if other columns have a check
|
|
|
|
$row.removeClass( checkedClass );
|
|
|
|
}
|
2015-05-04 04:00:10 +00:00
|
|
|
return $input.length ? status[ isChecked ? 0 : 1 ] : txt;
|
2013-03-26 21:16:13 +00:00
|
|
|
},
|
2014-02-19 10:28:45 +00:00
|
|
|
parsed : true, // filter widget flag
|
2015-05-04 04:00:10 +00:00
|
|
|
type : 'text'
|
2013-03-26 21:16:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Custom parser which returns the currently selected options
|
2015-05-04 04:00:10 +00:00
|
|
|
// updated dynamically using the 'change' function below
|
2013-03-26 21:16:13 +00:00
|
|
|
$.tablesorter.addParser({
|
2015-05-04 04:00:10 +00:00
|
|
|
id : 'select',
|
|
|
|
is : function() {
|
2013-03-26 21:16:13 +00:00
|
|
|
return false;
|
|
|
|
},
|
2015-05-04 04:00:10 +00:00
|
|
|
format : function( txt, table, cell ) {
|
2015-05-13 17:12:55 +00:00
|
|
|
var $select = $( cell ).find( 'select' );
|
|
|
|
return $select.length ? $select.val() : txt;
|
2013-03-26 21:16:13 +00:00
|
|
|
},
|
2014-02-19 10:28:45 +00:00
|
|
|
parsed : true, // filter widget flag
|
2015-05-04 04:00:10 +00:00
|
|
|
type : 'text'
|
2013-03-26 21:16:13 +00:00
|
|
|
});
|
|
|
|
|
2014-07-15 22:14:27 +00:00
|
|
|
// Select parser to get the selected text
|
|
|
|
$.tablesorter.addParser({
|
2015-05-04 04:00:10 +00:00
|
|
|
id : 'select-text',
|
|
|
|
is : function() {
|
2014-07-15 22:14:27 +00:00
|
|
|
return false;
|
|
|
|
},
|
2015-05-04 04:00:10 +00:00
|
|
|
format : function( txt, table, cell ) {
|
|
|
|
var $select = $( cell ).find( 'select' );
|
|
|
|
return $select.length ? $select.find( 'option:selected' ).text() || '' : txt;
|
2014-07-15 22:14:27 +00:00
|
|
|
},
|
|
|
|
parsed : true, // filter widget flag
|
2015-05-04 04:00:10 +00:00
|
|
|
type : 'text'
|
2014-07-15 22:14:27 +00:00
|
|
|
});
|
|
|
|
|
2014-07-23 17:05:46 +00:00
|
|
|
// Custom parser for parsing textarea values
|
2015-05-04 04:00:10 +00:00
|
|
|
// updated dynamically using the 'change' function below
|
2014-07-23 17:05:46 +00:00
|
|
|
$.tablesorter.addParser({
|
2015-05-04 04:00:10 +00:00
|
|
|
id : 'textarea',
|
|
|
|
is : function() {
|
2014-07-23 17:05:46 +00:00
|
|
|
return false;
|
|
|
|
},
|
2015-05-04 04:00:10 +00:00
|
|
|
format : function( txt, table, cell ) {
|
2015-05-13 17:12:55 +00:00
|
|
|
var $textarea = $( cell ).find( 'textarea' );
|
|
|
|
return $textarea.length ? $textarea.val() : txt;
|
2014-07-23 17:05:46 +00:00
|
|
|
},
|
|
|
|
parsed : true, // filter widget flag
|
2015-05-04 04:00:10 +00:00
|
|
|
type : 'text'
|
2014-07-23 17:05:46 +00:00
|
|
|
});
|
|
|
|
|
2013-03-26 21:16:13 +00:00
|
|
|
// update select and all input types in the tablesorter cache when the change event fires.
|
|
|
|
// This method only works with jQuery 1.7+
|
|
|
|
// you can change it to use delegate (v1.4.3+) or live (v1.3+) as desired
|
|
|
|
// if this code interferes somehow, target the specific table $('#mytable'), instead of $('table')
|
2015-05-04 04:00:10 +00:00
|
|
|
$( function() {
|
|
|
|
$( 'table' ).on( 'tablesorter-initialized updateComplete', function() {
|
|
|
|
var namespace = '.parser-forms',
|
|
|
|
restoreValue = function( isTbody ) {
|
2015-01-29 05:27:20 +00:00
|
|
|
// make sure we restore original values (trigger blur)
|
|
|
|
// isTbody is needed to prevent the select from closing in IE
|
|
|
|
// see https://connect.microsoft.com/IE/feedbackdetail/view/962618/
|
2015-05-04 04:00:10 +00:00
|
|
|
if ( isTbody ) {
|
|
|
|
$( ':focus' ).blur();
|
2015-01-29 05:27:20 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
};
|
2014-07-09 02:27:22 +00:00
|
|
|
// bind to .tablesorter (default class name)
|
2015-05-04 04:00:10 +00:00
|
|
|
$( this ).children( 'tbody' )
|
|
|
|
.off( namespace )
|
|
|
|
.on( 'mouseleave' + namespace, function( event ) {
|
|
|
|
restoreValue( event.target.nodeName === 'TBODY' );
|
2014-08-01 17:00:10 +00:00
|
|
|
})
|
2015-05-04 04:00:10 +00:00
|
|
|
.on( 'focus' + namespace, 'select, input, textarea', function() {
|
|
|
|
$( this ).data( 'ts-original-value', this.value );
|
2014-08-01 17:00:10 +00:00
|
|
|
})
|
2015-05-04 04:00:10 +00:00
|
|
|
.on( 'blur' + namespace, 'input, textarea', function() {
|
2014-08-01 17:00:10 +00:00
|
|
|
// restore input value;
|
2015-05-04 04:00:10 +00:00
|
|
|
// 'change' is triggered before 'blur' so this doesn't replace the new update with the original
|
|
|
|
this.value = $( this ).data( 'ts-original-value' );
|
2014-08-01 17:00:10 +00:00
|
|
|
})
|
2015-05-04 04:00:10 +00:00
|
|
|
.on( 'change keyup '.split( ' ' ).join( namespace + ' ' ), 'select, input, textarea', function( event ) {
|
|
|
|
if ( event.which === 27 ) {
|
2014-08-01 17:00:10 +00:00
|
|
|
// escape: restore original value
|
2015-05-04 04:00:10 +00:00
|
|
|
this.value = $( this ).data( 'ts-original-value' );
|
2014-08-02 02:31:33 +00:00
|
|
|
return;
|
2014-08-01 17:00:10 +00:00
|
|
|
}
|
|
|
|
// Update cell cache using... select: change, input: enter or textarea: alt + enter
|
2015-05-04 04:00:10 +00:00
|
|
|
if ( event.type === 'change' ||
|
|
|
|
( event.type === 'keyup' && event.which === 13 &&
|
|
|
|
( event.target.nodeName === 'INPUT' || event.target.nodeName === 'TEXTAREA' && event.altKey ) ) ) {
|
2015-01-29 05:27:20 +00:00
|
|
|
var undef,
|
2015-05-04 04:00:10 +00:00
|
|
|
$target = $( event.target ),
|
|
|
|
$cell = $target.closest( 'td' ),
|
|
|
|
$table = $cell.closest( 'table' ),
|
|
|
|
indx = $cell[ 0 ].cellIndex,
|
|
|
|
c = $table[ 0 ].config || false,
|
|
|
|
$hdr = c && c.$headerIndexed && c.$headerIndexed[ indx ] || [],
|
|
|
|
val = $target.val();
|
|
|
|
// abort if not a tablesorter table, or don't use updateCell if column is set
|
|
|
|
// to 'sorter-false' and 'filter-false', or column is set to 'parser-false'
|
|
|
|
if ( $hdr.length && ( $hdr.hasClass( 'parser-false' ) ||
|
|
|
|
( $hdr.hasClass( 'sorter-false' ) && $hdr.hasClass( 'filter-false' ) ) ) ) {
|
|
|
|
return;
|
2014-08-01 17:00:10 +00:00
|
|
|
}
|
|
|
|
// ignore change event if nothing changed
|
2015-10-22 21:41:34 +00:00
|
|
|
if ( c && val !== $target.data( 'ts-original-value' ) || event.target.type === 'checkbox' ) {
|
2015-05-04 04:00:10 +00:00
|
|
|
$target.data( 'ts-original-value', val );
|
2015-01-29 05:27:20 +00:00
|
|
|
// pass undefined resort value so it falls back to config.resort setting
|
2015-10-22 21:41:34 +00:00
|
|
|
$.tablesorter.updateCell( c, $cell, undef, function() {
|
2015-05-04 04:00:10 +00:00
|
|
|
updateServer( event, $table, $target );
|
2015-10-22 21:41:34 +00:00
|
|
|
});
|
2014-07-09 02:27:22 +00:00
|
|
|
}
|
2014-04-27 01:30:13 +00:00
|
|
|
}
|
2014-07-09 02:27:22 +00:00
|
|
|
});
|
2013-03-26 21:16:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-04 04:00:10 +00:00
|
|
|
})( jQuery );
|