c3lf-system-3/src/components/Table.vue

59 lines
1.5 KiB
Vue
Raw Normal View History

2019-11-14 03:37:35 +00:00
<template>
<table class="table table-striped table-dark">
<thead>
2019-11-14 18:30:00 +00:00
<tr>
<th scope="col" v-for="(column, index) in columns" :key="index">
<button class="btn text-light" v-on:click="toggleSort(column)">
{{ column }}
<span :class="{ 'text-info': column === sortBy }">
<font-awesome-icon :icon="sortIcon(column)"/>
</span>
</button>
</th>
</tr>
2019-11-14 03:37:35 +00:00
</thead>
2019-11-14 18:30:00 +00:00
<tbody>
<tr v-for="item in internalItems" :key="item[keyName]">
<td v-for="(column, index) in columns" :key="index">{{ item[column] }}</td>
</tr>
</tbody>
2019-11-14 03:37:35 +00:00
</table>
</template>
<script>
2019-11-14 18:30:00 +00:00
import * as R from 'ramda';
2019-11-14 03:37:35 +00:00
export default {
name: 'Table',
2019-11-14 18:30:00 +00:00
props: ['columns', 'items', 'keyName'],
data: (self) => ({
sortBy: self.keyName,
ascend: true
}),
computed: {
internalItems() {
const sortByOrd = R.sortBy(R.prop(this.sortBy));
const sorted = sortByOrd(this.items, [this.sortBy]);
return this.ascend ? sorted : R.reverse(sorted);
}
},
methods: {
sortIcon(column) {
if (column !== this.sortBy) return 'sort';
if (this.ascend) return 'sort-up';
return 'sort-down';
},
toggleSort(column) {
if (column === this.sortBy)
this.ascend = !this.ascend;
this.sortBy = column;
}
}
2019-11-14 03:37:35 +00:00
};
2019-11-14 18:30:00 +00:00
</script>
<style>
.table-body-move {
transition: transform 1s;
}
</style>