Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

tablesort source pure

Package tablesort provides functionality to render a Markdown table with sortable columns. It allows users to click o...

Readme View source

tablesort - Sortable markdown tables

Generate sortable markdown tables with clickable column headers. Sorting state is managed via URL query parameters.

Usage

 1import "gno.land/p/samcrew/tablesort"
 2
 3table := &tablesort.Table{
 4    Headings: []string{"Name", "Age", "City"},
 5    Rows: [][]string{
 6        {"Alice", "25", "New York"},
 7        {"Bob", "30", "London"},
 8        {"Charlie", "22", "Paris"},
 9    },
10}
11
12// Basic usage
13u, _ := url.Parse("/users")
14markdown := tablesort.Render(u, table, "")
15
16// Multiple tables on same page (use prefix to avoid conflicts)
17markdown1 := tablesort.Render(u, table, "table1-")
18markdown2 := tablesort.Render(u, table, "table2-")

On-chain Example

API

1type Table struct {
2    Headings []string   // Column headers
3    Rows     [][]string // Table data rows
4}
5
6// `u`: Current URL for generating sort links
7// `table`: Table data structure
8// `paramPrefix`: Prefix for URL params (use for multiple tables)
9func Render(u *url.URL, table *Table, paramPrefix string) string

URL Parameters:

  • {prefix}sort-asc={column}: Sort column ascending
  • {prefix}sort-desc={column}: Sort column descending

URL Examples:

  • /users?sort-desc=Name - Sort by Name descending
  • /page?users-sort-asc=Age&orders-sort-desc=Total - Multiple tables

Overview

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).

Functions 2

func Render

1func Render(u *url.URL, table *Table, paramPrefix string) string
source

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 SortRows

1func SortRows(rows [][]string, colIndex int, ascending bool)
source

SortRows sorts the rows slice by a given column index and direction

Types 1

type Table

struct
1type Table struct {
2	Headings []string   // ["A", "B", "C"]
3	Rows     [][]string // [["a1","b1","c1"], ["a2","b2","c2"], ...]
4}
source

Table holds the headings and rows for rendering. Each row must have the same number of cells as there are headings.

Imports 5

Source Files 5