Core: optimize textExtraction function

Reduced comparisons & moved "simple" textExtraction before checks for textExtraction functions & using getColumnData, to further speed up processing time
This commit is contained in:
Mottie 2015-01-20 12:31:19 -06:00
parent b3a7b82a67
commit 3213d16867
2 changed files with 19 additions and 16 deletions

View File

@ -1555,11 +1555,15 @@ $.extend($.tablesorter.themes.jui, {
<tr id="textextraction">
<td><a href="#" class="permalink">textExtraction</a></td>
<td>String Or Function</td>
<td>Multiple*</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.17.0</span>)
<td>Defines which method is used to extract data from a table cell for sorting (<span class="version updated">v2.18.5</span>)
<div class="collapsible">
<br>
* <span class="label label-info">Note</span> This option accepts multiple types (String, Object or Function); see below for further details.
<p></p>
In <span class="version updated">v2.18.5</span>, the code was further optimized. When set to <code>"basic"</code> (the default), the textExtraction code will check for data-attributes, otherwise, any other string value setting will skip the data-attribute value check; because of this change, there is a noticable lessening of initialization time in Internet Explorer.
<p></p>
In <span class="version updated">v2.17.0</span>, the <code>textExtraction</code> column can also be referenced by using a jQuery selector (e.g. class name, id or column index) that points to a table <em>header</em> cell.<br>
<pre class="prettyprint lang-js">textExtraction : {
// 'jQuery thead cell selector' : function ( new method )

View File

@ -180,24 +180,23 @@
}
function getElementText(table, node, cellIndex) {
if (!node) { return ""; }
var te, c = table.config,
t = c.textExtraction || '',
text = "";
if (t === "basic") {
// check data-attribute first
text = $(node).attr(c.textAttribute) || node.textContent || node.innerText || $(node).text() || "";
if (!node) { return ''; }
var te,
$node = $(node),
c = table.config,
t = c.textExtraction || '';
if (typeof(t) === 'string') {
// check data-attribute first when set to "basic"; don't use node.innerText - it's really slow!
return $.trim( (t === 'basic' ? $node.attr(c.textAttribute) || node.textContent : node.textContent ) || $node.text() || '' );
} else {
if (typeof(t) === "function") {
text = t(node, table, cellIndex);
if (typeof(t) === 'function') {
return $.trim( t(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() || "";
return $.trim( te(node, table, cellIndex) );
}
}
return $.trim(text);
// fallback
return $.trim( node.textContent || $node.text() || '' );
}
function detectParserForColumn(table, rows, rowIndex, cellIndex) {