Replies: 3 comments 1 reply
-
I think something like this should work? from textual.app import App, ComposeResult
from textual.widgets import DataTable, Footer
class ExampleApp(App):
BINDINGS = [("u", "update_table", "Update table")]
start = 1
def compose(self) -> ComposeResult:
yield DataTable()
yield Footer()
def populate_table(self) -> None:
table = self.query_one(DataTable)
# Save the current cursor coordinate
cursor_coordinate = table.cursor_coordinate
# After clearing and updating the table...
table.clear()
table.add_rows(
[[self.start + i + j * 3 for i in range(3)] for j in range(3)],
)
# ...restore the cursor coordinate
table.cursor_coordinate = cursor_coordinate
def on_mount(self) -> None:
table = self.query_one(DataTable)
table.add_columns("C1", "C2", "C3")
self.populate_table()
def action_update_table(self) -> None:
self.start += 1
self.populate_table()
if __name__ == "__main__":
app = ExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
-
Hey There, Restoring the cursor coordinates doesn't help in this case. I've a feeling the issue is something to do with my CSS or scroll bar settings, something along those lines. One thing that does 'solve' it is doing this call after adding the rows: whereas, like I say, even doing something like this after adding the rows: Ah, I just realized why that might be, I have self.cursor_type = 'row'. I guess that is going to gum up the works when moving the cursor around? I'm not sure... To clarify, I have: So yes, I just changed it to "cell" type cursor and then yes, doing a self.move_cursor(column=15) actually does what I would expect. I will look at this again tomorrow. I need to use "row" cursor type. Playing around with the cursor seems fraught and inconsistent. It would be much better I feel if it was never reset to 0. I still feel self.clear(columns=False) would be better if it didn't set everything back to (0,0) Would an alternative be for me not to call self.clear at all. But instead keep a record of the row keys I add, then remove them individually each time? That would be more analogous to the self.rows.clear() call I was using in 0.77 that actually worked without issue. Sorry for the ramble, it's very late here. Cheers, |
Beta Was this translation helpful? Give feedback.
-
Two solutions I found this morning. First one is very hacky. But it leads to solution (2) hence worth mentioning. Solution (1) is just to duplicate the code in self.clear() up to self.refresh(). That was essentially my old hack except with the old hack all I did was call self.rows.clear() rather than duplicate all the code. So a working hack is just to call this code rather than self.clear(columns=False):
So if that code works but self.clear(columns=True) doesn't work means it is the extra code in self.clear() which is the issue. The extra code being:
So solution (2) is to instead preserve these attributes around self.clear(columns=False), and indeed that works. Further testing shows it is just self.scroll_x and self.scroll_y that needs preserving. Final code being:
I'm not sure how widely this fix is needed. But certainly in cases where self.cursor_type = "row" it helps. I still feel self.clear() is potentially too greedy clearing attributes when we are just deleting rows. A few times I have problems with this function and the solution has been "preserve stuff before you call it." A different solution might have been to clear our rows myself with self.remove_row(key) rather than calling self.clear() at all. But I didn't explore that in detail. |
Beta Was this translation helpful? Give feedback.
-
The DataTable has horizontal scrollbars to accomodate all the columns. Columns are never cleared. But most user operations clear the rows and repopulate using DataTable.add_rows()
The issue is that when I call DataTable.clear(), it resets all coordinates back to (0,0). So the DataTable jumps back to column 0 each time.... which is obviously not what the user wants.
As a temporary workaround (I was using version 0.77 I think), instead of calling self.clear(), I just called self.rows.clear(). And obviously since that doesn't reset the coordinates back to column 0, there is no DataTable jumping. But now I've upgraded to 1.0, that workaround fails. and 'm back to a DataTable which jumps back to column 0 whenever the user does an operation that requires a row repopulation.
Couldn't there be a version of DataTable.clear() which, if columns=False, it's less aggressive in resetting the coordinates i.e. leave the column coordinates untouched?
I've tried preserving the coordinates around the self.clear() call but it doesn't appear to do anything useful.
Thanks,
Beta Was this translation helpful? Give feedback.
All reactions