I did something today about re-writing tables. But its not a clear example as I did it in the context for what I needed. I have a TinyDB setup, with tables representing sections and records with in the section(table). To keep this as generic as possible for the events I need to deal with in the ui, sections and rows are indices. So section(0,0) is section 0, row 0 etc...
So below is my first attempt at moving the rows around. 2 cases, in the same section, or from one section to another section.
Seems like its doing a lot, but on my ipad I dont see/feel/perceive any lag. I need to do more testing, also with a larger dataset. But so far it is working well.
Sorry its not runnable code, but only makes sense in Pythonista App. But still I have listed the steps I have done.
def move_row(self,
from_section=0, from_row=0,
to_section=0, to_row=0):
with TinyDB(self.db_fn) as db:
if from_section == to_section:
tbl = self.get_section_tbl(db, from_section)
'''
*. In section moving a row
1. read in all the records into a list
2. swap the from & to rows in the list
3. delete all the records from the section
4. rewite the records to the section
'''
recs = tbl.all()
recs[to_row], recs[from_row] = recs[from_row], recs[to_row]
doc_ids = list(tbl._read().keys())
tbl.remove(doc_ids=doc_ids)
tbl.insert_multiple(recs)
else:
'''
* Moving a row between sections
1. get the record from the from_section
2. delete the record from the from_section
3. read in all the recs from the to_section into a list
3. insert the read record into the list
4. delete & rewite the records to the section
'''
rec = self.cell_for_row(from_section, from_row)
self.delete(from_section, from_row)
tbl = self.get_section_tbl(db, to_section)
recs = tbl.all()
recs[to_row:to_row] = [rec]
doc_ids = list(tbl._read().keys())
tbl.remove(doc_ids=doc_ids)
tbl.insert_multiple(recs)
A pic of the ui