<template>
<div class="row">
<div class="col-md-4">
<div class="list-group">
<devgroup v-for="devgroup in devgroups" v-on:update="onDevgroupClick"></devgroup>
</div>
</div>
...
<div class="col-md-8" v-if="selectedDevice.id > 0">
<form class="form-horizontal" v-if="activeTab == 0">
<div class="form-group">
<label for="inputImei" class="col-sm-4 control-label">IMEI:</label>
<div class="col-sm-8">
<p class="form-control-static">{{selectedDevice.imei}}</p>
</div>
</div>
<div class="form-group">
<label for="inputName" class="col-sm-4 control-label">Наименование:</label>
<div class="col-sm-8">
<input type="text" class="form-control" v-model="selectedDevice.name">
</div>
</div>
<div class="form-group">
<label for="inputGroup" class="col-sm-4 control-label">Группа:</label>
<div class="col-sm-8">
<select class="form-control" v-model="selectedDevice.group_id">
<option v-for="devgroup in devgroups" v-bind:value="devgroup.id">{{devgroup.name}}</option>
</select>
</div>
</div>
...
<div class="form-group">
<div class="col-md-offset-8 col-md-4 text-right">
<button type="button" class="btn btn-success" v-bind:class="{disabled: !hasChanges}" v-on:click="saveDevice">Сохранить</button>
</div>
</div>
</form>
</div>
...
</template>
<script>
export default {
data() {
return {
hasChanges: false,
selectedDevice: {
id: 0,
name: '',
sensors: []
},
...
}
},
methods: {
onDevgroupClick(item) {
// Clone device object(for editing)
this.selectedDevice = Object.assign({}, item);
this.hasChanges = false;
},
...
},
watch : {
selectedDevice: function() {
this.hasChanges = true;
}
},
...
}
</script>
this.$nextTick(function () {
this.hasChanges = false;
})
...
methods: {
onDevgroupClick(item) {
// Clone device object(for editing)
this.selectedDevice = Object.assign({}, item);
this.hasChanges = false;
this.$nextTick(() => {
this.$watch('selectedDevice', this.hasChanged, { deep: true })
})
},
hasChanged() {
this.hasChanges = true;
}
}
<form @keydown="hasChanged=true">
<input type="text"/>
</form>
<button class="btn btn-primary" :class="{disabled: !hasChanged}"
@click="hasChanged=false">
Click
</button>