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

122 lines
3.2 KiB
Vue
Raw Normal View History

2016-10-26 03:58:35 +00:00
<template>
2016-10-27 23:46:13 +00:00
<tr class="md-table-row" :class="classes" @click="autoSelect">
2016-11-14 20:53:06 +00:00
<md-table-cell class="md-table-selection" v-if="hasSelection">
<md-checkbox v-model="checkbox" :disabled="isDisabled" @change="select"></md-checkbox>
2016-10-27 05:52:58 +00:00
</md-table-cell>
2016-10-26 03:58:35 +00:00
<slot></slot>
</tr>
</template>
<script>
2016-11-14 20:53:06 +00:00
import getClosestVueParent from '../../core/utils/getClosestVueParent';
const transitionClass = 'md-transition-off';
2016-10-26 03:58:35 +00:00
export default {
props: {
2016-11-14 20:53:06 +00:00
mdAutoSelect: Boolean,
mdSelection: Boolean,
mdItem: Object
},
2016-10-26 03:58:35 +00:00
data() {
return {
2016-11-14 20:53:06 +00:00
parentTable: {},
headRow: false,
2016-10-26 03:58:35 +00:00
checkbox: false,
index: 0
};
},
computed: {
2016-11-14 20:53:06 +00:00
isDisabled() {
return !this.mdSelection && !this.headRow;
},
hasSelection() {
return this.mdSelection || this.headRow && this.parentTable.hasRowSelection;
},
2016-10-26 03:58:35 +00:00
classes() {
return {
'md-selected': this.checkbox
};
}
},
methods: {
setSelectedRow(value, index) {
if (value) {
2016-11-14 20:53:06 +00:00
this.parentTable.selectedRows[index] = this.parentTable.data[index];
++this.parentTable.numberOfSelected;
} else {
2016-11-14 20:53:06 +00:00
delete this.parentTable.selectedRows[index];
--this.parentTable.numberOfSelected;
2016-10-26 03:58:35 +00:00
}
},
handleSingleSelection(value) {
2016-11-14 20:53:06 +00:00
this.setSelectedRow(value, this.index - 1);
this.parentTable.$children[0].checkbox = this.parentTable.numberOfSelected === this.parentTable.numberOfRows;
2016-10-26 03:58:35 +00:00
},
handleMultipleSelection(value) {
2016-11-14 20:53:06 +00:00
if (this.parentTable.numberOfRows > 25) {
this.parentTable.$el.classList.add(transitionClass);
2016-10-27 05:52:58 +00:00
}
2016-11-14 20:53:06 +00:00
this.parentTable.$children.forEach((row, index) => {
2016-10-26 03:58:35 +00:00
row.checkbox = value;
2016-11-14 20:53:06 +00:00
if (!row.headRow) {
this.setSelectedRow(value, index - 1);
}
2016-10-26 03:58:35 +00:00
});
if (value) {
2016-11-14 20:53:06 +00:00
this.parentTable.numberOfSelected = this.parentTable.numberOfRows;
} else {
2016-11-14 20:53:06 +00:00
this.parentTable.numberOfSelected = 0;
}
2016-11-14 20:53:06 +00:00
window.setTimeout(() => this.parentTable.$el.classList.remove(transitionClass));
2016-10-26 03:58:35 +00:00
},
select(value) {
2016-11-14 20:53:06 +00:00
if (this.hasSelection) {
2016-10-26 03:58:35 +00:00
if (this.headRow) {
this.handleMultipleSelection(value);
} else {
this.handleSingleSelection(value);
}
2016-11-14 20:53:06 +00:00
this.parentTable.emitSelection();
2016-10-26 03:58:35 +00:00
}
},
autoSelect() {
2016-11-14 20:53:06 +00:00
if (this.mdAutoSelect && this.hasSelection) {
this.checkbox = !this.checkbox;
this.handleSingleSelection(this.checkbox);
2016-11-14 20:53:06 +00:00
this.parentTable.emitSelection();
}
2016-10-26 03:58:35 +00:00
}
},
2016-11-14 20:53:06 +00:00
watch: {
data() {
this.parentTable.data[this.index] = this.item;
}
},
2016-10-26 03:58:35 +00:00
mounted() {
2016-11-14 20:53:06 +00:00
this.parentTable = getClosestVueParent(this.$parent, 'md-table');
2016-10-26 03:58:35 +00:00
if (this.$el.parentNode.tagName.toLowerCase() === 'thead') {
this.headRow = true;
} else {
2016-11-14 20:53:06 +00:00
this.parentTable.numberOfRows++;
this.index = this.parentTable.numberOfRows;
if (this.mdSelection) {
this.parentTable.hasRowSelection = true;
}
if (this.mdItem) {
this.parentTable.data.push(this.mdItem);
}
2016-10-26 03:58:35 +00:00
}
}
};
</script>