How to make editable table rows #4402
ed2050
started this conversation in
Show and tell
Replies: 1 comment
-
You might be able to do the same thing with pure css3 instead of brython. With I don't like pure css approach because css is tricky. Execution order isn't always clear, and css application rules lead to surprising results. There may be conflicts with quasar or tailwind rules. Personally I wouldn't attempt this way. But it may be possible. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This question has come up in forum before. Such as #3754.
Here's my solution to making a table with editable rows. It uses brython to run python code in the browser, instead of messing with javascript and quasar / vue slots.
Problem
Some notes on the problem.
ui.table
objects only allow manipulating the top-level table.Approach
Instead of putting edit buttons directly inside the table, we can break the problem into 2 parts:
This approach avoids any knowledge of slots or js or vue / quasar. Part 1 is trivial with nicegui. For part 2, we'll use brython to reposition buttons dynamically.
Solution
Here's a screenshot of the end result. The edit drawer on right isn't fully implemented. I just put a drawer showing the item, you can replace with item edit fields as needed.

Entire solution is available in these 2 files:
Just put them in same directory and run
python edit-table.py
.Code
I'll explain the solution in snippets. Let's start with our nicegui code.
Nicegui page
First our page function. It's very straightforward. Make random table data, set columns, build table, add edit buttons.
Things to note:
uid
field.editable
on first table cell in each row, so brython can find them all.uid-XXXX
on first table cell so brython can find the uid for that row.---------- Show code ----------
Loading Brython
Now some plumbing. Our nicegui page needs a way to include and launch brython code. We do that in the
shared_headers ()
call, which is shared with all our nicegui pages.shared_headers
adds scripts and stylesheets to document head, so we need to call it before building the page.Things to note:
rowedit.py
, which is added to page head with type "text/python". brython will detect and execute this script.---------- Show code ----------
Brython Script
Here's the meat of our approach. Our brython script scans the page for table cells and moves the corresponding button next to them. Let's start with the
place_button
function that takes a table cell as arg, finds corresponding button, calculates new position, and places it.---------- Show code ----------
Next we have a function
scan_rows
to scan the doc for all visible table rows and callplace_button
on each.---------- Show code ----------
Finally, we need a small piece of code to update the buttons when rows change (ie when user clicks pagination controls to view next / previous page of data). We'll make a mutation observer function
watch_rows
to listen for change in the DOM and callscan_rows
to update.---------- Show code ----------
Features
Why I like this approach better than vue / quasar / slots approach:
Potential Drawbacks
The brython approach does have a few differences that some might consider drawbacks (they're strengths to me):
That's off the top of my head.
More info
For more info, check out brython docs, in particular:
Possible Improvements
There are many ways to possibly improve the code. A few that come to mind:
watch_rows
to only act on changes which affect the table, instead of reacting to every change on the page. I didn't bother because I wanted to keep the code clean, if a bit inefficient. If you update other parts of the page often, you'll callscan_rows
a lot unnecessarily.scan_rows
.querySelectorAll
is a bit inefficient. You could try giving table cells (or rows) an id instead of classes forscan_rows
to locate them. I don't know how hard that would be. Manipulating table cells / rows in nicegui is difficult.Files
Entire solution is available in these 2 files:
Just put them in same directory and run
python edit-table.py
.Beta Was this translation helpful? Give feedback.
All reactions