Column selector widget: add attachTo function for popup dynamic elements

This commit is contained in:
Mottie 2014-02-05 22:20:25 -06:00
parent 07334d1e30
commit 36617802e0
2 changed files with 300 additions and 72 deletions

View File

@ -10,6 +10,8 @@
<!-- Demo stuff -->
<link class="ui-theme" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/cupertino/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/jq.css">
<link href="css/prettify.css" rel="stylesheet">
<script src="js/prettify.js"></script>
@ -21,10 +23,18 @@
<script src="../js/jquery.tablesorter.widgets.js"></script>
<script src="../js/widgets/widget-columnSelector.js"></script>
<style>
/* override document styling */
.popover.right { text-align: left; }
.ui-widget-content a { color: #428bca; }
</style>
<style id="css">.columnSelectorWrapper {
<style id="css">/*** custom css only popup ***/
.columnSelectorWrapper {
position: relative;
margin: 10px 0;
display: inline-block;
}
.columnSelector, .hidden {
display: none;
@ -44,6 +54,7 @@
display: block;
}
.columnSelector {
width: 120px;
position: absolute;
top: 30px;
padding: 10px;
@ -58,13 +69,26 @@
border-bottom: #99bfe6 solid 1px;
margin-bottom: 5px;
}
.columnSelector input {
margin-right: 5px;
}
.columnSelector .disabled {
color: #ddd;
}</style>
}
/*** Bootstrap popover ***/
#popover-target label {
margin: 0 5px;
}
#popover-target input {
margin-right: 5px;
}
</style>
<script id="js">$(function() {
$("table").tablesorter({
/*** custom css only button popup ***/
$(".custom-popup").tablesorter({
theme: 'blue',
widgets: ['zebra', 'columnSelector', 'stickyHeaders'],
widgetOptions : {
@ -73,9 +97,9 @@
// column status, true = display, false = hide
// disable = do not display on list
columnSelector_columns : {
0: 'disable' /* set to disabled no allowed to unselect it */
0: 'disable' /* set to disabled; not allowed to unselect it */
},
// remember selected columns
// remember selected columns (requires $.tablesorter.storage)
columnSelector_saveColumns: true,
// container layout
@ -98,10 +122,29 @@
// duplicates how jQuery mobile uses priorities:
// http://view.jquerymobile.com/1.3.2/dist/demos/widgets/table-column-toggle/
columnSelector_priority : 'data-priority'
}
});
/*** Bootstrap popover demo ***/
$('#popover')
.popover({
placement: 'right',
html: true, // required if content has HTML
content: '<div id="popover-target"></div>'
})
// bootstrap popover event triggered when the popover opens
.on('shown.bs.popover', function () {
// call this function to copy the column selection code into the popover
$.tablesorter.columnSelector.attachTo( $('.bootstrap-popup'), '#popover-target');
});
// initialize column selector using default settings
// note: no container is defined!
$(".bootstrap-popup").tablesorter({
theme: 'blue',
widgets: ['zebra', 'columnSelector', 'stickyHeaders']
});
});
</script>
</head>
@ -128,7 +171,8 @@
<li>This widget may not work properly if the table header includes rows with row or column spans.</li>
<li>The responsive part of this widget
<ul>
<li>Uses similar parameters as those used by jQuery mobile to set <a href="http://view.jquerymobile.com/1.3.2/dist/demos/widgets/table-column-toggle/#Settingcolumnpriority">column priority</a>.</li>
<li>Uses similar parameters as those used by jQuery mobile to set <a href="http://view.jquerymobile.com/1.4.0/demos/table-column-toggle/">column priority</a>.</li>
<li>The column priorities range from 1 to 6, with 1 having the highest priority. As the browser window shrinks, lower priority (higher numbers) columns will be hidden first until all numbered priority columns are hidden.</li>
<li>Any named data-priority, other than the numbers 1 - 6, (e.g. "critical" or "persistent") will be treated as a column which <em>will not be included</em> in the column selector.</li>
<li>Note that this widget uses media queries, which <a href="http://caniuse.com/#search=media">will not work in IE8 and older browsers</a>.</li>
</ul>
@ -140,57 +184,134 @@
<h3><a href="#">Options</a></h3>
<div>
<h3>Column selector widget default options (added inside of tablesorter <code>widgetOptions</code>)</h3>
<ul>
<li><code>columnSelector_container</code> (<code>null</code>) - Target an element within the current page where the column selector will be inserted.
<ul>
<li>This can be either a jQuery selector string ( e.g. <code>'#columnSelector'</code> )</li>
<li>or, a jQuery object ( e.g. <code>$('#columnSelector')</code> ).</li>
</ul>
</li>
<li><code>columnSelector_columns</code> (<code>{}</code>; empty object) - Assigns a column status for each selector:
<ul>
<li>To disable, or remove a column from the column selector, include the key word <code>&quot;disable&quot;</code> - this is one of many ways to remove a column from the column selector popup</li>
<li>Set a column status to <code>true</code> to initially display a column. This is the default for undefined columns.</li>
<li>Set a column status to <code>false</code> to initially hide a column.</li>
<li>Examples:
<pre class="prettyprint lang-js">widgetOptions : {
<h4>Column selector widget default options (added inside of tablesorter <code>widgetOptions</code>)</h4>
<table class="tablesorter-blue">
<thead>
<tr><th>Option</th><th>Description</th></tr>
</thead>
<tbody>
<tr>
<td><code>columnSelector_container</code></td>
<td>
Target an element within the current page where the column selector will be inserted.
<ul>
<li>This setting is optional. If not set, you can use the following function to attach it to another element - see the Bootstrap example below
<pre class="prettyprint lang-js">$.tablesorter.columnSelector.attachTo( $('table'), '.selector-target' );</pre>
</li>
<li>This can be either a jQuery selector string ( e.g. <code>'#columnSelector'</code> )</li>
<li>or, a jQuery object ( e.g. <code>$('#columnSelector')</code> ).</li>
</ul>
Default value: <code>null</code>
</td>
</tr>
<tr>
<td><code>columnSelector_columns</code></td>
<td>
Assigns a column status for each selector:
<ul>
<li>To disable, or remove a column from the column selector, include the key word <code>&quot;disable&quot;</code> - this is one of many ways to remove a column from the column selector popup</li>
<li>Set a column status to <code>true</code> to initially display a column. This is the default for undefined columns.</li>
<li>Set a column status to <code>false</code> to initially hide a column.</li>
<li>Examples:
<pre class="prettyprint lang-js">widgetOptions : {
columnSelector_columns : {
0 : &quot;disable&quot;, /* disable; i.e. remove column from selector */
1 : false, /* start with column hidden */
2 : true, /* start with column visible; default for undefined columns */
}
}</pre></li>
<li><code>columnSelector_saveColumns</code> (<code>true</code>) - Save the current manually set column status (not the column's responsive state). This option requires the storage utility contained within the tablesorter widgets file (<code>jquery.tablesorter.widgets.js</code>).</li>
<li><code>columnSelector_layout</code> (<code>'&lt;label&gt;&lt;input type=&quot;checkbox&quot;&gt;{name}&lt;/label&gt;'</code>) - This option defines the markup used for each column selector within the popup. The only required parameter is the <code>{name}</code> string which will be replaced with the appropriate column name/title.</li>
</ul>
Default value: <code>{}</code>; empty object
</td>
</tr>
<li><code>columnSelector_name</code> (<code>&quot;data-selector-name&quot;</code>) - The data-attribute within the table header cell which contains an alternate column selector name.
<ul>
<li>If the header cell does not have this attribute defined, the column selector name will be taken from the header cell internal text.</li>
<li>If defined, the text contained within this attribute will replace the <code>{name}</code> string within the layout option above.</li>
</ul>
</li>
<tr>
<td><code>columnSelector_saveColumns</code></td>
<td>Save the current manually set column status (not the column's responsive state). This option requires the storage utility contained within the tablesorter widgets file (<code>jquery.tablesorter.widgets.js</code>).<br>
<br>
Default value: <code>true</code>
</td>
</tr>
<tr>
<td><code>columnSelector_layout</code></td>
<td>
This option defines the markup used for each column selector within the popup. The only available parameter is the <code>{name}</code> string which will be replaced with the appropriate column name/title.<br>
<br>
An <code>&lt;input type=&quot;checkbox&quot;&gt;</code> is required within this setting!<br>
<br>
Default value: <code>'&lt;label&gt;&lt;input type=&quot;checkbox&quot;&gt;{name}&lt;/label&gt;'</code>
</td>
</tr>
<tr>
<td><code>columnSelector_name</code></td>
<td>
The data-attribute within the table header cell which contains an alternate column selector name.
<ul>
<li>If the header cell does not have this attribute defined, the column selector name will be taken from the header cell internal text.</li>
<li>If defined, the text contained within this attribute will replace the <code>{name}</code> string within the layout option above.</li>
</ul>
Default value: <code>&quot;data-selector-name&quot;</code>
</td>
</tr>
<tr>
<td><code>columnSelector_mediaquery</code></td>
<td>Set this option to add (<code>true</code>) or not add (<code>false</code>) the media query functionality of this widget.<br>
<br>
Default value: <code>true</code>
</td>
</tr>
<tr>
<td><code>columnSelector_mediaqueryName</code></td>
<td>When the media query checkbox is added (it also uses the <code>columnSelector_layout</code> markup), this is the name that is added. Set as "Auto" to signify to the user that columns disappearing and/or reappearing is automatically done.<br>
<br>
Default value: <code>&quot;Auto: &quot;</code>
</td>
</tr>
<tr>
<td><code>columnSelector_mediaqueryState</code></td>
<td>Set this option to <code>false</code> to start with the media query disabled (manual column selection mode).<br>
<br>
Default value: <code>true</code>
</td>
</tr>
<tr>
<td><code>columnSelector_breakpoints</code></td>
<td>
<ul>
<li>This option defines the media query breakpoints with which to use when a column with the associated priority is hidden or revealed.</li>
<li>For example, the last entry &quot;70em&quot; (1,120px) is assigned to data-priority 6. When the browser width is below this dimension, all columns with a data-priority of six will be hidden. Then when a browser width less than &quot;60em&quot; (960px) is reached, all columns of data-priority 5 and above will be hidden. At &quot;50em&quot; (800px), all columns of data-priority 4 and above are hidden, etc.</li>
<li>Adjust these values as desired, but a <em>maximum</em> of six data-priorities is set.</li>
</ul>
Default value: <code>[ &quot;20em&quot;, &quot;30em&quot;, &quot;40em&quot;, &quot;50em&quot;, &quot;60em&quot;, &quot;70em&quot; ]</code>
</td>
</tr>
<tr>
<td><code>columnSelector_priority</code></td>
<td>
This is the assigned data-attribute which contains the defined data priority for a table column.
<ul>
<li>Values of 1 through 6 set the breakpoints of that particular column.</li>
<li>A value of 1 has the highest priority, meaning it is the last column(s) to be hidden when the browser width goes below &quot;20em&quot; (320px).</li>
<li>A value of 7 has the lowest priority, meaning it is the first column(s) to be hidden when the browser width goes below &quot;70em&quot; (1,120px).</li>
<li>Any named priority value, (e.g. "critical" or "persistent") will flag the widget to remove that column from the selector list.</li>
<li>Undefined priorities will default to a priority value of 1.</li>
</ul>
Default value: <code>&quot;data-priority&quot;</code>
</td>
</tr>
</tbody>
</table>
<li><code>columnSelector_mediaquery</code> (<code>true</code>) - Set this option to add (<code>true</code>) or not add (<code>false</code>) the media query functionality of this widget.</li>
<li><code>columnSelector_mediaqueryName</code> (<code>&quot;Auto: &quot;</code>) - When the media query checkbox is added (it also uses the <code>columnSelector_layout</code> markup), this is the name that is added. Set as "Auto" to signify to the user that columns disappearing and/or reappearing is automatically done.</li>
<li><code>columnSelector_mediaqueryState</code> (<code>true</code>) - Set this option to <code>false</code> to start with the media query disabled (manual column selection mode).</li>
<li><code>columnSelector_breakpoints</code> (<code>[ &quot;20em&quot;, &quot;30em&quot;, &quot;40em&quot;, &quot;50em&quot;, &quot;60em&quot;, &quot;70em&quot; ]</code>)
<ul>
<li>This option defines the media query breakpoints with which to use when a column with the associated priority is hidden or revealed.</li>
<li>For example, the last entry &quot;70em&quot; (1,120px) is assigned to data-priority 6. When the browser width is below this dimension, all columns with a data-priority of six will be hidden. Then when a browser width less than &quot;60em&quot; (960px) is reached, all columns of data-priority 5 and above will be hidden. At &quot;50em&quot; (800px), all columns of data-priority 4 and above are hidden, etc.</li>
<li>Adjust these values as desired, but a <em>maximum</em> of six data-priorities is set.</li>
</ul>
</li>
<li><code>columnSelector_priority</code> (<code>&quot;data-priority&quot;</code>) - This is the assigned data-attribute which contains the defined data priority for a table column.
<ul>
<li>Values of 1 through 6 set the breakpoints of that particular column.</li>
<li>A value of 1 has the highest priority, meaning it is the last column(s) to be hidden when the browser width goes below &quot;20em&quot; (320px).</li>
<li>A value of 7 has the lowest priority, meaning it is the first column(s) to be hidden when the browser width goes below &quot;70em&quot; (1,120px).</li>
<li>Any named priority value, (e.g. "critical" or "persistent") will flag the widget to remove that column from the selector list.</li>
<li>Undefined priorities will default to a priority value of 1.</li>
</ul>
</li>
</ul>
</div>
<h3><a href="#">Removing a column from the selector</a></h3>
@ -215,6 +336,8 @@
<h1>Demo</h1>
<h3>CSS only popup</h3>
<!-- This selector markup is completely customizable -->
<div class="columnSelectorWrapper">
<input id="colSelect1" type="checkbox" class="hidden">
@ -222,14 +345,14 @@
<div id="columnSelector" class="columnSelector">
<!-- this div is where the column selector is added -->
</div>
</div>
</div> (When "Auto" is set, the table becomes responsive; resize the browser window to see it work)
<table class="tablesorter">
<table class="tablesorter custom-popup">
<thead>
<tr>
<th data-priority="critical">Name</th>
<!-- Remove column from selection popup by including -->
<!-- data-priority="Anything other than 1-6" OR data-column-selector="false" OR class="columnSelector-false" -->
<!-- data-priority="Anything other than 1-6" OR data-column-selector="false" OR class="columnSelector-false" -->
<th class="columnSelector-false">Major</th>
<th data-priority="6" data-selector-name="Gender">Sex</th>
<th data-priority="4">English</th>
@ -242,6 +365,9 @@
<tr><th>Name</th><th>Major</th><th>Sex</th><th>English</th><th>Japanese</th><th>Calculus</th><th>Geometry</th></tr>
</tfoot>
<tbody>
<tr><td>Student03</td><td>Languages</td><td>female</td><td>85</td><td>95</td><td>80</td><td>85</td></tr>
<tr><td>Student04</td><td>Languages</td><td>male</td><td>60</td><td>55</td><td>100</td><td>100</td></tr>
<tr><td>Student05</td><td>Languages</td><td>female</td><td>68</td><td>80</td><td>95</td><td>80</td></tr>
<tr><td>Student12</td><td>Mathematics</td><td>female</td><td>100</td><td>75</td><td>70</td><td>85</td></tr>
<tr><td>Student13</td><td>Languages</td><td>female</td><td>100</td><td>80</td><td>100</td><td>90</td></tr>
<tr><td>Student14</td><td>Languages</td><td>female</td><td>50</td><td>45</td><td>55</td><td>90</td></tr>
@ -251,21 +377,49 @@
<tr><td>Student18</td><td>Mathematics</td><td>male</td><td>30</td><td>49</td><td>55</td><td>75</td></tr>
<tr><td>Student19</td><td>Languages</td><td>male</td><td>68</td><td>90</td><td>88</td><td>70</td></tr>
<tr><td>Student20</td><td>Mathematics</td><td>male</td><td>40</td><td>45</td><td>40</td><td>80</td></tr>
<tr><td>Student21</td><td>Languages</td><td>male</td><td>50</td><td>45</td><td>100</td><td>100</td></tr>
<tr><td>Student22</td><td>Mathematics</td><td>male</td><td>100</td><td>99</td><td>100</td><td>90</td></tr>
<tr><td>Student23</td><td>Languages</td><td>female</td><td>85</td><td>80</td><td>80</td><td>80</td></tr>
<tr><td>Student01</td><td>Languages</td><td>male</td><td>80</td><td>70</td><td>75</td><td>80</td></tr>
<tr><td>Student02</td><td>Mathematics</td><td>male</td><td>90</td><td>88</td><td>100</td><td>90</td></tr>
<tr><td>Student03</td><td>Languages</td><td>female</td><td>85</td><td>95</td><td>80</td><td>85</td></tr>
<tr><td>Student04</td><td>Languages</td><td>male</td><td>60</td><td>55</td><td>100</td><td>100</td></tr>
<tr><td>Student05</td><td>Languages</td><td>female</td><td>68</td><td>80</td><td>95</td><td>80</td></tr>
<tr><td>Student06</td><td>Mathematics</td><td>male</td><td>100</td><td>99</td><td>100</td><td>90</td></tr>
<tr><td>Student07</td><td>Mathematics</td><td>male</td><td>85</td><td>68</td><td>90</td><td>90</td></tr>
<tr><td>Student08</td><td>Languages</td><td>male</td><td>100</td><td>90</td><td>90</td><td>85</td></tr>
<tr><td>Student09</td><td>Mathematics</td><td>male</td><td>80</td><td>50</td><td>65</td><td>75</td></tr>
<tr><td>Student10</td><td>Languages</td><td>male</td><td>85</td><td>100</td><td>100</td><td>90</td></tr>
<tr><td>Student11</td><td>Languages</td><td>male</td><td>86</td><td>85</td><td>100</td><td>100</td></tr>
<tr><td>Student24</td><td>Languages</td><td>female</td><td>100</td><td>91</td><td>13</td><td>82</td></tr>
</tbody>
</table>
<h3>Bootstrap Popover</h3>
<!-- Bootstrap popover button -->
<button id="popover" type="button" class="btn btn-default">
Select Column
</button>
<table class="tablesorter bootstrap-popup">
<thead>
<tr>
<th data-priority="critical">Rank</th>
<th data-priority="3">First Name</th>
<th data-priority="critical">Last Name</th>
<th data-priority="4">Age</th>
<th data-priority="4">Total</th>
<th data-priority="5">Discount</th>
<th data-priority="6">Date</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td>Philip Aaron Wong</td><td>Johnson Sr Esq</td><td>25</td><td>$5.95</td><td>22%</td><td>Jun 26, 2004 7:22 AM</td></tr>
<tr><td>11</td><td>Aaron</td><td>Hibert</td><td>12</td><td>$2.99</td><td>5%</td><td>Aug 21, 2009 12:21 PM</td></tr>
<tr><td>12</td><td>Brandon Clark</td><td>Henry Jr</td><td>51</td><td>$42.29</td><td>18%</td><td>Oct 13, 2000 1:15 PM</td></tr>
<tr><td>111</td><td>Peter</td><td>Parker</td><td>28</td><td>$9.99</td><td>20%</td><td>Jul 6, 2006 8:14 AM</td></tr>
<tr><td>21</td><td>John</td><td>Hood</td><td>33</td><td>$19.99</td><td>25%</td><td>Dec 10, 2002 5:14 AM</td></tr>
<tr><td>013</td><td>Clark</td><td>Kent Sr.</td><td>18</td><td>$15.89</td><td>44%</td><td>Jan 12, 2003 11:14 AM</td></tr>
<tr><td>005</td><td>Bruce</td><td>Almighty Esq</td><td>45</td><td>$153.19</td><td>44%</td><td>Jan 18, 2021 9:12 AM</td></tr>
<tr><td>10</td><td>Alex</td><td>Dumass</td><td>13</td><td>$5.29</td><td>4%</td><td>Jan 8, 2012 5:11 PM</td></tr>
<tr><td>16</td><td>Jim</td><td>Franco</td><td>24</td><td>$14.19</td><td>14%</td><td>Jan 14, 2004 11:23 AM</td></tr>
<tr><td>166</td><td>Bruce Lee</td><td>Evans</td><td>22</td><td>$13.19</td><td>11%</td><td>Jan 18, 2007 9:12 AM</td></tr>
<tr><td>100</td><td>Brenda Dexter</td><td>McMasters</td><td>18</td><td>$55.20</td><td>15%</td><td>Feb 12, 2010 7:23 PM</td></tr>
<tr><td>55</td><td>Dennis</td><td>Bronson</td><td>65</td><td>$123.00</td><td>32%</td><td>Jan 20, 2001 1:12 PM</td></tr>
<tr><td>9</td><td>Martha</td><td>delFuego</td><td>25</td><td>$22.09</td><td>17%</td><td>Jun 11, 2011 10:55 AM</td></tr>
</tbody>
</table>
@ -276,7 +430,8 @@
<h1>HTML</h1>
<div>
<pre class="prettyprint lang-html">&lt;!-- This selector markup is completely customizable --&gt;
<pre class="prettyprint lang-html">&lt;h3&gt;CSS only popup&lt;/h3&gt;
&lt;!-- This selector markup is completely customizable --&gt;
&lt;div class=&quot;columnSelectorWrapper&quot;&gt;
&lt;input id=&quot;colSelect1&quot; type=&quot;checkbox&quot; class=&quot;hidden&quot;&gt;
&lt;label class=&quot;columnSelectorButton&quot; for=&quot;colSelect1&quot;&gt;Column&lt;/label&gt;
@ -285,7 +440,7 @@
&lt;/div&gt;
&lt;/div&gt;
&lt;table class=&quot;tablesorter&quot;&gt;
&lt;table class=&quot;tablesorter custom-popup&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th data-priority=&quot;critical&quot;&gt;Name&lt;/th&gt;
@ -305,6 +460,29 @@
&lt;tbody&gt;
&lt;!-- ... --&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3&gt;Bootstrap Popover&lt;/h3&gt;
&lt;!-- Bootstrap popover button --&gt;
&lt;button id=&quot;popover&quot; type=&quot;button&quot; class=&quot;btn btn-default&quot;&gt;
Select Column
&lt;/button&gt;
&lt;table class=&quot;tablesorter bootstrap-popup&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th data-priority=&quot;critical&quot;&gt;Rank&lt;/th&gt;
&lt;th data-priority=&quot;3&quot;&gt;First Name&lt;/th&gt;
&lt;th data-priority=&quot;critical&quot;&gt;Last Name&lt;/th&gt;
&lt;th data-priority=&quot;4&quot;&gt;Age&lt;/th&gt;
&lt;th data-priority=&quot;4&quot;&gt;Total&lt;/th&gt;
&lt;th data-priority=&quot;5&quot;&gt;Discount&lt;/th&gt;
&lt;th data-priority=&quot;6&quot;&gt;Date&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;!-- ... --&gt;
&lt;/tbody&gt;
&lt;/table&gt;</pre>
</div>

View File

@ -15,14 +15,23 @@ tsColSel = ts.columnSelector = {
queryBreak : '@media screen and (min-width: [size]) { [columns] { display: table-cell; } }',
init: function(table, c, wo) {
var colSel;
var $t, colSel;
// abort if no input is contained within the layout
$t = $(wo.columnSelector_layout);
if (!$t.find('input').add( $t.filter('input') ).length) {
if (c.debug) {
ts.log('*** ERROR: Column Selector aborting, no input found in the layout! ***');
}
return;
}
// unique table class name
c.tableId = 'tablesorter' + new Date().getTime();
c.$table.addClass( c.tableId );
// build column selector/state array
colSel = c.selector = { $container : $(wo.columnSelector_container) };
colSel = c.selector = { $container : $(wo.columnSelector_container || '<div>') };
tsColSel.setupSelector(table, c, wo);
if (wo.columnSelector_mediaquery) {
@ -73,7 +82,8 @@ tsColSel = ts.columnSelector = {
if ($container.length) {
colSel.$wrapper[colId] = $(wo.columnSelector_layout.replace(/\{name\}/g, name)).appendTo($container);
colSel.$checkbox[colId] = colSel.$wrapper[colId]
.find('input')
// input may not be wrapped within the layout template
.find('input').add( colSel.$wrapper[colId].filter('input') )
.attr('data-column', colId)
.prop('checked', colSel.states[colId])
.bind('change', function(){
@ -86,7 +96,7 @@ tsColSel = ts.columnSelector = {
},
setupBreakpoints: function(c, wo){
var colSel = c.selector;
var $auto, colSel = c.selector;
// add responsive breakpoints
if (wo.columnSelector_mediaquery) {
@ -104,9 +114,11 @@ tsColSel = ts.columnSelector = {
if (colSel.$container.length) {
// Add media queries toggle
if (wo.columnSelector_mediaquery && wo.columnSelector_mediaquery) {
$( wo.columnSelector_layout.replace(/\{name\}/g, wo.columnSelector_mediaqueryName) )
.prependTo(colSel.$container)
.find('input')
$auto = $( wo.columnSelector_layout.replace(/\{name\}/g, wo.columnSelector_mediaqueryName) ).prependTo(colSel.$container);
$auto
// needed in case the input in the layout is not wrapped
.find('input').add( $auto.filter('input') )
.attr('data-column', 'auto')
.prop('checked', wo.columnSelector_mediaqueryState)
.bind('change', function(){
wo.columnSelector_mediaqueryState = this.checked;
@ -118,6 +130,15 @@ tsColSel = ts.columnSelector = {
});
tsColSel.updateBreakpoints(c, wo);
tsColSel.updateCols(c, wo);
// copy the column selector to a popup/tooltip
if (c.selector.$popup) {
c.selector.$popup.find('.tablesorter-column-selector')
.html( colSel.$container.html() )
.find('input').each(function(){
var indx = $(this).attr('data-column')
$(this).prop( 'checked', indx === 'auto' ? wo.columnSelector_mediaqueryState : colSel.states[indx] )
});
}
}).change();
}
// Add a bind on update to re-run col setup
@ -127,8 +148,7 @@ tsColSel = ts.columnSelector = {
}
},
updateBreakpoints: function(c, wo){
updateBreakpoints: function(c, wo) {
var priority, column, breaks,
colSel = c.selector,
prefix = '.' + c.tableId,
@ -169,7 +189,7 @@ tsColSel = ts.columnSelector = {
var column,
styles = [],
prefix = '.' + c.tableId;
c.selector.$container.find('input[data-column]').each(function(){
c.selector.$container.find('input[data-column]').filter('[data-column!="auto"]').each(function(){
if (!this.checked) {
column = parseInt( $(this).attr('data-column'), 10 ) + 1;
styles.push(prefix + ' tr th:nth-child(' + column + ')');
@ -185,6 +205,35 @@ tsColSel = ts.columnSelector = {
if (wo.columnSelector_saveColumns && ts.storage) {
ts.storage( c.$table[0], 'tablesorter-columnSelector', c.selector.states );
}
},
attachTo : function(table, elm) {
var colSel, wo, indx,
table = $(table)[0],
c = table.config,
$popup = $(elm);
if ($popup.length && c) {
if (!$popup.find('.tablesorter-column-selector').length) {
// add a wrapper to add the selector into, in case the popup has other content
$popup.append('<span class="tablesorter-column-selector"></span>');
}
colSel = c.selector;
wo = c.widgetOptions;
$popup.find('.tablesorter-column-selector')
.html( colSel.$container.html() )
.find('input').each(function(){
var indx = $(this).attr('data-column');
$(this).prop( 'checked', indx === 'auto' ? wo.columnSelector_mediaqueryState : colSel.states[indx] )
});
colSel.$popup = $popup.on('change', 'input', function(){
// data input
indx = $(this).attr('data-column');
// update original popup
colSel.$container.find('input[data-column="' + indx + '"]')
.prop('checked', this.checked)
.trigger('change');
});
}
}
};
@ -229,6 +278,7 @@ ts.addWidget({
remove: function(table, c){
var csel = c.selector;
csel.$container.empty();
csel.$popup.empty();
csel.$style.remove();
csel.$breakpoints.remove();
c.$table.unbind('updateAll' + namespace + ',update' + namespace);