JQuery moves up and finds

I have a table that uses jQuery sort to drag and sort items in a table.

When dropping an item in "sortable-row" I want to read the data identifier from the previous "row-section".

I managed to achieve this when the row is deleted in the row immediately below the "row-section" or two levels below.

First level down:

($(ui.item[0]).closest('tr').prev('.stop-row').data('id');

Second level down:

$(ui.item[0].previousElementSibling).closest('tr').prev('tr.stop-row').data('id');

The problem is that any number of lines can be added to the "section", so I need some kind of loop that will check if the previous item is a "section-line", if not, check the next previous one.

How do I achieve this with jQuery?

My table:

<table class="table table-bordered table-responsive">
    <thead>
        <tr>
            <td>Assignment</td>
        </tr>
        <tr>
            <td>Type</td>
        </tr>
        <tr>
            <td>Time</td>
        </tr>
        <tr>
            <td>Level</td>
        </tr>
    </thead>

    <tbody class="sortable">
        <tr class="section-row" data-id="{{ id }}">
            <td> Section-{{ id }}</td>
        </tr>
        <tr class="sortable_row">
            <td>{{ assignment }}</td>
            <td>{{ type }}</td>
            <td>{{ time }}</td>
            <td>{{ level }}</td>
        </tr>
    </tbody>
</table>
+4
1

: $(ui.item[0]).closest('tr').prevAll('.section-row:first').data('id');

tr, tr .section-row.

+6

Source: https://habr.com/ru/post/1660508/


All Articles