list

package
v2.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package list implements creation of lists (old tablelist).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build[T Listable](arr []T) ([]core.Row, error)

Build is responsible to receive a collection of objects that implements Listable and build the rows of TableList. This method should be used in case of a collection of values.

Example

ExampleBuild demonstrates how to create a list of rows based into an array of objects.

package main

import (
	"fmt"

	"github.com/flanksource/maroto/v2"
	"github.com/flanksource/maroto/v2/pkg/components/list"
	"github.com/flanksource/maroto/v2/pkg/components/row"
	"github.com/flanksource/maroto/v2/pkg/components/text"
	"github.com/flanksource/maroto/v2/pkg/core"
)

// Obj implements Listable interface
type Obj struct {
	ID   int
	Name string
}

// GetHeader is a method from Listable interface to create a header row
// based in an array element.
func (o Obj) GetHeader() core.Row {
	idCol := text.NewCol(6, "ID")
	nameCol := text.NewCol(6, "Name")
	return row.New(5).Add(idCol, nameCol)
}

// GetContent is a method from Listable interface to create a row
// based in an array element.
// i is the current index of the object list to be added into a row
// this can be used to customize pair/odd rows.
func (o Obj) GetContent(_ int) core.Row {
	idCol := text.NewCol(6, fmt.Sprintf("%d", o.ID))
	nameCol := text.NewCol(6, o.Name)
	return row.New(5).Add(idCol, nameCol)
}

func main() {
	/*
		// Obj implements Listable interface
		type Obj struct {
			ID   int
			Name string
		}

		// GetHeader is a method from Listable interface to create a header row
		// based in an array element.
		func (o Obj) GetHeader() core.Row {
			idCol := text.NewCol(6, "ID")
			nameCol := text.NewCol(6, "Name")
			return row.New(5).Add(idCol, nameCol)
		}

		// GetContent is a method from Listable interface to create a row
		// based in an array element.
		// i is the current index of the object list to be added into a row
		// this can be used to customize pair/odd rows.
		func (o Obj) GetContent(_ int) core.Row {
			idCol := text.NewCol(6, fmt.Sprintf("%d", o.ID))
			nameCol := text.NewCol(6, o.Name)
			return row.New(5).Add(idCol, nameCol)
		}
	*/

	objs := []Obj{
		{
			ID:   0,
			Name: "obj name 0",
		},
		{
			ID:   1,
			Name: "obj name 1",
		},
	}

	rows, _ := list.Build[Obj](objs)

	m := maroto.New()
	m.AddRows(rows...)

	// generate document
}

func BuildFromPointer

func BuildFromPointer[T Listable](arr []*T) ([]core.Row, error)

BuildFromPointer is responsible to receive a collection of objects that implements Listable and build the rows of TableList. This method should be used in case of a collection of pointers.

Example

ExampleBuild demonstrates how to create a list of rows based into an array of pointer objects.

package main

import (
	"fmt"

	"github.com/flanksource/maroto/v2"
	"github.com/flanksource/maroto/v2/pkg/components/list"
	"github.com/flanksource/maroto/v2/pkg/components/row"
	"github.com/flanksource/maroto/v2/pkg/components/text"
	"github.com/flanksource/maroto/v2/pkg/core"
)

// Obj implements Listable interface
type Obj struct {
	ID   int
	Name string
}

// GetHeader is a method from Listable interface to create a header row
// based in an array element.
func (o Obj) GetHeader() core.Row {
	idCol := text.NewCol(6, "ID")
	nameCol := text.NewCol(6, "Name")
	return row.New(5).Add(idCol, nameCol)
}

// GetContent is a method from Listable interface to create a row
// based in an array element.
// i is the current index of the object list to be added into a row
// this can be used to customize pair/odd rows.
func (o Obj) GetContent(_ int) core.Row {
	idCol := text.NewCol(6, fmt.Sprintf("%d", o.ID))
	nameCol := text.NewCol(6, o.Name)
	return row.New(5).Add(idCol, nameCol)
}

func main() {
	/*
		// Obj implements Listable interface
		type Obj struct {
			ID   int
			Name string
		}

		// GetHeader is a method from Listable interface to create a header row
		// based in an array element.
		func (o Obj) GetHeader() core.Row {
			idCol := text.NewCol(6, "ID")
			nameCol := text.NewCol(6, "Name")
			return row.New(5).Add(idCol, nameCol)
		}

		// GetContent is a method from Listable interface to create a row
		// based in an array element.
		// i is the current index of the object list to be added into a row
		// this can be used to customize pair/odd rows.
		func (o Obj) GetContent(_ int) core.Row {
			idCol := text.NewCol(6, fmt.Sprintf("%d", o.ID))
			nameCol := text.NewCol(6, o.Name)
			return row.New(5).Add(idCol, nameCol)
		}
	*/

	objs := []*Obj{
		{
			ID:   0,
			Name: "obj name 0",
		},
		{
			ID:   1,
			Name: "obj name 1",
		},
	}

	rows, _ := list.BuildFromPointer[Obj](objs)

	m := maroto.New()
	m.AddRows(rows...)

	// generate document
}

Types

type Listable

type Listable interface {
	GetHeader() core.Row
	GetContent(i int) core.Row
}

Listable is the main abstraction of a listable item in a TableList. A collection of objects that implements this interface may be added in a list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL