Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to batch insert and delete records from Tableview #121

Closed
israel-dryer opened this issue Jan 4, 2022 · 6 comments · Fixed by #124 or #125
Closed

Add methods to batch insert and delete records from Tableview #121

israel-dryer opened this issue Jan 4, 2022 · 6 comments · Fixed by #124 or #125
Labels
enhancement New feature or request

Comments

@israel-dryer
Copy link
Owner

This is possible with the underlying Tableview.view (Treeview), but this api really needs to be exposed with easy to use methods. See this post for a use case.

class Tableview:
    ...

    def delete_rows(self, indices: List=None):
        """Delete rows specified in indices. If indices is None, then
        ALL tablerows are deleted.
        """
        ...

    def insert_rows(self, index: Union[int, str], rows: List):
        """Insert rows specified by index. If index does not exist then
        the records are appended to the end of the table. You can also
        specify the index 'END' to append records at the end of the
        table.
        ...
@israel-dryer israel-dryer added the enhancement New feature or request label Jan 4, 2022
@israel-dryer
Copy link
Owner Author

It might make sense to expose the Tableview._build_table method so that the table can be rebuilt from scratch with column and row data after the table has already been instantiated. It would make sense for several use cases, that the column and row data may need to be rebuilt from scratch without regard to the table's prior contents.

@antrrax
Copy link
Contributor

antrrax commented Jan 5, 2022

One option would be to start the application with the treeview with empty rows, and let the user do the sql query outside the widget. And the result of this sql query will be inserted into the treeview. So the user could do as many queries as they want, and the data would be erased and inserted into the treeview at runtime.


I tried to do something like this, I made some changes I don't know if it generates bad consequences.


  1. to start in with the empty rows: in the private method '_build_table', I commented:
#self._build_table_rows(rowdata)
#self.build_horizontal_scrollbar()
  1. _build_table_rows I made it a public method

  2. in the unload_table_data method I had to do this bad adaptation:

try:
    for row in self._viewdata:
        row.hide()
except:
    pass
  1. ??? You also need to disable the paging buttons when starting with empty lines. And enable again when there is some information in the treeview lines

My custom sql search button:

Externally to the widget I now do this to delete all rows:
self.dt.view.delete(*self.dt.view.get_children())

or like this:

for item in self.dt.view.get_children():
    self.dt.view.delete(item)

clear the rowcount: '_tablerows' I think I would need to make an attribute public.
self.dt._tablerows = []

I do my sql query and inject the result in treeview:
self.dt.build_table_rows(result)

I update the data and do the autofit, reset sort header

self.dt.load_table_data() #except -> row.hide()
self.dt.autofit_columns()
self.dt._column_sort_header_reset()

https://www.mediafire.com/file/g95y9yjsuz5tzew/datable_mod.mp4/file
In the example video, the treeview rows start empty.
When my custom filter is blank, it loads the entire database table.
SELECT * FROM users ORDER BY cod_id ASC

When there is something typed in the custom filter field, it makes another sql query (example from the video: returns the entire database table in descending order.)
SELECT * FROM users ORDER BY cod_id DESC
01


Do these adjustments that I made generate any conflicts in the rest of the widget's code?

@antrrax
Copy link
Contributor

antrrax commented Jan 7, 2022

but this api really needs to be exposed

In addition to the possibility of inserting or deleting lines. A method to update rows would be interesting.

For example, The line information would be loaded into an external form. The user changes the necessary data and updates this new data in the indicated row of the treeview.


@israel-dryer
Copy link
Owner Author

but this api really needs to be exposed

In addition to the possibility of inserting or deleting lines. A method to update rows would be interesting.

For example, The line information would be loaded into an external form. The user changes the necessary data and updates this new data in the indicated row of the treeview.

I think I can make a small tweak to some things I added in the recent PR and get this to work pretty smoothly on updates... You can get the TableRow object via looking it up via the Tableview.get_row method. You can currently set the TableRow._values variable to the new list of values and then call TableRow.refresh to sync that with the underlying treeview.... However, I'm going to make a slight adjustment to the new TableRow.values property by adding a setter that calls the refresh method automatically. So, after this update, you should be able to do something like this to update the values in any given row (assuming the TableRow object is assigned to a variabled called "row" -> row.values = ['one', 'two', 'three']

@israel-dryer israel-dryer linked a pull request Jan 8, 2022 that will close this issue
@israel-dryer
Copy link
Owner Author

One note though... using the syntax row.values = [...] would invoke the setter and the sync would happen automatically. However, another option is to modify an element inside the row such as this row.values[1] = 'four'. This is modifying the contents of the row at index 1. If you did something like this, you would still need to call the refresh method manually.

@israel-dryer
Copy link
Owner Author

image

@israel-dryer israel-dryer linked a pull request Jan 8, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants