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;grow-truetakes the room the columns leave in a wider grid, from its width up (a title, a description); a number is its share of it,grow={2}twice that ofgrow;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;filters- theMasu.Filteroperators the column offers, of its type: those the app runs, in a controlled grid (on_query).editor- an editor of your own for its cells: a function component of a form field (a Koyomi picker, a Kotoba editor, any HEEx), or the name of an editor of the browser's; see the Editors of your own guide.
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>Writes and actions
A grid with a changeset edits its cells, and with struct and
deletable adds and deletes rows, its own way; on_update,
on_create, on_delete and on_restore write them your way, each a
function of a Masu.Write; see the Editing guide.
history keeps who changed each cell, when, and from what, and brings
a value back; see Masu.History and the History guide.
:action acts on the rows selected, and :row_action on a row; see
Masu.Action and the Actions guide. on_row_activate
opens a row (a panel beside the grid, its page) on a click on it or
Enter. scope is in every write and every action.
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), up to about 10,000; past that, an Ectosource(see the Sources guide). 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[].read(:any) - an Ecto source's read of a block of rows, your own: a function of the query (with the grid's filters, search and order) and%{offset:, limit:, order:, after:}, which gives the rows; see the Sources guide. Defaults tonil.changeset(:any) - a function of a row and params that gives a changeset: the person edits the cells, which the grid saves its own way (Masu.Write.save/1), 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.create_with(:any) - fields every new row gets, as a map or keyword list (%{account_id: @current_scope.account.id}): put onstructbefore the changeset and again after it, so params cannot change them; the person neither sets nor edits a cell of these fields. Defaults tonil.on_update(:any) - a function of aMasu.Writeof edited cells, which saves them and gives the write back with their results; the person edits the cells, seeMasu.Write. Defaults tonil.on_create(:any) - a function of aMasu.Writeof new rows, ason_update; the person adds rows. Defaults tonil.on_delete(:any) - a function of aMasu.Writeof rows to delete, ason_update; the person deletes rows. Defaults tonil.on_restore(:any) - a function of aMasu.Writeof deleted rows to bring back (an undo), ason_update; a grid that deletes its own way brings them back its own way. Defaults tonil.deletable(:any) -trueto delete rows (the grid's own way: with the repo of an Ecto source, or from a list; or withon_delete),falsenever, or a function of a row that says whether it deletes. Defaults tonil.row_editable(:any) - a function of a row:falsemakes every cell of the row read-only. Defaults tonil.history(:atom) - aMasu.Historystore: the grid records who changed each cell, when, from what to what, and a person sees a cell's history and brings a value back;usersays who. Defaults tonil.scope(:any) - what your app knows of the person (@current_scope): everyMasu.Writeand every action's context has it. Defaults tonil.live(:any) -{pubsub, topic}: shows the rows that change on the topic as they change, seeMasu.Live. Defaults tonil.presence(:any) - removed in 0.4: a grid's people are those of an Issho room,issho; see the Upgrading guide. Defaults tonil.issho(:any) - the grid's people, in an Issho room:[instance: MyApp.Issho, document: "notebook:42/tasks"], with the keys of every component (Issho.Join): the grid'sdocument(its name, the same wherever it is: its page, a document; its room is the part before the first"/"),can,people(falsehides the avatars above the grid, on a page that lists its people itself),follow(falsefor a grid that takes no part in following),claimsandon_behalf_of; and Masu's ownactor(the person as the room knows them;userunless given). Agents in the room are among them; see the Collaboration guide. Defaults tonil.people(:any) - removed in 0.5:issho's:people. Defaults tonil.follow(:any) - removed in 0.5:issho's:follow. Defaults tonil.user(:map) - withpresenceorissho: 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.views_scope(:any) - withviews: whose views these are, such as the account's id; the store keeps each scope's views apart, so a view shared in one is not offered in another. Defaults tonil.columns_menu(:boolean) - whentrue, a columns menu to show, hide, pin and move columns. Defaults tofalse.share_view(:boolean) - withissho: 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.on_row_activate(:any) - a function called with%{id: id, row: row, grid: id, scope: scope}when the person opens a row: a click on it, or Enter on its active cell, where the cell does not edit. It gives{:patch, path}or{:navigate, path}for the grid to follow, or anything else; see the Actions guide. 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(:any) - the rows that start open: withtree, the ids of the rows; with groups,:allfor every group at every level, or the keys of the columns whose groups start open ([:status]); seeMasu.Group. 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.count_limit(:integer) - the most rows an Ecto source counts: past it, the grid says it has at least that many ("10,000+"), and counts them all when the person goes to the end; see the Export, selection and large tables guide. Defaults tonil.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.filter_bar(:boolean) - the filter bar: the chips of the filters and Add filter above the grid, and on each header a menu of its sorts and filter;falsefor the leaner header, a funnel on each header that filters; see the Filters guide. Defaults totrue.header_filters(:boolean) - whether a header's menu has its column's filter;falsewith the filter bar: the filters are the bar's alone, and a header's menu has its sorts; see the Filters guide. 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 whole grid, as CSS (100%fills its container); the rows take what its toolbar leaves;"auto": the height of its rows, up tomax_height(a grid in a document). Defaults to"32rem".max_height(:string) - withheight="auto": the most the rows take, as CSS; past it, they scroll. Defaults to"32rem".wheel(:atom) -:pagefor a grid in a page that scrolls (a document): the wheel scrolls the page until the person is in the grid (a click, the keyboard), and the grid then. Defaults to:grid. Must be one of:grid, or:page.view(:map) - the view the grid opens with: a view's params (Masu.View), such as a saved view's (Masu.Views); a link's view (url) wins over it, and it over the person's last view. Defaults tonil.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, by key (search,empty,rows...):Masu.Messageshas every key, for a grid in another language. 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)grow(:any)align(:atom)sortable(:boolean)filterable(:boolean)searchable(:boolean)editable(:any)values(:list)decimals(:integer)raw(:boolean)hidden(:boolean)pinned(:atom)aggregate(:any)filters(:list)editor(: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)
action- an action on the rows selected, a button of the bar above the grid while rows are selected; needsselectable; seeMasu.Action. Accepts attributes:label(:string) (required)on_action(:any)event(:string)confirm(:string)danger(:boolean)keep_selection(:boolean)
row_action- an action on a row, in the menu at the end of each row and in the menu of its cells: a function, an event, or a link (navigate,patch); seeMasu.Action. Accepts attributes:label(:string) (required)on_action(:any)event(:string)navigate(:any)patch(:any)show(:any)disabled(:any)confirm(:string)danger(:boolean)
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), see the Collaboration guide; initials in their color unless given.