Skip to content

Table Component

The s-table component is a flexible and customizable Vue 3 component for rendering tables. It supports various features such as data pagination, row selection, filtering, and dynamic column rendering. This component is part of the masc-vue component library.

Introduction

The s-table component simplifies the creation of tables with features like row selection, pagination, and filtering. You can easily display and interact with tabular data by binding it to your data and configuring its behavior through props.

In order to define columns heading and content, use s-table-column

Usage

To use the s-table component, follow these steps:

  1. Import the component and any other necessary components:

    vue
    <script setup>
    import { STable, STableColumn } from "masc-vue";
    </script>
    <script setup>
    import { STable, STableColumn } from "masc-vue";
    </script>
  2. Use the s-table and s-table-column component within your template:

    vue
    <template>
    	<s-table
    		:data="tableData"
    		:meta="tableMeta"
    		:selectable="enableRowSelection"
    		:selected="selectedRows"
    		:onRowSelect="handleRowSelect"
    		:filters="tableFilters"
    		:noDataText="noDataMessage"
    	>
    		<!-- Define table columns and slots here -->
    		<!-- Example: -->
    		<s-table-column field="name" label="Name" />
    		<s-table-column field="email" label="Email" />
    	</s-table>
    </template>
    <template>
    	<s-table
    		:data="tableData"
    		:meta="tableMeta"
    		:selectable="enableRowSelection"
    		:selected="selectedRows"
    		:onRowSelect="handleRowSelect"
    		:filters="tableFilters"
    		:noDataText="noDataMessage"
    	>
    		<!-- Define table columns and slots here -->
    		<!-- Example: -->
    		<s-table-column field="name" label="Name" />
    		<s-table-column field="email" label="Email" />
    	</s-table>
    </template>
  3. Customize the component by binding it to your data and using the provided props to configure its behavior.

  4. The s-table component will render the table based on the provided data and user interactions.

Props

The s-table component accepts the following props:

PropTypeDefaultDescription
dataArray of Objects[]The data to be displayed in the table.
metaObject{}Metadata for controlling pagination.
selectableBooleanfalseEnable row selection in the table.
selectedArray of Objects[]The selected rows in the table.
onRowSelectFunctionnullCallback function when a row is selected.
filtersObject{}Filters to be applied to the table columns.
noDataTextString"There's no data"Text to display when there's no data to show in the table.

Events

The s-table component emits the following events:

Event NameDescription
onPageChangeEmitted when the current page is changed.
onPerPageChangeEmitted when the items per page is changed.
onFilterEmitted when a column filter is applied.
update:selectedEmitted when the selected rows are updated.

Example

Here's an example of using the s-table component:

Table
Name
Email
New User?
Date
Name 1
name_1@example.com
Yes
Dec 5, 2024
Name 2
name_2@example.com
No
Dec 5, 2024
Name 3
name_3@example.com
Yes
Dec 5, 2024
Name 4
name_4@example.com
No
Dec 5, 2024
Name 5
name_5@example.com
Yes
Dec 5, 2024
Name 6
name_6@example.com
No
Dec 5, 2024
Name 7
name_7@example.com
Yes
Dec 5, 2024
Name 8
name_8@example.com
No
Dec 5, 2024
Name 9
name_9@example.com
Yes
Dec 5, 2024
Name 10
name_10@example.com
No
Dec 5, 2024
vue
<template>
	<s-table :data="data" :meta="meta">
		<s-table-column field="name" label="Name" filterable />
		<s-table-column field="email" label="Email" />
		<s-table-column field="new" label="New User?" type="boolean" />
		<s-table-column field="date" label="Date" type="date" class="text-center" />
		<s-table-column v-slot="props">
			<s-btn class="btn-xs w-full" @click="onEdit(props.row)">Edit</s-btn>
		</s-table-column>
	</s-table>
</template>

<script setup lang="ts">
import { ref, computed } from "vue";
import { STable, STableColumn, useNotify } from "masc-vue";

const notify = useNotify();
function onEdit(row: any) {
	notify.alert(`Editing ${row.name}`);
}

type TdataRow = {
	name: string;
	email: string;
	new: boolean;
	date: Date;
};

const data = computed(() => {
	const start = Number(meta.value.page || 1);
	const count = Number(meta.value.perPage || 10);
	const startCount = (start - 1) * count;
	const rows: TdataRow[] = [];
	for (let i = 1; i <= count; i++) {
		const x = startCount + i;
		rows.push({
			name: `Name ${x}`,
			email: `name_${x}@example.com`,
			new: i % 2 ? true : false,
			date: new Date(),
		});
	}
	return rows;
});

const meta = ref({ total: 100, perPage: 10, page: 1 });
</script>
<template>
	<s-table :data="data" :meta="meta">
		<s-table-column field="name" label="Name" filterable />
		<s-table-column field="email" label="Email" />
		<s-table-column field="new" label="New User?" type="boolean" />
		<s-table-column field="date" label="Date" type="date" class="text-center" />
		<s-table-column v-slot="props">
			<s-btn class="btn-xs w-full" @click="onEdit(props.row)">Edit</s-btn>
		</s-table-column>
	</s-table>
</template>

<script setup lang="ts">
import { ref, computed } from "vue";
import { STable, STableColumn, useNotify } from "masc-vue";

const notify = useNotify();
function onEdit(row: any) {
	notify.alert(`Editing ${row.name}`);
}

type TdataRow = {
	name: string;
	email: string;
	new: boolean;
	date: Date;
};

const data = computed(() => {
	const start = Number(meta.value.page || 1);
	const count = Number(meta.value.perPage || 10);
	const startCount = (start - 1) * count;
	const rows: TdataRow[] = [];
	for (let i = 1; i <= count; i++) {
		const x = startCount + i;
		rows.push({
			name: `Name ${x}`,
			email: `name_${x}@example.com`,
			new: i % 2 ? true : false,
			date: new Date(),
		});
	}
	return rows;
});

const meta = ref({ total: 100, perPage: 10, page: 1 });
</script>

In this example, we've used the s-table component to create a table with custom columns and row selection.

Table Column Component

The s-table-column component is used in conjunction with the s-table component to define custom columns within a table. It allows you to specify the content and formatting of individual table cells.

The s-table-column component is used to define the content and formatting of a column in a table. You can customize the display value, data source, and various other attributes for each column to tailor the appearance and behavior of your table.

Table Column Props

The s-table-column component accepts the following props:

PropTypeDefaultDescription
fieldString""The field name from the row data.
displayString""The display field name to use (if different).
typeString"text"The type of content (e.g., "text", "date", "boolean").
btnTextString""Text for the button (if used).
onClickFunctionnullCallback function for button click.
toString""URL to navigate when the button is clicked.
filterableString, Boolean, Number, ArraynullFilterable options for the column.
optionsArray of Objects[]Options for a select input (if used).

Released under the MIT License.