Skip to content

ProList 高级列表 ​

用 metas 描述列表项的标题、描述、头像、内容和操作,支持选择、展开、分页和卡片网格。

何时使用 ​

  • 项目、应用、文章等需要统一列表项结构的页面。
  • 需要远程分页、批量选择或卡片网格时使用;复杂多列数据使用 ProTable 高级表格。

在线体验 ​

ProList 高级列表 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
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()
}

常见用法 ​

选择、展开和分页 ​

go
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()
}

卡片网格 ​

go
func cards(c *ui.Context, rows []project, metas ui.ProListMetas[project]) {
    ui.ProList(c, rows, metas).Grid(3).Bordered().Show()
}

远程分页 ​

go
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) 估算。选择和展开状态按键保存,与条目是否被构建无关。条目内的控件滚出视口时会失去键盘焦点。

go
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 入门。

ProList 高级列表

gallery 演示 ProList 的注册调用(含 Render 函数),源码 page_pro3_list.go#L55。

加载中…

ProList 远程分页

gallery 演示 ProListRequest 的注册调用(含 Render 函数),源码 page_p2e_more.go#L118。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.ProList ​

go
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()
方法说明签名
BorderedBordered frames the list.Bordered() *ProListW[T]
DefaultExpandAllRowsDefaultExpandAllRows controls the initial expansion of each row (default true).DefaultExpandAllRows(on bool) *ProListW[T]
EmptyTextEmptyText replaces the empty-state text.EmptyText(s string) *ProListW[T]
ErrErr returns the error of the last Request.Err() error
EstimatedItemHeightEstimatedItemHeight is the height Virtual assumes for an item not measured yet (List.EstimatedItemHeight).EstimatedItemHeight(px float32) *ProListW[T]
ExpandRowByClickExpandRowByClick toggles expansion when the row body is activated.ExpandRowByClick() *ProListW[T]
ExpandableExpandable adds an expand toggle per item showing fn below it.Expandable(fn func(c *Context, row T)) *ProListW[T]
ExpandedRowKeysExpandedRowKeys binds the expanded keys to application-owned state.ExpandedRowKeys(keys *[]string) *ProListW[T]
GridGrid lays the items out as cards in n columns.Grid(n int) *ProListW[T]
HeaderTitleHeaderTitle sets the toolbar title.HeaderTitle(s string) *ProListW[T]
HeightHeight fixes the height of the items' viewport (List.Height); the toolbar, selection bar and pager stay outside it.Height(px float32) *ProListW[T]
IDID returns the widget ID (valid after Show).ID() ID
IsLoadingIsLoading reports a Request in flight.IsLoading() bool
ItemLayoutItemLayout selects horizontal (default) or vertical item actions.ItemLayout(s string) *ProListW[T]
LoadingLoading shows the loading mask.Loading(on bool) *ProListW[T]
OnItemClickOnItemClick makes the items (cards in grid mode) clickable.OnItemClick(fn func(row T)) *ProListW[T]
PagePage returns the current page (1-based).Page() int
PaginationPagination shows n items per page with a pager.Pagination(n int) *ProListW[T]
ReloadReload loads the current page again (valid after Show).Reload()
RequestRequest 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]
RowExpandableRowExpandable determines which rows offer an expand control.RowExpandable(fn func(T) bool) *ProListW[T]
RowKeyRowKey gives rows stable keys (selection and expansion follow them; default: the row index).RowKey(f func(T) string) *ProListW[T]
SelectionSelection adds a checkbox per item bound to the selected keys.Selection(keys *[]string) *ProListW[T]
SelectionChangedSelectionChanged reports a selection change in the last Show.SelectionChanged() bool
ShowShow builds the list.Show() *ProListW[T]
SplitSplit toggles the lines between items (default on).Split(on bool) *ProListW[T]
ToolBarToolBar builds actions at the right of the toolbar.ToolBar(fn func()) *ProListW[T]
TotalTotal returns the total count reported by Request.Total() int
VirtualVirtual 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.

字段说明类型
Titlefunc(row T) string
SubTitlenext to the title (tags)func(c *Context, row T)
Descriptionfunc(row T) string
Avatarfunc(c *Context, row T)
Contentmiddle areafunc(c *Context, row T)
Actionslinks at the end, split by barsfunc(c *Context, row T)
Extraright-most (an image)func(c *Context, row T)

相关链接 ​

以 MIT 许可发布