How Edit differents rows in DataTable?

105 Views Asked by At

I want to edit records in the DataTable, I enter 2 or more records in the DataTable. When I click the edit button (IconButton) on the second or third registration I only get it to load the data of the first record, I leave you an image and the code I hope soon help or suggestions, thanks.

image show data of first regist

import flet as ft
from flet import *


def main(page: ft.Page):

    codpro = TextField(label="CÓDIGO PRODUCTO")
    nompro = TextField(label="NOMBRE PRODUCTO")

    #combo de categoria
    ddcate = ft.Dropdown(label="CATEGORIA",
        width=150,
        options=[
            ft.dropdown.Option("dulce"),
            ft.dropdown.Option("salado"),
        ],
    )

    #combo de peso
    ddpeso = ft.Dropdown(label="PESO",
        width=100,
        options=[
            ft.dropdown.Option("KG"),
            ft.dropdown.Option("G"),
        ],
    )

    # CREANDO LOS CONTROLES PARA EDITAR DATOS
    edit_nomprotxt = TextField(label="NOMBRE PRODUCTO")

    edit_ddcate = ft.Dropdown(label="CATEGORIA",
        width=150,
        options=[
            ft.dropdown.Option("dulce"),
            ft.dropdown.Option("salado"),
        ],
    )

    edit_ddpeso = ft.Dropdown(label="PESO",
        width=100,
        options=[
            ft.dropdown.Option("KG"),
            ft.dropdown.Option("G"),
        ],
    )


    #DATATABLE
    tablaproducto = DataTable(
                  #CABECERA DE LAS CALUMNAS  
                  columns=[
                      DataColumn(Text("CÓDIGO")),
                      DataColumn(Text("PRODUCTO")),
                      DataColumn(Text("CATEGORÍA")),
                      DataColumn(Text("PESO")),
                      DataColumn(Text("ACCIONES")),
                  ],
                  #FILAS  
                  rows=[]          
    )

Create this function for edit data but only load data of first regist

 # Crear Dialog
    dialog = AlertDialog(
        title=Text("Editar datos"),
        content=Column([
            edit_nomprotxt,
            edit_ddcate,
            edit_ddpeso,
                     
        ]),
        actions=[
            TextButton("Guardar",
                       on_click=guardar
                      )
        ]
    )

 #Funcion EDITAR
    def editar(e):
        
        edit_nomprotxt.value = tablaproducto.rows[0].cells[1].content.value
        edit_ddcate.value = tablaproducto.rows[0].cells[2].content.value
        edit_ddpeso.value = tablaproducto.rows[0].cells[3].content.value

        page.dialog = dialog
        dialog.open = True
        page.update()

1

There are 1 best solutions below

0
On

this is solutions:

def editar(e):

    
    e.control.data = "ON"
    filas = tablaproducto.rows[:]
    for valor in filas:
        if valor.cells[-1].content.controls[1].data == 'ON':
            fila = filas.index(valor)


    e.control.data = "OFF"
    edit_nomprotxt.value = tablaproducto.rows[fila].cells[1].content.value
    edit_ddcate.value = tablaproducto.rows[fila].cells[2].content.value
    edit_ddpeso.value = tablaproducto.rows[fila].cells[3].content.value