Mottie/tablesorter

Group widget not working when using ajax

lindonb opened this issue · 13 comments

When using ajax in version 2.14.1, attempting to use the group widget causes an error where c.cache[tbodyIndex].normalized is undefined at line 105 of widget-grouping.js.

Hi @lindonb!

This was a bit more complicated than I expected, so I didn't get a chance to fix it in this last update... I will soon!

Thanks @Mottie!

Hi @Mottie - I still get the exact same error with version 2.14.4. Did this clear it up in your tests?
Thanks,
lindonb

Hey @lindonb!

Doh, so sorry! I thought I included this fix.... try adding this immediately below the format function:

format: function(table, c, wo) {
    if ($.isEmptyObject(c.cache)) { return; }

I would appreciate if you could test this and make sure it works... I don't have any server side sorting set up to ensure it works.

Hi @Mottie,

That got rid of the error and all looks well with group headings when the table first loads with a pre-sorted column - thank you!.

But if I resort or paginate there are no group headers. Also the collapse/expand icon on the group header row has no impact except that the icon moves (i.e. rows are not hidden).
Thanks again,
@lindonb

Sorry for not working out all the bugs =(

Hi @lindonb!

I pushed the updates to the repo, but I haven't updated the tag. So please grab the "raw" code directly from here for testing: https://github.com/Mottie/tablesorter/blob/master/js/widgets/widget-grouping.js

Thanks for your patience!

Hi @Mottie!

You are an amazingly responsive developer so no need to thank me for my patience since I never have to wait!

Maybe I'm doing something wrong but the only difference in the version you gave the link to and v2.14.4 is the one line of code I tried before:
if ($.isEmptyObject(c.cache)) { return; }

Apologies if I missed the boat. Thanks again for your help on this.
Regards,
lindonb

Please check again (grouping.js)... I think it was a problem caused early today when an accidental commit was reverted - fixed now.

See 32f1c06 and 6e82ea1

this was totally my fault and I apologize, git is great but at times it can be a real PITA.

Looks like that did it! Appropriate group headers now appear after pagination and resorting and the collapse/expand group header icon is also working - great job!

One small issue - I do have a checkbox column with no sorting or filtering and it has a "select all" checkbox in the header. When I click any of the checkboxes, the group header rows disappear. (FYI in case it helps - clicking one of the checkboxes used to generate an ajax call and I reported this as issue #361 and it was fixed.)

Also, just wanted to mention that when showing the row count in the group header, it is only counting what's on the current page. I think that would be expected when using ajax as I assume the only way to provide a count across multiple pages would be for the ajax call to return the group header counts for all records, which seems like a waste. When using ajax, I think I would just not show the count.

Want to thank you again for working through this as I am anxious for users to see the group header in action - they will be majorly impressed.
Regards,
lindonb

Hmm, well the group widget now updates whenever the pagerChange event occurs, so if the check box isn't doing that, I would just include some code to update the group widget within the checkbox change event. Something like this:

$('tbody').on('change', '.checkbox', function(){
    // do something
    // update the grouping widget
    table = $(this).closest('table')[0];
    $.tablesorter.grouping.update(table, table.config, table.config.widgetOptions);
});

As for the row count, you can remove it by setting the group_count option to false or an empty string.


Edit: or better (easier) yet.. just trigger the apply widgets method:

$('tbody').on('change', '.checkbox', function(){
    // do something
    // update all widgets (including the grouping widget)
    $(this).trigger('applyWidgets'); // this will bubble up to the table
});

Hi @Mottie - that works! Thanks again,
lindonb