I have a table that was generated dynamically using Vuejs. It has input elements in cells that render as readOnly. Each element has an 'edit' button and when it is click, it changes to 'save' and enables the input elements of that row for editing. When save is clicked, I would like to capture the new value entered and the previous value. I formatted my date field as mm/dd/yy from the original which is in this format 2019-10-10T07:00:00Z I am able to successfully format the date using momentjs but it doesn't seem to stick. The value entered is different from when I alert it. Any ideas of what I'm doing wrong? Do I need to refactor my code to allow me to do this because this has to be done for each field, which is to get access to new value and previous value.
<div id="app">
<table border=1 width=100%>
<tr>
<td>EDIT</td>
<td v-for="editableKey in editableKeys" :key="editableKey" class="label">{{ editableKey }}</td>
</tr>
<tr v-for="(program, index) in programs">
<td><button v-if="program.isReadOnly" v-on:click="editItem(program)">edit</button> <button @click="saveItem(program)" v-else>save</button></td>
<td><input type="text" v-bind:data-id="program.id" :readonly="program.isReadOnly" v-model="formatDate(program)"></td>
<td><input type="text" v-bind:data-id="program.id" :readonly="program.isReadOnly" v-model="program.company"></td>
<td><input type="text" v-bind:data-id="program.id" :readonly="program.isReadOnly" v-model="program.funding"></td>
<td><input type="text" v-bind:data-id="program.id" :readonly="program.isReadOnly" v-model="program.funded"></td>
<td><select :class="bgColor(program)" type="text" v-bind:data-id="program.id" :disabled="program.isReadOnly" v-model="program.Recruit">
<option>Internal</option>
<option>Recruiting</option>
</select>
<!--<input :class="bgColor(program)" type="text" v-bind:data-id="program.id" :readonly="program.isReadOnly" v-model="program.Recruit">--></td>
<td><input type="text" v-on:change="" v-bind:data-id="program.id" :readonly="program.isReadOnly" v-model="program.program"></td>
</tr>
</table>
</div>
new Vue({
el:"#app",
data: () => ({
programs: [],
editableKeys: ['date', 'company', 'funding', 'funded', 'recruit', 'program'],
}),
created () {
this.getPrograms();
},
methods: {
getPrograms() {
axios.get("https://my-json-server.typicode.com/isogunro/jsondb/Programs").then(response => {
this.programs = response.data.map(program => ({
...program,
isReadOnly: true
}));
}).catch(error => {
console.log(error);
});
},
editItem (program) {
program.isReadOnly = false
},
saveItem (program) {
program.isReadOnly = true
console.log(program)
alert("New Value: "+program.Date)
alert("Previous Value: "+program.Date)
},
bgColor (program) {
return program.funded === program.funding ? 'yellow' : 'white'
},
formatDate(program){
var formatL = moment.localeData().longDateFormat('L');
var format2digitYear = formatL.replace(/YYYY/g,'YY');
return moment(program.Date).format(format2digitYear);
},
updateField(program){
console.log(program)
alert(program)
}
}
})
Here's a pen for some clarity. Thanks for any help that can be provided.