Filter: Update select2 filterFormatter to properly escape regex characters

This update will not worl with select2 v4.0.0!
This commit is contained in:
Mottie 2015-01-16 20:25:27 -06:00
parent 14799b9ce8
commit 66b5682cf0
2 changed files with 13 additions and 7 deletions

View File

@ -481,7 +481,7 @@
<li><a href="example-widget-filter-custom.html">custom</a> (v2.3.6; <span class="version updated">v2.10.1</span>)</li>
<li><a href="example-widget-filter-custom-search.html">custom searches</a> (<span class="version">v2.17.5</span>; <span class="version updated">v2.17.8</span>)</li>
<li>formatter: <a href="example-widget-filter-formatter-1.html">jQuery UI widgets</a> and <a href="example-widget-filter-formatter-2.html">HTML5 Elements</a> (v2.7.7; <span class="version updated">v2.17.5</span>).</li>
<li>formatter: <a href="example-widget-filter-formatter-select2.html">select2</a> (<span class="version">v2.16.0</span>; <span class="version updated">v2.17.5</span>)</li>
<li>formatter: <a href="example-widget-filter-formatter-select2.html">select2</a> (<span class="version">v2.16.0</span>; <span class="version updated">v2.18.5</span>)</li>
</ul>
</li>
<li>Grouping rows widget:

View File

@ -1,4 +1,4 @@
/*! Filter widget select2 formatter function - updated 7/17/2014 (v2.17.5)
/*! Filter widget select2 formatter function - updated 1/16/2015 (v2.18.5)
* requires: jQuery 1.7.2+, tableSorter (FORK) 2.16+, filter widget 2.16+ and select2 v3.4.6+ plugin
*/
/*jshint browser:true, jquery:true, unused:false */
@ -45,6 +45,12 @@ ts.filterFormatter.select2 = function($cell, indx, select2Def) {
// this function updates the hidden input and adds the current values to the header cell text
updateSelect2 = function() {
var v = $cell.find('.select2').select2('val') || o.value || '';
// convert array to string
if ($.isArray(v)) { v = v.join('\u0000'); }
// escape special regex characters (http://stackoverflow.com/a/9310752/145346)
v = v.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
// convert string back into an array
if (v.indexOf('\u0000')) { v = v.split('\u0000'); }
$input
// add regex, so we filter exact numbers
.val( $.isArray(v) && v.length && v.join('') !== '' ? '/(' + matchPrefix + (v || []).join(matchSuffix + '|' + matchPrefix) + matchSuffix + ')/' : '' )
@ -99,7 +105,7 @@ ts.filterFormatter.select2 = function($cell, indx, select2Def) {
c.$table.bind('filterFomatterUpdate', function(){
// value = '/(^x$|^y$)/' => 'x,y'
var val = c.$table.data('lastSearch')[indx] || '';
val = val.replace(/[/()$^]/g, '').split('|');
val = val.replace(/^\/\(\^?/,'').replace(/\$\|\^/g, '|').replace(/\$?\)\/$/g,'').split('|');
$cell.find('.select2').select2('val', val);
updateSelect2();
ts.filter.formatterUpdated($cell, indx);