Core: Refer textExtraction by header class/id

This commit is contained in:
Mottie 2014-05-14 07:51:44 -05:00
parent 4627f214b8
commit 9657255429
5 changed files with 33 additions and 18 deletions

View File

@ -38,7 +38,7 @@
2: function(node, table, cellIndex){ return $(node).find("span").text(); },
3: function(node, table, cellIndex){ return $(node).find("em").text(); },
4: function(node, table, cellIndex){ return $(node).find("a").text(); },
5: function(node, table, cellIndex){ return $(node).find("u").text(); }
'.date' : function(node, table, cellIndex){ return $(node).find("u").text(); }
}
});
@ -58,6 +58,7 @@
<p class="tip">
<em>NOTE!</em>
<ul>
<li>In <span class="version updated">v2.16.5</span>, the <code>textExtraction</code> column can also be referenced by using a jQuery selector (e.g. class name or ID).</li>
<li>The textExtraction function setting for each column was added in version 2.0.12 (not part of the original plugin).</li>
<li>The "First Name" column is sorting by the contents of the &lt;strong&gt; tag (in red).</li>
<li>Added "table" and "cellIndex" variables to the textExtraction function (v2.1.2).</li>
@ -73,7 +74,7 @@
<th>Age</th>
<th>Total</th>
<th>Discount</th>
<th>Date</th>
<th class="date">Date</th>
</tr>
</thead>
<tbody>

View File

@ -1348,9 +1348,11 @@ From the example function above, you'll end up with something similar to this HT
<td><a href="#" class="permalink">textExtraction</a></td>
<td>String Or Function</td>
<td>&quot;basic&quot;</td>
<td>Defines which method is used to extract data from a table cell for sorting (<span class="version updated">v2.16.0</span>)
<td>Defines which method is used to extract data from a table cell for sorting (<span class="version updated">v2.16.5</span>)
<div class="collapsible">
<br>
In <span class="version updated">v2.16.5</span>, the <code>textExtraction</code> column can also be referenced by using a jQuery selector (e.g. class name or ID).<br>
<br>
As of version 2.16.0,
<ul>
<li>The default text extraction method has been renamed and updated to get data from a data-attribute (set by the <a href="#textattribute"><code>textAttribute</code></a> option).</li>

View File

@ -167,7 +167,7 @@
function getElementText(table, node, cellIndex) {
if (!node) { return ""; }
var c = table.config,
var te, c = table.config,
t = c.textExtraction || '',
text = "";
if (t === "basic") {
@ -176,8 +176,8 @@
} else {
if (typeof(t) === "function") {
text = t(node, table, cellIndex);
} else if (typeof(t) === "object" && t.hasOwnProperty(cellIndex)) {
text = t[cellIndex](node, table, cellIndex);
} else if (typeof (te = ts.getColumnData( table, t, cellIndex )) === 'function') {
text = te(node, table, cellIndex);
} else {
// previous "simple" method
text = node.textContent || node.innerText || $(node).text() || "";

View File

@ -100,19 +100,19 @@
<table id="table4" class="tester">
<thead>
<tr><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th></tr>
<tr><th>1</th><th class="te-test">2</th><th>3</th><th>4</th><th>5</th><th>6</th></tr>
</thead>
<tbody>
<tr><td>F01</td><td>F11</td><td>A21</td><td>01</td><td>ZZS</td><td>WA</td></tr>
<tr><td>F01</td><td>F11</td><td>A21</td><td>02</td><td>ZY</td><td>WB</td></tr>
<tr><td>F02</td><td>F12</td><td>A22</td><td>03</td><td>ZZ</td><td>WA</td></tr>
<tr><td>F02</td><td>F12</td><td>A22</td><td>04</td><td>ZZ</td><td>WB</td></tr>
<tr><td>F02</td><td>F12</td><td>A23</td><td>05</td><td>ZY</td><td>WA</td></tr>
<tr><td>F02</td><td>F12</td><td>A23</td><td>06</td><td>ZX</td><td>WB</td></tr>
<tr><td>F02</td><td>F12</td><td>A23</td><td>07</td><td>ZX</td><td>WA</td></tr>
<tr><td>F02</td><td>F13</td><td>A23</td><td>08</td><td>ZZ</td><td>WB</td></tr>
<tr><td>F03</td><td>F13</td><td>A24</td><td>09</td><td>ZZ</td><td>WA</td></tr>
<tr><td>F03</td><td>F13</td><td>A24</td><td>10</td><td>ZZ</td><td>WB</td></tr>
<tr><td>F01</td><td>x<span>F11</span></td><td>x<span>A21</span></td><td>01</td><td>ZZS</td><td>WA</td></tr>
<tr><td>F01</td><td>g<span>F11</span></td><td>g<span>A21</span></td><td>02</td><td>ZY</td><td>WB</td></tr>
<tr><td>F02</td><td>z<span>F12</span></td><td>z<span>A22</span></td><td>03</td><td>ZZ</td><td>WA</td></tr>
<tr><td>F02</td><td>b<span>F12</span></td><td>b<span>A22</span></td><td>04</td><td>ZZ</td><td>WB</td></tr>
<tr><td>F02</td><td>d<span>F12</span></td><td>d<span>A23</span></td><td>05</td><td>ZY</td><td>WA</td></tr>
<tr><td>F02</td><td>f<span>F12</span></td><td>f<span>A23</span></td><td>06</td><td>ZX</td><td>WB</td></tr>
<tr><td>F02</td><td>h<span>F12</span></td><td>h<span>A23</span></td><td>07</td><td>ZX</td><td>WA</td></tr>
<tr><td>F02</td><td>a<span>F13</span></td><td>a<span>A23</span></td><td>08</td><td>ZZ</td><td>WB</td></tr>
<tr><td>F03</td><td>c<span>F13</span></td><td>c<span>A24</span></td><td>09</td><td>ZZ</td><td>WA</td></tr>
<tr><td>F03</td><td>e<span>F13</span></td><td>e<span>A24</span></td><td>10</td><td>ZZ</td><td>WB</td></tr>
</tbody>
</table>

View File

@ -174,6 +174,14 @@ $(function(){
$table4.tablesorter({
sortAppend : [[2,0],[3,0]],
sortForce : [[0,0],[1,0]],
textExtraction : {
'.te-test' : function(node){
return $(node).find('span').text();
},
2 : function(node){
return $(node).find('span').text();
}
},
initialized: function(table){
var e, c = table.config;
// trigger sort on 5th column
@ -462,7 +470,7 @@ $(function(){
});
test( "textExtraction Method", function() {
expect(2);
expect(4);
$table1.trigger('sorton', [[[ 0,0 ]]]);
tester.cacheCompare( table1, 0, [ 'test1', 'test2', 'test3', '', 'testa', 'testb', 'testc' ], 'from data-attribute' );
@ -470,6 +478,10 @@ $(function(){
$table3.trigger('sorton', [[[ 0,1 ]]]);
tester.cacheCompare( table3, 0, [ '', 'a255', 'a102', 'a87', 'a55', 'a43', 'a33', 'a10', 'a02', 'a1' ], 'ignore data-attribute' );
tester.cacheCompare( table4, 1, [ 'f11', 'f11', 'f12', 'f12', 'f12', 'f12', 'f12', 'f13', 'f13', 'f13' ], 'extract using class name' );
tester.cacheCompare( table4, 2, [ 'a21', 'a21', 'a23', 'a23', 'a23', 'a22', 'a22', 'a23', 'a24', 'a24' ], 'extract using column index' );
});
/************************************************