DataTable.vue 1.84 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
<template>
<div class="table-container">
<b-row>
<b-col v-if="lengthChange" md="4" class="mb-3">
<b-form inline>
<label class="mr-2">Show</label>
<b-form-select v-model="perPage" :options="pageOptions" class="mr-2" @input="onContextChanged" />
<label>entries per page</label>
</b-form>
</b-col>
</b-row>
<slot />
<b-row>
<b-col md="4" class="mx-auto">
<b-pagination v-if="paging && totalRows > perPage" v-model="currentPage" :total-rows="totalRows" :per-page="perPage"
class="justify-content-center" @input="onContextChanged"
/>
</b-col>
</b-row>
</div>
</template>

<script>
import axios from 'axios'

export default {
name: 'Datatable',
props: {
lengthChange: {
type: Boolean,
default: true
},
paging: {
type: Boolean,
default: true
},
infos: {
type: Boolean,
default: true
},
searchRoute: {
type: String,
default: null
}
},
data () {
return {
currentPage: 1,
perPage: 10,
totalRows: 0,
pageOptions: [5, 10, 15, 25, 50],
searchQuery: null,
selected: [],
action: null
}
},
mounted () {
if (this.actions) {
this.action = Object.keys(this.actions)[0]
}
},
methods: {
onContextChanged () {
this.$emit('context-changed')
},
async loadData (sortBy, sortDesc) {
try {
const { data } = await axios.get(this.searchRoute, {
params: {
page: this.currentPage,
perPage: this.perPage,
column: sortBy,
direction: sortDesc ? 'desc' : 'asc'
}
})
this.totalRows = data.total
return data.data
} catch (e) {
console.error(e)
return []
}
}
}
}
</script>