我在 Vue 更新 UI 中的数据出现问题,但似乎只有复选框存在此问题。
我试图一次只允许选中一个复选框,因此我在单击其中之一时将所有复选框设置为 false。但是,这在数据中有效,但在复选框中无法正确呈现。
HTML:
<table class="buildings-modify--table table-full table-spacing">
<thead>
<tr>
<th>Name</th>
<th>Primary</th>
<th>Address</th>
<th>City/Town</th>
<th>Province</th>
<th>Postal Code</th>
<th>Phone</th>
<th>Active</th>
</tr>
</thead>
<tbody>
<tr v-for="landlord in selectedLandlords" :key="landlord.id">
<td>{{ landlord.name }}</td>
<td>
<input type="checkbox" :value="landlord.is_primary" @change.prevent="makePrimaryLandlord(landlord)">
</td>
<td>{{ landlord.address }}</td>
<td>{{ landlord.city }}</td>
<td>{{ landlord.province}}</td>
<td>{{ landlord.postal_code}}</td>
<td>{{ landlord.phone }}</td>
<td>{{ landlord.is_active ? "Yes" : "No" }}</td>
</tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
视图代码:
export default {
data: function() {
return {
selectedLandlords: []
}
},
methods: {
makePrimaryLandlord: function(landlord) {
this.selectedLandlords = this.selectedLandlords.map(item => {
item.is_primary = false; return item});
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
只有复选框似乎有问题。如果我更改名称或带有过滤数组的文本值,将它们全部设置为特定值,它们会更改,但复选框数据更改不会反映在 UI 中,但 Vue 中的数据是正确的。