The grid component.
import Masu.Components
<.data_grid id="users" rows={@users} label="Users">
<:col field={:name} />
<:col field={:email} />
<:col field={:inserted_at} type={:datetime} label="Joined" />
</.data_grid>data_grid/1 goes in a LiveView. It needs the Masu hook in the
LiveSocket and the Masu CSS (see the README). The rows come from a
list (rows), or from an Ecto query or schema and a repo (source and
repo), which then sorts, filters and counts in the database; see the
Sources guide.
Summary
Functions
Renders a data grid.
Functions
Renders a data grid.
The browser draws only the rows on the screen, and asks the grid for the others as the person scrolls. A click on a column's header sorts by it (with Shift, by it too); the edge of a header resizes its column, and a double click there fits the column to its content. The arrow keys move between the cells, Shift with them selects a range, and ⌘C (Ctrl+C) copies the range as text that a spreadsheet pastes.
Columns
Each :col is a column (see Masu.Column):
field- the key of the row's value (a map key or a struct field);value- a function of the row that computes the value instead; it needs akey, and does not sort or filter with an Ecto source;key- the name of the column inMasu.Testand in filters; the field as a string unless given;label- the header; the field in words unless given;type-:text(the default),:enum,:integer,:number,:decimal,:date,:datetime,:timeor:boolean;widthandmin_width- in pixels;align-:start,:centeror:end(numbers are at the end);sortable,filterableandsearchable-falseto turn off,searchableis on for text and enums;editable- in a grid that edits:falsefor a read-only column, or a function of the row that says whether its cell edits;values- the values of an:enumcolumn, which its editor offers;decimals- the digits after the point of a:decimalor:numbercolumn, whatever the value has.
A :col with content renders each cell, with the row as :let:
<:col :let={user} field={:name}>
<.link navigate={~p"/users/#{user}"}>{user.name}</.link>
</:col>Menus
cell_menu gives each cell a menu (a right click, or the context-menu
key or Shift+F10 on the active cell): copy, filter by the cell's value,
sort. :header_action and :cell_action add items of your own to the
header menus and to the menu of the cells; see Masu.Action and the
Menus of your own guide.
Attributes
id(:string) (required) - the id of the grid.rows(:list) - the rows: maps or structs, or lists of values (seecolumns). Defaults tonil.columns(:list) - for a grid with no:colslots: its columns, each a key, or a keyword list or map of a:col's attributes; without afield, a column reads the row at its place (rows as lists), and without atype, it takes the type of its values (:auto); see the Columns guide. Defaults tonil.source(:any) - an Ecto query or schema (withrepo), or{module, opts}of aMasu.Source. Defaults tonil.repo(:atom) - the repo of an Ecto source. Defaults tonil.repo_opts(:list) - options for each call to the repo. Defaults to[].changeset(:any) - a function of a row and params that gives a changeset: the person edits the cells, see the Editing guide. Defaults tonil.update(:any) - a function of a row and params that saves them:{:ok, row}or{:error, changeset_or_message}; instead ofchangeset. Defaults tonil.create(:any) - a function of params that inserts a new row:{:ok, row}or{:error, changeset_or_message}; the person adds rows, see the Editing guide. Defaults tonil.struct(:any) - withchangeset: the struct of a new row (or a function that gives it), which the changeset casts and the repo inserts; the person adds rows. Defaults tonil.delete(:any) - a function of a row that deletes it::ok,{:ok, row}or{:error, changeset_or_message}; the person deletes rows. Defaults tonil.deletable(:any) -trueto delete rows with the repo of an Ecto source (or from a list), or a function of a row that says whether it deletes. Defaults tonil.restore(:any) - withdelete: a function of a deleted row that brings it back (an undo); a grid that deletes with the repo inserts it again. Defaults tonil.on_delete(:any) - a function called with the rows deleted, in the LiveView's process. Defaults tonil.row_editable(:any) - a function of a row:falsemakes every cell of the row read-only. Defaults tonil.on_save(:any) - a function called with the rows saved by an edit, in the LiveView's process. Defaults tonil.live(:any) -{pubsub, topic}: shows the rows that change on the topic as they change, seeMasu.Live. Defaults tonil.presence(:atom) - your app'sPhoenix.Presencemodule: shows the other people who have the grid open, seeMasu.Presence. Defaults tonil.user(:map) - withpresence: this person,%{id: ..., name: ..., color: ..., avatar: ...}. Defaults tonil.url(:boolean) - whentrue, the person's view (order, filters, search, columns) goes into the URL's params, under the grid's id, and a link opens it; seeMasu.View. Defaults tofalse.views(:atom) - aMasu.Viewsstore: the grid keeps each person's last view, and a views menu saves named views; needsuser. Defaults tonil.views_key(:string) - withviews: the name of the grid in the store; its id unless given. Defaults tonil.columns_menu(:boolean) - whentrue, a columns menu to show, hide, pin and move columns. Defaults tofalse.share_view(:boolean) - withpresence: whether the person's order, filters and search go to the others, who see them when they follow the person. Defaults totrue.selectable(:boolean) - rows with a box to select them, and every row of the query at once; seeMasu.Live.selected/1. Defaults tofalse.on_select(:any) - a function called with%{count: n, all: boolean}when the selection changes. Defaults tonil.export(:any) - an Export menu:truefor CSV and XLSX, or a list of:csvand:xlsx;false(the default) has none, and the server exports nothing; seeMasu.Export. Defaults tofalse.cell_menu(:any) - a menu on each cell (a right click, or the context-menu key or Shift+F10 on the active cell):truefor its built-in items, or a list of:copy,:filterand:sort;false(the default) has only the:cell_actionitems, if any; seeMasu.Action. Defaults tofalse.group_by(:any) - the keys of the columns the rows start grouped by, the first the outermost; seeMasu.Group. Defaults to[].tree(:any) - rows in a tree: the field of a row that holds its parent's id (or a function of the row); seeMasu.Tree. Defaults tonil.expanded(:list) - withtree: the ids of the rows that start open. Defaults to[].count(:atom) - how the grid counts its rows::exactwith the query,:deferredafter the first rows (in a task),:noneonly when the person goes to the end; see the Export, selection and large tables guide. Defaults to:exact. Must be one of:exact,:deferred, or:none.paging(:atom) -:pagesshows a page of rows at a time, with a pager below the grid, and the page in the view. Defaults to:scroll. Must be one of:scroll, or:pages.page_size(:integer) - withpaging={:pages}: the rows of a page, read from the source at once (at mostmax_block). Defaults to50.block_size(:integer) - the rows the grid reads from its source at once as the person scrolls (at mostmax_block). Defaults to100.max_block(:integer) - the most rows of one read from the source: higher for a source where a read is costly and rows are small; see the Export, selection and large tables guide. Defaults to500.load(:atom) - how the grid reads its source::inline(the default) in the event,:asyncin a task, for a slow source: the LiveView answers meanwhile, the grid says it runs, with Stop; see the Sources guide. Defaults to:inline. Must be one of:inline, or:async.timeout(:integer) - withload={:async}: the milliseconds a read may take, then it stops and the grid says so. Defaults tonil.on_query(:any) - a controlled grid: the LiveView runs the query. The grid showsrowsas they are, and calls this function with%{query: %Masu.Query{}, page: n, offset: n, limit: n}when the person sorts, filters, searches or goes to a page; see the Sources guide. Defaults tonil.total(:integer) - withon_query: how many rows the LiveView's query has (its rows are a page of them). Defaults tonil.loading(:boolean) - withon_query: the LiveView runs its query; the grid says so, and keeps its rows until the new ones come. Defaults tofalse.multi_sort(:boolean) -falsesorts by one column at a time: Shift adds none. Defaults totrue.row_id(:any) - a function that gives a row's id; theidof the row unless given, or its place in the query for a row without one in a grid that needs no ids.:position: the place in the query, always, for a read-only grid of rows with no id; see the Sources guide. Defaults tonil.sort(:list) - the first order: keys,{key, :asc | :desc}orMasu.Sortstructs. Defaults to[].filters(:list) - the first filters:{key, op, value}orMasu.Filterstructs. Defaults to[].searchable(:boolean) - whentrue, a search box above the grid. Defaults tofalse.label(:string) - the name of the grid for a screen reader. Defaults tonil.label_id(:string) - the id of an element that names the grid. Defaults tonil.density(:atom) - the room around a cell's text: rows of 28, 36 or 48 pixels and their padding, unlessrow_heightis given. Defaults to:normal. Must be one of:compact,:normal, or:comfortable.row_height(:integer) - the height of a row, in pixels; of thedensityunless given. Defaults tonil.height(:string) - the height of the grid, as CSS. Defaults to"32rem".locale(:string) - the locale that formats numbers and dates; the browser's unless given. Defaults tonil.messages(:map) - words that replace the built-in ones (search,empty,rows,loading; the pager'sprevious,next,page,of_pages,range). Defaults to%{}.class(:any) - classes for the grid element. Defaults tonil.- Global attributes are accepted. attributes for the grid element.
Slots
col- a column; without any, the columns arecolumns, or the source's. Accepts attributes:field(:any)value(:any)key(:string)label(:string)type(:atom)width(:integer)min_width(:integer)align(:atom)sortable(:boolean)filterable(:boolean)searchable(:boolean)editable(:any)values(:list)decimals(:integer)hidden(:boolean)pinned(:atom)aggregate(:any)
header_action- an item of each column's header menu: a function, an event to the LiveView, a JS command, or a panel of its content; seeMasu.Action. Accepts attributes:label(:string) (required)on_action(:any)event(:string)js(:any)columns(:list)types(:list)show(:any)
cell_action- an item of the menu of a cell, as:header_action; seeMasu.Action. Accepts attributes:label(:string) (required)on_action(:any)event(:string)js(:any)columns(:list)types(:list)show(:any)
empty- what the grid shows when it has no rows.detail- what a row shows when it opens, under it: any HEEx, anotherdata_gridtoo; it gets the row.person- draws a person who has the grid open (:let), seeMasu.Presence; initials in their color unless given.