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

81 lines
2.2 KiB
Vue
Raw Normal View History

2016-10-26 03:58:35 +00:00
<template>
<tr class="md-table-row" :class="classes">
2016-10-27 05:52:58 +00:00
<md-table-cell class="md-table-selection" v-if="$parent.mdRowSelection">
2016-10-26 03:58:35 +00:00
<md-checkbox v-model="checkbox" @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>
const transitionClass = 'md-transition-off';
2016-10-26 03:58:35 +00:00
export default {
data() {
return {
checkbox: false,
index: 0
};
},
computed: {
classes() {
return {
'md-selected': this.checkbox
};
}
},
methods: {
setSelectedRow(value, index) {
if (value) {
this.$parent.selectedRows[index] = value;
++this.$parent.numberOfSelected;
} else {
delete this.$parent.selectedRows[index];
--this.$parent.numberOfSelected;
2016-10-26 03:58:35 +00:00
}
},
handleSingleSelection(value) {
this.setSelectedRow(value, this.index);
2016-10-27 05:52:58 +00:00
this.$parent.$children[0].checkbox = this.$parent.numberOfSelected === this.$parent.numberOfRows;
2016-10-26 03:58:35 +00:00
},
handleMultipleSelection(value) {
2016-10-27 05:52:58 +00:00
if (this.$parent.numberOfRows > 25) {
this.$parent.$el.classList.add(transitionClass);
}
this.$parent.$children.forEach((row) => {
2016-10-26 03:58:35 +00:00
row.checkbox = value;
});
if (value) {
/*this.$parent.selectedRows = {}; //and so on, this can be lazly created the first time or on component boot*/
this.$parent.numberOfSelected = this.$parent.numberOfRows;
} else {
/*this.$parent.selectedRows = {};*/
this.$parent.numberOfSelected = 0;
}
window.setTimeout(() => this.$parent.$el.classList.remove(transitionClass));
2016-10-26 03:58:35 +00:00
},
select(value) {
if (this.$parent.mdRowSelection) {
if (this.headRow) {
this.handleMultipleSelection(value);
} else {
this.handleSingleSelection(value);
}
}
}
},
mounted() {
if (this.$el.parentNode.tagName.toLowerCase() === 'thead') {
this.headRow = true;
} else {
this.$parent.numberOfRows++;
this.index = this.$parent.numberOfRows;
}
}
};
</script>