ProList 高级列表
用 metas 描述列表项的标题、描述、头像、内容和操作,支持选择、展开、分页和卡片网格。
何时使用
- 项目、应用、文章等需要统一列表项结构的页面。
- 需要远程分页、批量选择或卡片网格时使用;复杂多列数据使用 ProTable 高级表格。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
type project struct{ Key, Name, Desc string }
func page(c *ui.Context) {
rows := []project{{"p1", "TanGo", "UI 框架"}, {"p2", "MinCode", "IDE"}}
ui.ProList(c, rows, ui.ProListMetas[project]{
Title: func(r project) string { return r.Name },
Description: func(r project) string { return r.Desc },
}).HeaderTitle("项目列表").RowKey(func(r project) string { return r.Key }).Show()
}常见用法
选择、展开和分页
var selectedProjects []string
func selectable(c *ui.Context, rows []project, metas ui.ProListMetas[project]) {
ui.ProList(c, rows, metas).RowKey(func(r project) string { return r.Key }).
Selection(&selectedProjects).Pagination(5).Expandable(func(c *ui.Context, r project) {
ui.Text(c, r.Desc)
}).Show()
}卡片网格
func cards(c *ui.Context, rows []project, metas ui.ProListMetas[project]) {
ui.ProList(c, rows, metas).Grid(3).Bordered().Show()
}远程分页
func remote(c *ui.Context, metas ui.ProListMetas[project]) {
ui.ProList(c, nil, metas).Request(func(page, size int) ([]project, int, error) {
return []project{}, 0, nil
}).Pagination(10).Show()
}长列表:虚拟滚动
Height(px) 固定条目区高度,再加 Virtual() 只构建视口内的条目(同 List 的虚拟形态);工具栏、选择提示和分页器留在区外。条目高度(含展开后的高度)滚到时实测、按 RowKey 缓存,未测到的按 EstimatedItemHeight(px) 估算。选择和展开状态按键保存,与条目是否被构建无关。条目内的控件滚出视口时会失去键盘焦点。
func longProList(c *ui.Context, rows []project, metas ui.ProListMetas[project]) {
ui.ProList(c, rows, metas).RowKey(func(r project) string { return r.Key }).
Height(480).Virtual().Show()
}主题与样式
ProList 复用 List、Card 和 Pro 工具栏令牌;Bordered、Split、Grid 控制列表和卡片形态。
注意事项
RowKey 用于保存选择和展开状态,应返回稳定唯一值。远程请求在页码变化时触发,Reload 可手动刷新;大量本地数据应启用分页、远程请求或 Height(px).Virtual()。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.ProList
func ProList[T any](c *Context, rows []T, metas ProListMetas[T]) *ProListW[T]ProList starts a list over rows; chain options, then Show.
返回 *ProListW,可链式调用的方法见 ProListW 方法。
ProListW 方法
ui.ProListW:ProListW is a data-driven list (Ant Design Pro ProList): items built from metas, a toolbar with a title and actions, selection, expandable items, pagination, a loading state and a card grid mode.
ui.ProList(c, rows, metas).HeaderTitle("Projects").RowKey(key).
Selection(&sel).Pagination(5).Show()
| 方法 | 说明 | 签名 |
|---|---|---|
Bordered | Bordered frames the list. | Bordered() *ProListW[T] |
DefaultExpandAllRows | DefaultExpandAllRows controls the initial expansion of each row (default true). | DefaultExpandAllRows(on bool) *ProListW[T] |
EmptyText | EmptyText replaces the empty-state text. | EmptyText(s string) *ProListW[T] |
Err | Err returns the error of the last Request. | Err() error |
EstimatedItemHeight | EstimatedItemHeight is the height Virtual assumes for an item not measured yet (List.EstimatedItemHeight). | EstimatedItemHeight(px float32) *ProListW[T] |
ExpandRowByClick | ExpandRowByClick toggles expansion when the row body is activated. | ExpandRowByClick() *ProListW[T] |
Expandable | Expandable adds an expand toggle per item showing fn below it. | Expandable(fn func(c *Context, row T)) *ProListW[T] |
ExpandedRowKeys | ExpandedRowKeys binds the expanded keys to application-owned state. | ExpandedRowKeys(keys *[]string) *ProListW[T] |
Grid | Grid lays the items out as cards in n columns. | Grid(n int) *ProListW[T] |
HeaderTitle | HeaderTitle sets the toolbar title. | HeaderTitle(s string) *ProListW[T] |
Height | Height fixes the height of the items' viewport (List.Height); the toolbar, selection bar and pager stay outside it. | Height(px float32) *ProListW[T] |
ID | ID returns the widget ID (valid after Show). | ID() ID |
IsLoading | IsLoading reports a Request in flight. | IsLoading() bool |
ItemLayout | ItemLayout selects horizontal (default) or vertical item actions. | ItemLayout(s string) *ProListW[T] |
Loading | Loading shows the loading mask. | Loading(on bool) *ProListW[T] |
OnItemClick | OnItemClick makes the items (cards in grid mode) clickable. | OnItemClick(fn func(row T)) *ProListW[T] |
Page | Page returns the current page (1-based). | Page() int |
Pagination | Pagination shows n items per page with a pager. | Pagination(n int) *ProListW[T] |
Reload | Reload loads the current page again (valid after Show). | Reload() |
Request | Request loads the list page by page from a server: fn runs on a background goroutine with the 1-based page and the page size (set with Pagination, default 10) and returns the page's rows and the total count. | Request(fn func(page, size int) ([]T, int, error)) *ProListW[T] |
RowExpandable | RowExpandable determines which rows offer an expand control. | RowExpandable(fn func(T) bool) *ProListW[T] |
RowKey | RowKey gives rows stable keys (selection and expansion follow them; default: the row index). | RowKey(f func(T) string) *ProListW[T] |
Selection | Selection adds a checkbox per item bound to the selected keys. | Selection(keys *[]string) *ProListW[T] |
SelectionChanged | SelectionChanged reports a selection change in the last Show. | SelectionChanged() bool |
Show | Show builds the list. | Show() *ProListW[T] |
Split | Split toggles the lines between items (default on). | Split(on bool) *ProListW[T] |
ToolBar | ToolBar builds actions at the right of the toolbar. | ToolBar(fn func()) *ProListW[T] |
Total | Total returns the total count reported by Request. | Total() int |
Virtual | Virtual builds only the items in view (List.Virtual; needs Height). | Virtual() *ProListW[T] |
ProListMetas 参数类型
ui.ProListMetas:ProListMetas maps a row to the parts of a ProList item. Every part is optional.
| 字段 | 说明 | 类型 |
|---|---|---|
Title | func(row T) string | |
SubTitle | next to the title (tags) | func(c *Context, row T) |
Description | func(row T) string | |
Avatar | func(c *Context, row T) | |
Content | middle area | func(c *Context, row T) |
Actions | links at the end, split by bars | func(c *Context, row T) |
Extra | right-most (an image) | func(c *Context, row T) |