KivyMD icon indicating copy to clipboard operation
KivyMD copied to clipboard

MDDataTable - CheckBoxes no reset when creating a new instance of the MDDataTable

Open BaggioMarco opened this issue 3 years ago • 11 comments

Description of the Bug

Imagine the following scenario (based on the code provided below):

  • you have an MDDataTable. Its rows were generated on the basis of "self.rowdata" list
  • you select some rows from check boxes and with "self.data_tables.get_row_checks()" you get all rows selected
  • then using the button "remove selected", you remove such data from the "self.rowdata" list that you used to build the MDDataTable and the MDDataTable get reloaded again considering the "self.rowdata" list updated.
  • It opens fine, with no checkbox selected.
  • NOW THE ISSUE: if you click the button "TEST" (self.data_tables.get_row_checks()), it will give you back some rows selected, even if nothing is selected. ---> The problem is that self.data_tables.get_row_checks() seems to keep in memory still the checkbox selected in the previous instance of the MDDataTable.
  • SOLUTION: at the moment I found a workaround to fix this problem. You need to click two times on the checkbox that allow to select/unselect all checkboxes at the same time, so everything became UNSELECTED. This is the only way I found to reset the checkboxes. ---> However this solution required the user to do that manually, and therefore it cannot really be a solution considering that the user is unaware about the issue. The solution would be to do that via code, but I'm not sure a similar method exist currently.

Code and Logs

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.dialog import MDDialog
from kivymd.uix.datatables import MDDataTable
from kivy.metrics import dp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDRaisedButton

KV = '''

FloatLayout:
    MDFlatButton:
        text: "ALERT DIALOG"
        pos_hint: {'center_x': .5, 'center_y': .5}
        on_release: app.show_confirmation_dialog()
'''

class Example(MDApp):
    dialog = None
    rowdata = [['1', '1', '1'],
               ['2', '2', '2'],
               ['3', '3', '3'],
               ['4', '4', '4']]

    def build(self):

        self.layout = MDBoxLayout(size_hint_y=None, height=400)
        self.data_tables = MDDataTable(
            # size_hint=(0.9, 0.6),
            size_hint=(1, 1),
            use_pagination=True,
            check=True,
            column_data=[

                ("Column 1", dp(30)),
                ("Column 2", dp(30)),
                ("Column 3", dp(30)),

            ],
            row_data=self.rowdata,
        )
        self.layout.add_widget(self.data_tables)


        return Builder.load_string(KV)

    def show_confirmation_dialog(self):
        if not self.dialog:
            self.dialog = MDDialog(
                title="Table",
                type="custom",
                content_cls= self.layout,
                size_hint_x = 0.8, # set the horizontal size of the dialog box
                buttons=[
                            MDRaisedButton(
                                text="Remove Selected", on_release=self.removeSelectedRows
                            ),
                            MDRaisedButton(
                                text="TEST", on_release=self.TEST
                            ),
                        ],
            )
        self.dialog.open()

    def removeSelectedRows(self, *args):
        rows2remove_raw = self.data_tables.get_row_checks()
        # print(rows2remove_raw)
        for i in self.rowdata:
            for j in rows2remove_raw:
                if i == j:
                    print(i)
                    self.rowdata.remove(i)
        self.data_tables.row_data = self.rowdata

        # UPDATE THE TABLE
        self.dialog.dismiss()
        self.dialog = None

        self.layout = MDBoxLayout(size_hint_y=None, height=400)
        self.data_tables = MDDataTable(
            # size_hint=(0.9, 0.6),
            size_hint=(1, 1),
            use_pagination=True,
            check=True,
            column_data=[

                ("Column 1", dp(30)),
                ("Column 2", dp(30)),
                ("Column 3", dp(30)),

            ],
            row_data=self.rowdata,
        )
        self.layout.add_widget(self.data_tables)
        self.show_confirmation_dialog()


    def TEST(self, *args):
        checkselectedRows = self.data_tables.get_row_checks()
        print(checkselectedRows)

Example().run()
 

Versions

  • OS: WIndows
  • Python: 3.9
  • Kivy: 2.0.0
  • KivyMD: latest from master branch

BaggioMarco avatar Mar 20 '21 11:03 BaggioMarco

Here a Video, in particular from second 20 to 29 you can spot the issue: https://user-images.githubusercontent.com/72442283/112760361-faf4b500-8ff6-11eb-80d9-e6ac32e6cdfb.mp4

BaggioMarco avatar Mar 28 '21 16:03 BaggioMarco

@BaggioMarco I can't replicate your issue. I've followed your actions exactly as in the video. Please run

pip uninstall kivy
pip uninstall kivymd
pip install kivy
pip install https://github.com/kivymd/KivyMD/archive/master.zip

Versions used to replicate: Python 3.9.2 Kivy: 2.0.0 KivyMD: Latest version from git

kevin-schumann avatar Mar 28 '21 17:03 kevin-schumann

thanks @kevin-schumann I've just unistall and reinstall everything using your commands. The issue now is slightly different,m but the point is always the same. I've just recorded a new video, please have a look to what happen in particular from seconds 23. When a row is removed, the checkboxes are showing the bug. VIDEO: https://user-images.githubusercontent.com/72442283/112762654-1c5a9e80-9001-11eb-8aab-389654c8f50f.mp4

In addition... spotted another ISSUE: t if you remove all rows, the MDdataTable doesn't get empty, instead it will show a single value on the first column, and even if nothing is visually selected, if you click on "TEST" button it will show it as "selected"

BaggioMarco avatar Mar 28 '21 18:03 BaggioMarco

@BaggioMarco Ok, I am able to reproduce this. I'll look into it.

kevin-schumann avatar Mar 30 '21 11:03 kevin-schumann

Hi @kevin-schumann do you have some news about this issue? Thanks in advance

BaggioMarco avatar Apr 07 '21 16:04 BaggioMarco

@KivyMD-Bot @kevin-schumann some news? thank you

BaggioMarco avatar May 07 '21 16:05 BaggioMarco

@BaggioMarco I am sorry for my absence. I've been quite busy lately. I will look into it when I got time for it.

kevin-schumann avatar May 17 '21 00:05 kevin-schumann

@KivyMD-Bot @kevin-schumann some update about the issue?

BaggioMarco avatar Sep 28 '21 18:09 BaggioMarco

@BaggioMarco Most likely, there is no news on this issue. The MDDataTable class is the last in the list of issues to be solved :)

HeaTTheatR avatar Sep 28 '21 18:09 HeaTTheatR

@BaggioMarco I came across this same exact issue. Were you able to find a workaround?

jlucode avatar Mar 08 '22 16:03 jlucode

@jlucode No, I guess that there is not workaround unfortunately.... I hope they will fix it soon because the MDTable is a really useful widget

BaggioMarco avatar Mar 08 '22 16:03 BaggioMarco