How can I have a Tanstack Table with a row already...
# javascript
h
How can I have a Tanstack Table with a row already pinned at the start? I followed this example https://tanstack.com/table/v8/docs/framework/react/examples/row-pinning But I get infinite re-render with
Copy code
ColumnDefTemplate { info ->
    if(info.row.getIsPinned() == RowPinningPosition.`false` && info.row.original.materialNumber == rememberPin)
        info.row.pin(RowPinningPosition.top, false, false)
    info.getValue()
}
t
You call
info.row.pin
during render and force rerender
h
I also tried
Copy code
useEffect(rememberPin) {
        productionMaterialTable
            .getRowModel()
            .rowsById
            .toPairList()
            .firstOrNull { (_, row) ->
                row.original.materialNumber == rememberPin
            }
            ?.second?.pin(<http://RowPinningPosition.top|RowPinningPosition.top>, false, false)
    }
But to no avail
Do you have a suggestion how to pin a row depending on a value during construction of the table?
t
You should set "isPinned" before render
h
But then it's never pinned, isn't it? I load the site for the first time, load data, and want to pin one of the rows that has a specific value. The value could also come from
props
for example, or for simplicity could just be "pin where materialNumber = 200".
t
useTable
option should contain "isPinned" record (it already contains "isSelected" record)
Also you can modify table state before rendering
h
The
TableOptions
in
useReactTable
don't provide "isPinned"
How do I modify it before rendering? That is what I need.
Copy code
useReactTable<T>(
    options = jso {
        val (expanded, setExpanded) = userSettings.expanded
        val (sorting, setSorting) = userSettings.sorting
        val (grouping, setGrouping) = userSettings.grouping
        val (columnVisibility, setColumnVisibility) = userSettings.columnVisibility
        val (globalFilter, setGlobalFilter) = userSettings.globalFilter
        val (columnFilters, setColumnFilters) = userSettings.columnFilters
        val (showFilters, setShowFilters) = userSettings.showFilters
        val (currentPage, setCurrentPage) = userSettings.page
//        val (rowPinning, setRowPinning) = userSettings.rowPinning
        val (currentRowsPerPage, setCurrentRowsPerPage) = userSettings.rowsPerPage

        this.data = data ?: emptyArray()
        this.columns = columns
        this.state = jso {
//            this.rowPinning = userSettings.rowPinning.component1()
            this.expanded = userSettings.expanded.component1().unsafeCast<ExpandedState>()
            this.sorting = userSettings.sorting.component1()
            this.grouping = userSettings.grouping.component1()
            this.columnVisibility = userSettings.columnVisibility.component1().toRecord()
            this.globalFilter = userSettings.globalFilter.component1()
            this.columnFilters = userSettings.columnFilters.component1()

        }
[...]
You mean here at the
this.state
?
t
Copy code
val table = useTable()

// call table methods
// (pseudocode)
table.state.setPinned(...)

//
// render
Looks like solution
h
That was my very first attempt. The problem there was, I don't know the rowId of the row that MaterialNumber X will inherit.
And the
RowPinningState
expects a rowId in
top
Or will the rowId just be the corresponding index of the array that is put into the
data
of the table?
t
You can set custom rowId function
h
no way
t
key function is what you need
h
Let's see if this works
Copy code
val productionMaterialTable =
        defaultTableOptions(
            productionMaterials,
            productionMaterialColumns,
            materialTableUserSettings
        ) {
            keepPinnedRows = true
            enablePinning = true

            this.getRowId = { row, _, _ ->
                row.materialNumber.value.toString()
            }
        }
    
    setRowPin(jso<RowPinningState> {
        top = arrayOf(rememberPin.value.toString())
    })
Sadly I get infinite rerenders with this:
Copy code
val productionMaterialTable =
    defaultTableOptions(
        productionMaterials,
        productionMaterialColumns,
        materialTableUserSettings
    ) {
        keepPinnedRows = true
        enablePinning = true
        this.getRowId = { row, _, _ ->
            row.materialNumber.value.toString()
        }
    }
useEffect(rememberPin) {
    println(rememberPin)
    if(rememberPin.value >= 0) {
        println("SET PIN")
        setRowPin(jso<RowPinningState> {
            top = arrayOf(rememberPin.value.toString())
            bottom = arrayOf()
        })
    }
}
I also tried it without the
useEffect
Okay this works:
Copy code
val productionMaterialTable =
    defaultTableOptions(
        productionMaterials,
        productionMaterialColumns,
        materialTableUserSettings
    ) {
        keepPinnedRows = true
        enablePinning = true

        if(rememberPin.value >= 0) {
            state.rowPinning = jso<RowPinningState> {
                top = arrayOf(rememberPin.value.toString())
                bottom = arrayOf()
            }
        }
        
        this.getRowId = { row, _, _ ->
            row.materialNumber.value.toString()
        }
    }
Thank you for the help and pointers 💝
😀 1