vue-material/src/components/mdTable/mdTableHead.vue

76 lines
1.8 KiB
Vue
Raw Normal View History

2016-10-27 05:52:58 +00:00
<template>
<th class="md-table-head" :class="classes" @click="changeSort">
<div class="md-table-head-container" v-md-ink-ripple="!mdSortBy">
2016-10-27 13:46:40 +00:00
<div class="md-table-head-text md-test">
<md-icon class="md-sortable-icon" v-if="mdSortBy">arrow_downward</md-icon>
2016-10-27 05:52:58 +00:00
<slot></slot>
<md-tooltip v-if="mdTooltip">{{ mdTooltip }}</md-tooltip>
2016-10-27 13:46:40 +00:00
</div>
2016-10-27 05:52:58 +00:00
</div>
</th>
</template>
<script>
2016-11-14 20:53:06 +00:00
import getClosestVueParent from '../../core/utils/getClosestVueParent';
2016-10-27 05:52:58 +00:00
export default {
props: {
mdNumeric: Boolean,
mdSortBy: String,
mdTooltip: String
2016-10-27 05:52:58 +00:00
},
data() {
return {
sortType: null,
2016-11-14 20:53:06 +00:00
sorted: false,
parentTable: {}
2016-10-27 05:52:58 +00:00
};
},
computed: {
classes() {
2016-11-14 20:53:06 +00:00
const matchSort = this.hasMatchSort();
2016-10-27 05:52:58 +00:00
if (!matchSort) {
this.sorted = false;
}
return {
'md-numeric': this.mdNumeric,
2016-10-27 05:52:58 +00:00
'md-sortable': this.mdSortBy,
'md-sorted': matchSort && this.sorted,
'md-sorted-descending': matchSort && this.sortType === 'desc'
};
}
},
methods: {
2016-11-14 20:53:06 +00:00
hasMatchSort() {
return this.parentTable.sortBy === this.mdSortBy;
},
2016-10-27 05:52:58 +00:00
changeSort() {
2016-11-14 20:53:06 +00:00
if (this.mdSortBy) {
if (this.sortType === 'asc' && this.sorted) {
this.sortType = 'desc';
} else {
this.sortType = 'asc';
}
this.sorted = true;
2016-10-27 05:52:58 +00:00
2016-11-14 20:53:06 +00:00
this.parentTable.sortType = this.sortType;
this.parentTable.emitSort(this.mdSortBy);
2016-10-27 05:52:58 +00:00
}
2016-11-14 20:53:06 +00:00
}
},
mounted() {
this.parentTable = getClosestVueParent(this.$parent, 'md-table');
2016-10-27 05:52:58 +00:00
2016-11-14 20:53:06 +00:00
if (this.hasMatchSort()) {
2016-10-27 05:52:58 +00:00
this.sorted = true;
2016-11-14 20:53:06 +00:00
this.sortType = this.parentTable.sortType;
2016-10-27 05:52:58 +00:00
}
}
};
</script>