Features
- Flexible Templating: Customize the appearance of columns and cells.
- Action Bindings: Define actions such as edit, delete, or send email.
- Dynamic Data Rendering: Supports array or JSON input.
Expected Behavior
Data Handling
- Data provided to the grid will be displayed as text without modification.
- Custom renderers can be used to format displayed data.
- Updates to the dataset should reflect in real-time.
| Case | Input Data | Display in Grid | Output on Selection |
|---|---|---|---|
| Standard Row Data | { id: 1, name: 'John' } | Displays 'John' in Name col. | { id: 1, name: 'John' } |
Column Display
Currently, all column data is rendered as text. Custom cell renderers can be used to achieve different visual presentations.
Repeat Template
The repeatTemplate property allows the rendering of multiple elements inside a single cell when the data is structured as an array. This is useful for displaying lists, tags, or grouped information.
Usage
To enable repeatTemplate, add it as an attribute on an nv-datagridcolumn. The grid will iterate over array elements and render them accordingly.
Example
<nv-datagrid data-json='[
{"id":1,"name":"John Doe","roles":[{"id":"1","name":"Admin"},{"id":"2","name":"User"}]},
{"id":2,"name":"Jane Doe","roles":[{"id":"2","name":"User"}]}
]'>
<nv-datagridcolumn accessor="name" header="Name">
<div slot="cell">
<strong>__name__</strong>
</div>
</nv-datagridcolumn>
<nv-datagridcolumn accessor="roles" header="Role(s)" repeat-template>
<div slot="cell">
<nv-badge label="__roles.name__"></nv-badge>
</div>
</nv-datagridcolumn>
</nv-datagrid>
Behavior
- If the
rolesaccessor contains an array, each element will be rendered inside the same cell. - The
repeatTemplateensures proper iteration over array values. - This is especially useful for rendering multiple tags, badges, or nested items.
Notes
- The grid does not modify original data, ensuring consistency.
- Custom cell renderers allow advanced formatting and interactivity.
Action Binding
To enable actions within the grid, follow these steps:
- Add the
data-bind-eventattribute to interactive elements inside the grid. - Use the structured format for the attribute value:
actionEventToTrigger:actionNameToTrigger:propsNames.- Example:
click:sendEmail:id,emailclick→ Event typesendEmail→ Action to triggerid,email→ Properties to pass
- Example:
- Define an
actionattribute on the<nv-datagrid>component, assigning it a function that receives anActionEventobject.
ActionEvent Object Structure
interface ActionEvent {
/**
* Key action to be performed
*/
keyAction: string;
/**
* Details of the action to be performed
*/
details: any;
}
Example Usage
<nv-datagrid
data-json="[...]"
action="handleGridAction">
<nv-datagridcolumn accessor="actions">
<div slot="cell">
<nv-iconbutton size="xs" name="pencil" data-bind-event="click:edit:id"></nv-iconbutton>
<nv-iconbutton size="xs" name="trash" data-bind-event="click:delete:id"></nv-iconbutton>
<nv-iconbutton size="xs" name="mail" data-bind-event="click:sendEmail:id,email"></nv-iconbutton>
</div>
</nv-datagridcolumn>
</nv-datagrid>
Usage Examples
Using JSON Data
<nv-datagrid data-json="[
{"id":1,"name":"John Doe","age":25,"email":"john.doe@mail.com", "address": {"city": "New York", "country": "USA"}, "status": {"label": "success", "color": "1", "icon": "check"}},
{"id":2,"name":"Jane Doe","age":30,"email":"jane.doe@mail.com", "address": {"city": "Los Angeles", "country": "USA"}, "status": {"label": "warning", "color": "2", "icon": "alert-circle"}}
]">
<nv-datagridcolumn accessor="name" header="Name">
<div slot="header">
<strong>Name</strong>
</div>
<div slot="cell">
<strong>__name__</strong>
</div>
</nv-datagridcolumn>
<nv-datagridcolumn accessor="age" header="Age">
<div slot="header">
<em>Age</em>
</div>
<div slot="cell">
<span>__age__ years old</span>
</div>
</nv-datagridcolumn>
<nv-datagridcolumn accessor="email" header="Mail Address">
<div slot="cell">
<a href="mailto:__email__">__email__</a>
</div>
</nv-datagridcolumn>
<nv-datagridcolumn accessor="address" header="Address">
<div slot="cell">
<span>__address.city__, __address.country__</span>
</div>
</nv-datagridcolumn>
<nv-datagridcolumn accessor="status" header="Status">
<div slot="header">
<span class="text-md font-bold text-low">Status</span>
</div>
<div slot="cell">
<nv-badge color="__status.color__" lead-icon="__status.icon__">
__status.label__
</nv-badge>
</div>
</nv-datagridcolumn>
<nv-datagridcolumn accessor="actions">
<div slot="cell">
<nv-iconbutton size="xs" name="pencil" data-bind-event="click:edit:id"></nv-iconbutton>
<nv-iconbutton size="xs" name="trash" data-bind-event="click:delete:id"></nv-iconbutton>
<nv-iconbutton size="xs" name="mail" data-bind-event="click:sendEmail:id,email"></nv-iconbutton>
</div>
</nv-datagridcolumn>
</nv-datagrid>
Summary
- The component does not alter input data unless explicitly edited.
- Selection and editing emit corresponding events to track changes.
- The grid offers full customization for display and behavior.