Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3k stars 880 forks source link

[Bug] CRUD field JS Does Not Play Nicely When Sorting Repeatable #5482

Open zachweix opened 3 months ago

zachweix commented 3 months ago

Bug report

What I did

I added a rule in a repeatable crud field with a subfield

crud.field('rules').subfield('always_active').onChange(function(field) {
    if (field.value == 1) {
        crud.field('zmanim').subfield('when_to_show', field.rowNumber).hide();
    } else {
        crud.field('zmanim').subfield('when_to_show', field.rowNumber).show();
    }
}).change();

I then added more rows to the repeatable field and rearranged them.

What I expected to happen

Changing "always_active" shows/hides "when_to_show" on the same row.

What happened

Changing "always_active" shows/hides "when_to_show" on a different row.

What I've already tried to fix it

I've tried tweaking the repeatable blade file, but was unable to find the source of the problem. Essentially, when we are creating the rule, we are binding the specific field being edited to a row number. If instead we either bind a row number edited to a row number or if we bind the field to its neighbor, no matter the row - either option should fix it, I just couldn't think of the best way to do that

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there? Yes

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

PHP 8.3.2-1+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Jan 20 2024 14:16:40) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.2, Copyright (c) Zend Technologies with Zend OPcache v8.3.2-1+ubuntu22.04.1+deb.sury.org+1, Copyright (c), by Zend Technologies with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans

LARAVEL VERSION:

11.0.7.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.3.0 backpack/crud: 6.7.0 backpack/editable-columns: 3.0.1 backpack/generators: v4.0.4 backpack/pro: 2.1.8 backpack/theme-tabler: 1.2.8

karandatwani92 commented 3 months ago

Hey @zachweix Your code works perfectly:

crud.field('features').subfield('quantity').onChange(function(field) {
    if (field.value == 0) {
        crud.field('features').subfield('value', field.rowNumber).hide();
    } else {
        crud.field('features').subfield('value', field.rowNumber).show();
    }
}).change();

chrome-capture-2024-2-28

BUT after reordering, the row number stays the same & causes the BUG: chrome-capture-2024-2-28 (1)

I have assigned my colleague to fix it. Thanks for reporting.

zachweix commented 3 months ago

Thank you very much!