Add showError function. Fixes #486

This commit is contained in:
Mottie 2014-02-05 06:10:55 -06:00
parent b1aa1b3879
commit cb40882acc
15 changed files with 97 additions and 22 deletions

View File

@ -247,26 +247,21 @@
result = p.ajaxProcessing(data, table) || [ 0, [] ],
hl = $t.find('thead th').length;
$t.find('thead tr.' + p.cssErrorRow).remove(); // Clean up any previous error.
// Clean up any previous error.
ts.showError(table);
if ( exception ) {
if (c.debug) {
ts.log('Ajax Error', xhr, exception);
}
$err = $('<tr class="' + p.cssErrorRow + '" role="alert" aria-live="assertive">' +
'<td style="text-align:center;" colspan="' + hl + '">' + (
ts.showError(table,
xhr.status === 0 ? 'Not connected, verify Network' :
xhr.status === 404 ? 'Requested page not found [404]' :
xhr.status === 500 ? 'Internal Server Error [500]' :
exception === 'parsererror' ? 'Requested JSON parse failed' :
exception === 'timeout' ? 'Time out error' :
exception === 'abort' ? 'Ajax Request aborted' :
'Uncaught error: ' + xhr.statusText + ' [' + xhr.status + ']' ) + '</td></tr>')
.click(function(){
$(this).remove();
})
// add error row to thead instead of tbody, or clicking on the header will result in a parser error
.appendTo( $t.find('thead:first') );
'Uncaught error: ' + xhr.statusText + ' [' + xhr.status + ']' );
c.$tbodies.eq(0).empty();
} else {
// process ajax object
@ -757,8 +752,34 @@
});
};
}()
});
}() });
// see #486
ts.showError = function(table, message){
$(table).each(function(){
var $row,
c = this.config,
errorRow = c.pager && c.pager.cssErrorRow || c.widgetOptions.pager_css && c.widgetOptions.pager_css.errorRow || 'tablesorter-errorRow';
if (c) {
if (typeof message === 'undefined') {
c.$table.find('thead').find(c.selectorRemove).remove();
} else {
$row = ( /tr\>/.test(message) ? $(message) : $('<tr><td colspan="' + c.columns + '">' + message + '</td></tr>') )
.click(function(){
$(this).remove();
})
// add error row to thead instead of tbody, or clicking on the header will result in a parser error
.appendTo( c.$table.find('thead:first') )
.addClass( errorRow + ' ' + c.selectorRemove.replace(/^[.#]/, '') )
.attr({
role : 'alert',
'aria-live' : 'assertive'
});
}
}
});
};
// extend plugin scope
$.fn.extend({
tablesorterPager: $.tablesorterPager.construct

View File

@ -176,6 +176,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -211,6 +211,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -133,6 +133,7 @@
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -134,6 +134,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -177,6 +177,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -179,6 +179,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -202,6 +202,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -193,6 +193,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -235,6 +235,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -191,6 +191,7 @@ caption {
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -144,6 +144,7 @@
/* ajax error row */
.tablesorter .tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: #e6bf99;
}

View File

@ -320,6 +320,7 @@
/* ajax error row */
.tablesorter-errorRow td {
text-align: center;
cursor: pointer;
background-color: @errorBackground;
}

View File

@ -2085,8 +2085,6 @@ $(function(){
<td><a href="example-widget-filter-custom.html">Example</a></td>
</tr>
: true, // hide filter row when table is empty
<tr id="widget-filter-hideempty">
<td><a href="#" class="permalink">filter_hideEmpty</a></td>
<td>Boolean</td>
@ -4765,6 +4763,30 @@ $.tablesorter.isDigit( &quot;(2,345.67)&quot; );</pre>
</td>
</tr>
<tr id="function-showError">
<td><a href="#" class="permalink">showError</a></td>
<td>This function allows adding/removing a row to the thead, to display any errors <span class="version">v2.15</span>
<div class="collapsible"><br>
This function is ONLY included within the <code>widget-pager.js</code> and <code>jquery.tablesorter.pager.js</code> files; in version 3+, I plan to add it as a selectable option in a build.<br>
<br>
Use it as follows:
<pre class="prettyprint lang-js">$.tablesorter.showError( table, message );</pre>
<ul>
<li><code>table</code> - table DOM element (or jQuery object) of table (or tables).</li>
<li><code>message</code> - a plain string, or string of an HTML row.</li>
</ul>
This function will add a table row to the thead, with a class name from either the pager plugin <code>cssErrorRow</code> option setting, or the pager widget <code>pager_css.errorRow</code> option (the default class name is `&quot;tablesorter-errorRow&quot;`; and styled within each theme css file).<br>
<br>
When passing this function a message, there are three possibilities:
<ol>
<li>Plain string - <code>&quot;table refuses to cooperate&quot;</code></li>
<li>HTML row string - <code>'&lt;tr&gt;&lt;td colspan=&quot;' + table.config.columns + '&quot;&gt;yeah, instead of showing your data... I am taking a nap&lt;/td&gt;&lt;/tr&gt;'</code> (the <a href="#variable-columns"><code>table.config.columns</code> variable</a> contains the number of table columns; use as needed)</li>
<li>undefined - completely leave out a message parameter <code>$.tablesorter.showError( table );</code> to remove all error rows from the table thead.</li>
</ul>
</div>
</td>
</tr>
</tbody>
<!-- non-sorting tbody -->

View File

@ -440,20 +440,14 @@ tsp = ts.pager = {
result = wo.pager_ajaxProcessing(data, table) || [ 0, [] ],
hl = $t.find('thead th').length;
$t.find('thead tr.' + wo.pager_css.errorRow).remove(); // Clean up any previous error.
// Clean up any previous error.
ts.showError(table);
if ( exception ) {
if (c.debug) {
ts.log('Ajax Error', xhr, exception);
}
$err = $('<tr class="' + wo.pager_css.errorRow + '" role="alert" aria-live="assertive">' +
'<td style="text-align:center;" colspan="' +
hl + '">' + exception.message + ' (' + xhr.status + ')</td></tr>')
.click(function(){
$(this).remove();
})
// add error row to thead instead of tbody, or clicking on the header will result in a parser error
.appendTo( $t.find('thead:first') );
ts.showError(table, exception.message + ' (' + xhr.status + ')');
c.$tbodies.eq(0).empty();
} else {
// process ajax object
@ -812,4 +806,30 @@ tsp = ts.pager = {
};
// see #486
ts.showError = function(table, message){
$(table).each(function(){
var $row,
c = this.config,
errorRow = c.pager && c.pager.cssErrorRow || c.widgetOptions.pager_css && c.widgetOptions.pager_css.errorRow || 'tablesorter-errorRow';
if (c) {
if (typeof message === 'undefined') {
c.$table.find('thead').find(c.selectorRemove).remove();
} else {
$row = ( /tr\>/.test(message) ? $(message) : $('<tr><td colspan="' + c.columns + '">' + message + '</td></tr>') )
.click(function(){
$(this).remove();
})
// add error row to thead instead of tbody, or clicking on the header will result in a parser error
.appendTo( c.$table.find('thead:first') )
.addClass( errorRow + ' ' + c.selectorRemove.replace(/^[.#]/, '') )
.attr({
role : 'alert',
'aria-live' : 'assertive'
});
}
}
});
};
})(jQuery);