Playground
gnomod.toml
module = "gno.land/p/samcrew/tablesort/v0" gno = "0.9" [addpkg] creator = "g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen"
render.gno
package tablesort
import (
"net/url"
"strings"
"gno.land/p/mason/md/v0"
"gno.land/p/nt/ufmt/v0"
)
// Table holds the headings and rows for rendering.
// Each row must have the same number of cells as there are headings.
type Table struct {
Headings []string // ["A", "B", "C"]
Rows [][]string // [["a1","b1","c1"], ["a2","b2","c2"], ...]
}
// Render generates a Markdown table from a Table struct with sortable columns based on URL params.
// paramPrefix is an optional prefix for in the URL to identify the tablesort Renders (e.g. "members-").
func Render(u *url.URL, table *Table, paramPrefix string) string {
direction := ""
currentHeading := ""
if h := u.Query().Get(paramPrefix + "sort-asc"); h != "" {
direction = "asc"
currentHeading = h
} else if h := u.Query().Get(paramPrefix + "sort-desc"); h != "" {
direction = "desc"
currentHeading = h
}
var sb strings.Builder
// Find the index of the column to sort
colIndex := -1
for i, h := range table.Headings {
if h == currentHeading {
colIndex = i
break
}
}
// Sort rows if necessary
if colIndex != -1 {
SortRows(table.Rows, colIndex, direction == "asc")
}
// Build header
sb.WriteString(buildHeader(u, table.Headings, currentHeading, direction, paramPrefix))
sb.WriteString("\n")
numCols := len(table.Headings)
// Build rows
for i, row := range table.Rows {
// Validate row length
if len(row) != numCols {
return "tablesort fails: row " + ufmt.Sprintf("%d", i+1) + " has " +
ufmt.Sprintf("%d", len(row)) + " cells, expected " +
ufmt.Sprintf("%d", numCols) + ", because there are " + ufmt.Sprintf("%d", numCols) + " columns.\n"
}
sb.WriteString("|")
for _, cell := range row {
sb.WriteString(" " + cell + " |")
}
sb.WriteString("\n")
}
return sb.String()
}
// buildHeader builds the Markdown header row with clickable links and arrows
func buildHeader(u *url.URL, headings []string, currentHeading, direction string, paramPrefix string) string {
var sb strings.Builder
sb.WriteString("|")
for _, h := range headings {
arrow := ""
if h == currentHeading {
if direction == "asc" {
arrow = " ↑"
} else if direction == "desc" {
arrow = " ↓"
}
}
// Build URL for the header link with toggle logic
newURL := *u
q := newURL.Query()
if h == currentHeading {
// Toggle sort direction
if direction == "asc" {
q.Del(paramPrefix + "sort-asc")
q.Set(paramPrefix+"sort-desc", h)
} else {
q.Del(paramPrefix + "sort-desc")
q.Set(paramPrefix+"sort-asc", h)
}
} else {
// First click defaults to descending
q.Del(paramPrefix + "sort-asc")
q.Set(paramPrefix+"sort-desc", h)
}
newURL.RawQuery = q.Encode()
link := md.Link(h+arrow, newURL.String())
sb.WriteString(" " + link + " |")
}
sb.WriteString("\n|")
for range headings {
sb.WriteString(" --- |")
}
return sb.String()
}
tablesort.gno
// Package tablesort provides functionality to render a Markdown table with sortable columns.
// It allows users to click on column headers to sort the table in ascending or descending sort direction.
// The sorting state is managed via URL query parameters.
// It displays an error if the table is malformed (e.g. rows with missing cells).
// Multiple tablesort can be rendered on the same page by using a paramPrefix for each Render (See the Render function).
package tablesort
import (
"sort"
)
// rowSorter implements sort.Interface for sorting rows by a specific column.
type rowSorter struct {
rows [][]string
colIndex int
ascending bool
}
func (rs rowSorter) Len() int {
return len(rs.rows)
}
func (rs rowSorter) Less(i, j int) bool {
iCell := rs.rows[i][rs.colIndex]
jCell := rs.rows[j][rs.colIndex]
if rs.ascending {
return iCell < jCell
}
return iCell > jCell
}
func (rs rowSorter) Swap(i, j int) {
rs.rows[i], rs.rows[j] = rs.rows[j], rs.rows[i]
}
// SortRows sorts the rows slice by a given column index and direction
func SortRows(rows [][]string, colIndex int, ascending bool) {
sort.Sort(rowSorter{rows, colIndex, ascending})
}
tablesort_test.gno
package tablesort
import (
"net/url"
"strings"
"testing"
)
func TestRender(t *testing.T) {
// --- Case 1: Testing Render with a Table with "Role" column sorted descending and param prefix "members-"
table := &Table{
Headings: []string{"Tier", "Member", "Role"},
Rows: [][]string{
{"T1", "g11111", "finance-officer"},
{"T2", "g22222", "developer"},
{"T3", "g33333", "developer"},
},
}
u, _ := url.Parse("/test?members-sort-desc=Tier")
md := Render(u, table, "members-")
expected := "| [Tier ↓](/test?members-sort-asc=Tier) | [Member](/test?members-sort-desc=Member) | [Role](/test?members-sort-desc=Role) |\n" +
"| --- | --- | --- |\n" +
"| T3 | g33333 | developer |\n" +
"| T2 | g22222 | developer |\n" +
"| T1 | g11111 | finance-officer |\n"
// Trim spaces for comparison
md = strings.TrimSpace(md)
expected = strings.TrimSpace(expected)
if md != expected {
t.Errorf("Render() output mismatch.\nExpected:\n%s\nGot:\n%s", expected, md)
}
// --- Case 2: Testing Render with an invalid Table (row with missing cell)
table = &Table{
Headings: []string{"Tier", "Member", "Role"},
Rows: [][]string{
{"T1", "g11111"}, // Missing the "Role" cell
},
}
md = Render(u, table, "")
expected = "tablesort fails: row 1 has 2 cells, expected 3, because there are 3 columns.\n"
if md != expected {
t.Errorf("Expected error message:\n%s\nGot:\n%s", expected, md)
}
// --- Case 3: Testing SortRows
rows := [][]string{
{"T1", "g11111", "finance-officer"},
{"T2", "g22222", "developer"},
{"T3", "g33333", "developer"},
}
SortRows(rows, 2, false) // Sort by "Role" descending
expected = "finance-officer"
if rows[0][2] != expected {
t.Errorf("SortRows() failed. Expected first role: %s, got: %s", expected, rows[0][2])
}
}