0

I want the user to enter the values and press the plus button to display all the values in the TextField in the data table and in my code to access these records through a list. In the picture below, the data table is created again from the beginning, if I want it to be added as a record in the data table every time the value is entered in the TextField and the add option is pressed by the user. The point is that I want these records to be in the list in the source

def main(page: ft.Page):
    def add_clicked(e):
        page.add(
            ft.DataTable(
                columns=[
                    ft.DataColumn(ft.Text("First name")),
                    ft.DataColumn(ft.Text("Last name")),
                    ft.DataColumn(ft.Text("Age")),
                ],
                rows=[
                    ft.DataRow(
                        cells=[
                            ft.DataCell(ft.Text(new_task.value)),
                            ft.DataCell(ft.Text(new_task1.value)),
                            ft.DataCell(ft.Text(new_task2.value)),
                        ],
                    ),
                ]

            )



        )
        # page.add(ft.DataCell(ft.Text(new_task.value)))
        # page.add(ft.DataCell(ft.Text(new_task1.value)))
        # page.add(ft.DataCell(ft.Text(new_task2.value)))
        # page.add(ft.Checkbox(label=new_task.value))
        # page.add(ft.Checkbox(label=new_task1.value))
        # page.add(ft.Checkbox(label=new_task2.value))
        new_task.value = ""
        new_task1.value = ""
        new_task2.value = ""
        page.update()

    new_task = ft.TextField(hint_text="Whats needs to be done?")
    new_task1 = ft.TextField(hint_text="Whats needs to be done?")
    new_task2 = ft.TextField(hint_text="Whats needs to be done?")

    page.add(new_task,new_task1,new_task2, ft.FloatingActionButton(icon=ft.icons.ADD, on_click=add_clicked),

             )

ft.app(target=main)

enter image description here

Navrang
  • 41
  • 4

1 Answers1

0

To add the new record entered by the user to the existing data table in the list, you can modify the add_clicked function to update the rows list with the new record before creating the DataTable

def add_clicked(e):
    # Get the existing rows
    existing_rows = page.get_table_data("my_table") or []
    
    # Create a new row with the values entered by the user
    new_row = ft.DataRow(
        cells=[
            ft.DataCell(ft.Text(new_task.value)),
            ft.DataCell(ft.Text(new_task1.value)),
            ft.DataCell(ft.Text(new_task2.value)),
        ],
    )
    
    # Add the new row to the existing rows
    rows = existing_rows + [new_row]
    
    # Create the data table with the updated rows
    table = ft.DataTable(
        columns=[
            ft.DataColumn(ft.Text("First name")),
            ft.DataColumn(ft.Text("Last name")),
            ft.DataColumn(ft.Text("Age")),
        ],
        rows=rows,
        name="my_table"
    )
    
    # Replace the existing data table with the new one
    page.replace(table)
    
    # Clear the text fields
    new_task.value = ""
    new_task1.value = ""
    new_task2.value = ""
NoobCoder
  • 625
  • 1
  • 5
  • 18
  • You could also just use a Ref to the existing DataTable and update the rows property; without having to replace the existing table. – Zaren Mar 07 '23 at 00:21