question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TableBlock with custom TabbedInterface

See original GitHub issue

When new TableBlock is used with default TabbedInterface - all works fine:

from wagtail.contrib.table_block.blocks import TableBlock
from wagtail.wagtailadmin.edit_handlers import StreamFieldPanel
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore.models import Page


class TestPage(Page):

    content = StreamField([
        ('table', TableBlock()),
    ])

    search_fields = Page.search_fields
    content_panels = Page.content_panels + [
        StreamFieldPanel('content')
    ]

I see editor as expected:

2016-06-07 11-03-33 wagtail - test page test page - google chrome

But when I try to create custom TabbedInterface:

from wagtail.contrib.table_block.blocks import TableBlock
from wagtail.wagtailadmin.edit_handlers import StreamFieldPanel, TabbedInterface, ObjectList
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore.models import Page


class TestPage(Page):

    content = StreamField([
        ('table', TableBlock()),
    ])

    search_fields = Page.search_fields
    editor_content_panels = [
        StreamFieldPanel('content')
    ]

    edit_handler = TabbedInterface([
        ObjectList(Page.content_panels, heading="Default Editor"),
        ObjectList(Page.promote_panels, heading="Promote"),
        ObjectList(Page.settings_panels, heading="Settings", classname='settings'),
        ObjectList(editor_content_panels, heading="Editor"),
    ])

After creating new page with TableBlock and re-opening it for edition - table editor don’t appears:

2016-06-07 11-06-11 wagtail - test page test page - google chrome

But… After opening “Developer Tools” (F12) magic happens:

2016-06-07 11-08-52 wagtail - test page test page - google chrome

I’ve checked this bug with latest Chrome and Firefox. Looks like some JavaScript window refresh is needed on Tab click event.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
soarcommented, Jul 15, 2016

Oh, 2 hours spent and I finally found the solution:

@hooks.register('insert_global_admin_js')
def fix_editors_in_tabs():
    script = """
            <script>
                $(document).ready(function() {
                    $('.tab-nav a').on('shown.bs.tab', function (e) {
                        // Fix for TableBlock editor
                        $(window).trigger('resize');

                        // Fix for SimpleMDE editor
                        if (window.SimpleMDEInstances != null) {
                            $.each(window.SimpleMDEInstances, function(index, inst) {
                                inst.codemirror.refresh();
                            });
                        }
                    });
                });
            </script>
        """
    return script

It works for TableBlock and my MarkdownBlock.

3reactions
danthedeckiecommented, Jan 22, 2020

@BertrandBordage 's fix helped, but didn’t work for me when the second tab is the active one when the page loads (on Chrome - Firefox was fine). I got this to work, but it’s very ugly:

@hooks.register('insert_global_admin_js')
def fix_tables_in_tabs():
    return """
        <script>
            (function() {
                function fix_tables(){
                     $('.handsontable').each(function () {
                        var inputId = $(this).siblings('input').attr('id');
                        if (typeof inputId !== 'undefined') {
                            initTable(inputId, %s);
                        }
                    });
                };
                $(document).ready(function() {
                    setTimeout(fix_tables, 1000);
                    $('.tab-nav a').on('shown.bs.tab', fix_tables);
                });
            })();
        </script>
    """ % json.dumps(TableBlock().table_options)

There has to be a better way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customising the editing interface - Wagtail's documentation
Customising the editing interface¶. Customising the tabbed interface¶. As standard, Wagtail organises panels for pages into three tabs: 'Content', ...
Read more >
Custom Admin Tabs - Learn Wagtail
Learn how to create Wagtail websites with over 50 free tutorial videos and a professional beginners course.
Read more >
Site settings — Wagtail 2.8.1 documentation
You can also customize the editor handlers like you would do for Page model with a custom ... from wagtail.admin.edit_handlers import TabbedInterface, ...
Read more >
Using forms in admin views - Wagtail's documentation
A single panel object (usually ObjectList or TabbedInterface ) exists at the top level and is the only one directly accessed by the...
Read more >
Customising CreateView, EditView and DeleteView — Wagtail ...
... edit_handler = TabbedInterface([ ObjectList(custom_panels, heading='First Tab'), ... Expected value: The path to a custom template to use for CreateView.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found