Evertras / bubble-table

A customizable, interactive table component for the Bubble Tea framework
MIT License
451 stars 27 forks source link

Unclear how to show selected column with dynamic row updates #163

Closed sho-87 closed 8 months ago

sho-87 commented 8 months ago

In the features example, there is a column that shows automatically to indicate which rows the user has selectable. As I understand, that column does not need to be manually specified - you simply set SelectableRows(true) on the model. I'm trying to get that column to display when the row data is being dynamically populated.

The table is created like this:

// this is the underlying bubbletea model
type model struct {
    table    projectsTable
    projects []Project
}

type projectsTable struct {
    model table.Model
}

func createProjectsTable() projectsTable {
    columns := generateColumns()
    rows := generateRowsFromProjects([]Project{})

    model := projectsTable{
        model: table.New(columns).
            WithRows(rows).
            SelectableRows(true).
            WithSelectedText(" ", "Y").
            Focused(true).
            SortByAsc(columnLastModified),
    }
    return model
}

func generateColumns() []table.Column {
    columns := []table.Column{
        table.NewFlexColumn(columnName, "Name", 1),
        table.NewFlexColumn(columnPath, "Path", 3),
        table.NewColumn(columnLastModified, "Last Modified", 20),
    }
    return columns
}

func generateRowsFromProjects(projects []Project) []table.Row {
    rows := []table.Row{}
    for _, entry := range projects {
        row := table.NewRow(table.RowData{
            columnName:         entry.Name,
            columnPath:         entry.Path,
            columnLastModified: entry.LastModified.Format("2006-01-02 15:04:05"),
        })
        rows = append(rows, row)
    }

    return rows
}

In the initial state where rows is empty, the selected column displays correctly

But I have a keybinding that lets the user regenerate the rows when m.projects data is updated:

m.table.model = m.table.model.WithRows(generateRowsFromProjects(m.projects)).WithColumns(generateColumns())

When that happens, the table updates with the correct data but the select column is no longer visible

Is there something special that needs to be done when the rows are being updated/replaced using WithRows()? I couldn't find anything in the documentation about this as it seems everything related to the select column is handled automatically under the hood

sho-87 commented 8 months ago

so after messing around with this a bit more, it seems the problem is with WithColumns(generateColumns()). this works correctly if I remove that call, but shouldn't the select column be automatically added even if I respecify which columns are displayed?

Evertras commented 8 months ago

The simple answer to your original question: this is a bug. The selected text column should properly persist after WithColumns is called again. I can reproduce this locally, and this should be fixed.

The more specific answer to your particular case is that you shouldn't need to call WithColumns if your columns aren't changing. The columns alone should persist.

Evertras commented 8 months ago

Interestingly, I'm also realizing now that the select functionality won't persist after WithRows is called, because it doesn't know which new row corresponds to an old selected row. That may need some further thought, and if you need this functionality let's open a new issue. This issue will just be scoped to fixing the disappearing column, I just wanted to point that out as a potential followup.

Evertras commented 8 months ago

Whoops, I didn't mean to let it autoclose. I'll let you close this if you consider it fixed, or I'll close it myself later if there's no reply for a bit. Released as v0.15.7, using WithColumns and SelectableRows should properly keep the selected column in place after changing columns.

That being said, you shouldn't need to use WithColumns in your given example, so you may want to get rid of it anyway unless you really are changing columns around on the fly.

sho-87 commented 8 months ago

just tried it out and it works great, thanks for your speed!