Skip to content

Sorting does not work for StackedInline if widget of the field has <table> #384

Open
@igor-zmitrovich

Description

@igor-zmitrovich

https://github.com/jrief/django-admin-sortable2/blob/2.1.10/client/admin-sortable2.ts#L214

This line selects the <thead> of the field's widget <table> and attaches the event listeners to it in if (tBody) {...}

image

This causes the UI to not handle the drag events correctly:
ezgif-5-393cf1496a

As a workaround, I've copy-pasted the compiled version of adminsortable2.js to myproject/static/adminsortable2/js/adminsortable2.js and modified the InlineSortable's constructor as follows:

...
  var InlineSortable = class {
    constructor(inlineFieldSet) {
      this.reversed = inlineFieldSet.classList.contains("reversed");

      // Old code - selects any child (even tables inside fields' widgets)
      // const tBody = inlineFieldSet.querySelector("table tbody");

      // Fixed code - selects only direct child table
      let tBody = null;
      const table = inlineFieldSet.querySelector("table");
      if (table && table.parentElement === inlineFieldSet) {
        tBody = table.querySelector("tbody");
      }

      if (tBody) {
...

I am using:

Django==4.2.9
django-admin-sortable2==2.1.10
django-json-widget==1.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions