在快速仪表板上工作,我想从departments_by_dept_emp
中删除第二个对象。如何用Vue最好地实现这一目标?
HTML
<tbody id="rows" v-for="result in results">
<tr>
<td>{{ result.first_name }} {{ result.last_name }}</td>
<td v-for="department in result.departments_by_dept_emp">{{ department.dept_name }}</td>
<td>{{ result.hire_date }}</td>
<td>{{ result.birth_date }}</td>
</tr>
</tbody>
JavaScript的
{
"emp_no": 10010,
"birth_date": "1963-06-01",
"first_name": "Duangkaew",
"last_name": "Piveteau",
"gender": "F",
"hire_date": "1989-08-24",
"departments_by_dept_emp": [
{
"dept_no": "d004",
"dept_name": "Production"
},
{
"dept_no": "d006",
"dept_name": "Quality Management"
}
]
}
Axios调用获取数据存储在一个空数组中
data () {
return {
results: [],
}
}
值得好
getData() {
this.loading = true
axios.get('https://url', { 'headers': { 'Api-Key': '' } })
.then(response => {
this.results = response.data.resource
this.next = response.data.meta.next
this.loading = false
console.log(response.data.resource)
})
.catch(error => {
console.log(error)
})
}
因此,从示例中,我基本上只需要生产部门名称并从我的表中删除质量管理。我不相信slice()会做到这一点,因为它只会克隆一个维度。此外,像splice()或findIndex之类的东西我收到错误“无法读取未定义的属性'{example}'”
如果您只想显示数组中的元素,则可以传递要显示的元素的索引
var app = new Vue({
el: '#app',
data: {
results: [{
"emp_no": 10010,
"birth_date": "1963-06-01",
"first_name": "Duangkaew",
"last_name": "Piveteau",
"gender": "F",
"hire_date": "1989-08-24",
"departments_by_dept_emp": [
{
"dept_no": "d004",
"dept_name": "Production"
},
{
"dept_no": "d006",
"dept_name": "Quality Management"
}
]
}]
}
})
<script src="https://unpkg.com/vue"></script>
<div id="app">
<table>
<tbody id="rows" >
<tr v-for="result in results">
<td>{{ result.first_name }} {{ result.last_name }}</td>
<td>{{ result.departments_by_dept_emp[0].dept_name }}</td>
<td>{{ result.hire_date }}</td>
<td>{{ result.birth_date }}</td>
</tr>
</tbody>
</table>
</div>
希望这对你有用。
由于vue.js数据对象只是javascript对象,因此应该可以使用array.pop()。
您应该能够在方法块中添加这样的函数:
popDept: function() {
return this.departments_by_dept_emp.pop();
},
此功能将从列表中删除部门,并将其返回。
有可能为您编写一些代码......
Vue是一个javascript框架,所以你可以用普通的javascript编写函数来查找数组中的项目,然后将其删除。所以在你通过department.dept_name
迭代的模板中,你可以添加一个点击监听器,@click="removeElement(department.dept_name)"
然后在你的脚本部分:
export default {
data: () => ({
results: {
"emp_no": 10010,
"birth_date": "1963-06-01",
"first_name": "Duangkaew",
"last_name": "Piveteau",
"gender": "F",
"hire_date": "1989-08-24",
"departments_by_dept_emp": [
{
"dept_no": "d004",
"dept_name": "Production"
},
{
"dept_no": "d006",
"dept_name": "Quality Management"
}
]
}
}),
methods: {
removeElement(x) {
var ind = this.results.departments_by_dept_emp.findIndex(n => n.dept_name === x)
this.results.departments_by_dept_emp.splice(ind, 1)
}
这将从数组中删除对象,并保持原样。