Vue-table is a data table component that allows the developer a high degree of customization, it takes care of complex tasks and gives the developer control over how the data is displayed. This component works with the Laravel framework pagination response.
npm i @barbosa89/vue-table
The component styles are based on the Bootstrap 4 classes, so your project must work with that CSS framework. Vue-table is structured in three rows (.row), so you must nest the component inside a Bootstrap container (.container/.container-fluid).
Vue-table does not display the data sent by the server directly, this passes each record to a slot, the developer uses the slot to determine the way the data is displayed.
The component can be configured with four props:
<template>
<div class="container">
<h1 class="text-center my-4">Vue Table component</h1>
<vue-table :headers="headers" :url="url">
<template v-slot:record="{ record }">
<td>
<a href="#">
{{ record.id }}
</a>
</td>
<td>
<a href="#">
{{ record.first_name }}
</a>
</td>
<td>
<a href="#">
{{ record.last_name }}
</a>
</td>
<td>
{{ record.email }}
</td>
</template>
</vue-table>
</div>
</template>
<script>
import VueTable from 'VueTable'
export default {
data() {
return {
headers: [
{
description: 'Id',
field: 'id', # Sortable column
},
{
description: 'Name',
field: 'name', # Sortable column
},
{
description: 'Last',
field: 'last', # Sortable column
},
{
description: 'Email' # No sortable column
},
],
url: 'https://myserver.app/api/users?page=1',
// Optional, You will need to pass the props
lang: 'en',
locales: {
en:{
display: 'Records per page',
search: 'Search',
record: 'Record',
of: 'of',
total: 'Total'
},
es:{
display: 'Registros por página',
search: 'Buscar',
record: 'Registro',
of: 'de',
total: 'Total'
}
}
};
},
components: {
VueTable
}
}
</script>
With packages like vue-i18n, you can automate multiple languages:
lang: document.documentElement.lang,
locales: {
en:{
display: $t('display'),
search: $t('search'),
record: $t('record'),
of: $t('of'),
total: $t('total')
},
es:{
display: $t('display'),
search: $t('search'),
record: $t('record'),
of: $t('of'),
total: $t('total')
}
}
Vue-table sends Laravel a series of parameters namely:
URL example:
mylaravel.app/endpoint?page=1&per_page=15&query_by_=text&ordered_desc=column_name
In an example of implementation in Laravel 7:
public function index()
{
$posts = Post::query();
foreach(request()->toArray() as $filter => $value) {
$filter = Str::camel($filter);
if($posts->hasNamedScope($filter)) {
$posts->{$filter}($value);
}
}
return $posts->paginate(request->get('per_page', 15));
}
You need to create three scopes corresponding to each parameter, excluding per_page and page, which are passed to Laravel directly:
// Eloquent model
public function scopeQueryBy($query, $value)
{
return $query->Where('name', 'like', '%' . $value . '%');
}
public function scopeOrderedDesc($query, $value)
{
return $query->orderBy($value, 'DESC');
}
public function scopeOrderedAsc($query, $value)
{
return $query->orderBy($value, 'ASC');
}
npm install -g @vue/cli
# OR
yarn global add @vue/cli
You will need an add-on service for Vue CLI
npm install -g @vue/cli-service-global
Run local server
npm run dev
Run production mode
npm run build
Author: barbosa89
Source Code: https://github.com/barbosa89/vue-table
#vuejs #vue #javascript #vue-js