Topic: Filtering table rows leaves the action menu attached to a different record

Nina Laurent free asked 1 week ago


We have an action dropdown in each row of a filtered results table. If I open one menu and then change the search text, the expanded menu sometimes appears on another record. Clicking its action still uses the row currently rendered there.

The row wrapper and menu state both use the visible array index. The record has an immutable ID from our API. Should the open-menu state use that ID as well as the React key?


Mateusz Lazaru staff answered 1 week ago


Use the record ID for both row identity and any application-owned open-menu state. An index can point to a different record after filtering, so retaining openIndex=2 does not necessarily mean the original record remains open.

Also close the menu when that ID is no longer in the visible results. Keep the component's documented toggle behavior separate from the state that identifies which record an action belongs to.

const [openRecordId, setOpenRecordId] = useState(null);
useEffect(() => {
  if (!visibleRows.some(row => row.id === openRecordId)) {
    setOpenRecordId(null);
  }
}, [visibleRows, openRecordId]);

https://mdbootstrap.com/docs/react/components/dropdowns/


Nina Laurent free commented 1 week ago

Changing the row keys and the stored menu value to the API ID fixed the mismatch. I added the visibility check too, so filtering the open record out now closes its menu. Sorting the results no longer transfers the expanded state to another row.



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB React
  • MDB Version: MDB5 9.0.0
  • Device: Laptop
  • Browser: Chrome
  • OS: Linux
  • Provided sample code: No
  • Provided link: No