Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition between "updateRows" and "sorton" #782

Closed
brendanfalkowski opened this issue Dec 24, 2014 · 2 comments
Closed

Race condition between "updateRows" and "sorton" #782

brendanfalkowski opened this issue Dec 24, 2014 · 2 comments

Comments

@brendanfalkowski
Copy link

Debugged a race condition: after updating the table data, then sorting would not apply.

Original code:

// Replace markup
$jQ('.tablesorter tbody').html(markup.join(''));

// Update tablesorter
$jQ('.tablesorter').trigger('updateRows', [false]);

// Sort
$jQ('.tablesorter').trigger('sorton', [
    [[0, 'd']]
]);

// Result:
// Table is updated but not sorted.

Modified to beat race condition:

// Replace markup
$jQ('.tablesorter tbody').html(markup.join(''));

// Update tablesorter
$jQ('.tablesorter').trigger('updateRows', [false]);

// Delay for 2000ms
window.setTimeout(function () {
    // Sort
    $jQ('.tablesorter').trigger('sorton', [
        [[0, 'd']]
    ]);
}, 2000);

// Result:
// Table is updated...pause...then sorted correctly.

Is there a better way than window.setTimeout?

Note: I'm using 2.17.8 (tried updating to 2.18.4, but pager widget stopped working — one thing at a time).

@Mottie
Copy link
Owner

Mottie commented Dec 24, 2014

Hi @brendanfalkowski!

Odd, it's working for me - demo

The only issue I noticed is that the zebra widget isn't updating as expected :(

So I had to add a callback:

$('tbody')
    .append(rows)
    .trigger('updateRows', [false])
    .trigger('sorton', [ [[0,'d']], function(){
        // for some reason the zebra widget isn't updating :(
       $('table').trigger('applyWidgetId', ['zebra']);
    } ]);

@Mottie
Copy link
Owner

Mottie commented Dec 24, 2014

Ok, so I just made a change to the core in the working branch. So you can now apply a new sort from within any of the update methods (addRows, updateRows, updateAll, updateCell)

// as of v2.18.5 (currently in the working branch only)
// resort = false (no resort)
// resort = true (reapply current sort)
// resort = array (e.g. [[0,0]]; apply new sort)
// resort = [] (empty array = sort reset)
var resort = [[0,'d']];

$('tbody')
    .append(rows)
    .trigger('updateRows', [ resort ]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants