2
Data should be a function which returns an object and not just an object like:
data: function() {
return {
todos: [
newData
]
}
}
Also finditem()
should be a computed property like:
computed: {
finditem: function() {
var setarray = [];
var result = Object.keys(newData).map(function (key) {
return [key, newData[key]];
});
var travelModeId = '';
$(result).each(function (i, value) {
var getValue = value[0];
// No Input
if (getValue == "Reservation" || getValue == "LastUpdatedBy" || getValue == "LastUpdated") {
var first = value[0];
var second = value[1];
var htmlId = 0;
setarray.push({ item: first, result: second, html: htmlId });
}
// Text Input
if (getValue == "Fare" || getValue == "AgentFee" || getValue == "ChangeFee" || getValue == "NteAmount" || getValue == "GsaRate" || getValue == "ClientApprovedAmount" || getValue == "CancelledDate") {
var first = value[0];
var second = value[1];
var htmlId = 1;
setarray.push({ item: first, result: second, html: htmlId });
}
// Checkbox
if (getValue == "Ground" || getValue == "NoGsa") {
var first = value[0];
var second = value[1];
var htmlId = 2;
setarray.push({ item: first, result: second, html: htmlId });
}
// Select
if (getValue == "TravelModeId") {
travelModeId = value[1];
}
// Select Dropdown
if (getValue == "TravelModeDropdown") {
var first = "Travel Mode";
var second = value[1];
var htmlId = 3;
setarray.push({ item: first, result: travelModeId, html: htmlId, dropdown: second });
}
});
return setarray;
}
}
And than you call the computed property like:
<li v-for="item in finditem" v-if="item.html == 0">
Methods don’t get called again automatically if you variable/dependency is changed which gets used inside the method.
Computed porperty get called again if a variable/dependency is changed and so v-for
gets the correct data automatically.
So try these changes and let’s see if it updates the data.
1
I see a few issues here:
-
item
is not withinv-for
, ratherli
elements are siblings to the first with the loop. Merge allli
elements together and use thev-if
andv-if-else
conditionals on the inputs instead. -
data
is an object and not a function. -
No
key
attribute onli
.
template
<ul>
<li v-for="(item, index) in finditem()" :key="index">
<label :for="item.item">{{ item.item }}</label>
<div v-if="item.html === 0" :id="item.item">{{ item.result }}</div>
<input v-else-if="item.html == 1" :id="item.item" type="text" v-model="item.result" />$
<input v-else-if="item.html == 2" type="checkbox" :id="item.item" v-model="item.result" />
<select v-else-if="item.html == 3" :id="item.item" v-model="item.result">
<option v-for="drop in item.dropdown" :value="drop.Value" :key="drop.Value">{{ drop.Value }}</option>
</select>
</li>
</ul>
script
data: () => ({
todos: []
})
Update
I’ve updated your fiddle. The inputs now render based on the conditional statements in the template and removed the jQuery dependency.
1
There are 2 things that can cause this issue:
- Its good practice to create your
data
as a function. - The reference of your variable
newdata
within yourfinditem()
method should bethis.todos[0]
or but shouldn’t you usethis.todos
as an empty array?. This is becausenewData
is a variable or string or number or whatever?
Your code should look something like (thinking newData
is a string within the todos array):
new Vue({
el: '#components-demo',
data: function () {
return {
todos: ['newData']
}
},
methods: {
finditem() {
var setarray = [];
var result = Object.keys(this.todos).map(function (key) {
return [key, this.todos[key]];
});
var travelModeId = '';
$(result).each(function (i, value) {
var getValue = value[0];
// No Input
if (getValue == "Reservation" || getValue == "LastUpdatedBy" || getValue == "LastUpdated") {
var first = value[0];
var second = value[1];
var htmlId = 0;
setarray.push({ item: first, result: second, html: htmlId });
}
// Text Input
if (getValue == "Fare" || getValue == "AgentFee" || getValue == "ChangeFee" || getValue == "NteAmount" || getValue == "GsaRate" || getValue == "ClientApprovedAmount" || getValue == "CancelledDate") {
var first = value[0];
var second = value[1];
var htmlId = 1;
setarray.push({ item: first, result: second, html: htmlId });
}
// Checkbox
if (getValue == "Ground" || getValue == "NoGsa") {
var first = value[0];
var second = value[1];
var htmlId = 2;
setarray.push({ item: first, result: second, html: htmlId });
}
// Select
if (getValue == "TravelModeId") {
travelModeId = value[1];
}
// Select Dropdown
if (getValue == "TravelModeDropdown") {
var first = "Travel Mode";
var second = value[1];
var htmlId = 3;
setarray.push({ item: first, result: travelModeId, html: htmlId, dropdown: second });
}
});
return setarray;
}
}
});
Nope, I did not test this code…
- [Vuejs]-Vue.js 3- Component doesn't load in VueJS with router
- [Vuejs]-How to change a property of an object in Vue?