javascript - 如何将数据从 vue 中的表格组件传递到另一个表格组件,使用编辑按钮传递表格中的每一行
问题描述
我有两个表组件,一个使用 axios 获取记录,我希望另一个表组件在单击父组件上的编辑按钮后获取记录。我已经尝试过使用 vue 提供的父子组件通信,但它不适用于表格。第一个表组件
<div><table class="table table-bordered table-hover">
<thead>
<tr>
<th>Request NO</th>
<th>User name</th>
<th>Product</th>
<th>Requested Quantity</th>
<th>Approved Quantity</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead> <tbody>
<tr v-for="temp in reques " >
<td>{{temp.operation_id}}{{temp.user_id}}</td>
<td>{{temp.user_id}}</td>
<td>{{temp.product_name}}</td>
<td>{{temp.rquantity}}</td>
<td>{{temp.aquantity}}</td>
<td>{{temp.status}}</td>
<td>{{temp.created_at}}</td>
<td>
<div role="group" class="btn-group">
<button class="btn btn-info" @click="edit(temp)"
type="button">
<i class="fa fa-edit"></i>
</button>
<button class="btn btn-danger" type="button">
<i class="fa fa-trash"></i>
</button>
</div>
</td>
</tr>
</tbody>
</table>div class="col">
<approved :form="form"></approved>
</div></div>
import VForm from 'vform'
import AlertError from "vform/src/components/AlertError";
import HasError from "vform/src/components/HasError";
import approved from "./approved";
import operationcreate from "./create" export default {
components: {HasError, AlertError,
approved,operationcreate,
},
data() {
return {
reques: [],
here: [],
form: new VForm({
operation_id:'',
product_name:'',
rquantity: '',
aquantity:'',
status:'',
created_at:'',
user_id:''
})
}
},
methods:{
edit(temp){
this.form.fill(temp);
}
},
created() {
axios.get('/request-list')
.then(({data}) => this.reques = data);
}
}
第二个表格组件
<div>
<div class="card">
<div class="card-header">
<h3 class="card-title">Quick Access</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
<div class="card-body p-0">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th> Product Name</th>
<th> Request Quantity </th>
<th> Approved Quantity</th>
<th> Status</th>
</tr>
</thead>
<tbody>
<tr v-for="temp in form">
<td>1</td>
<td>{{temp.rquantity}}</td>
<td>{{temp.aquantity}}</td>
<td>{{temp.status}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script>
export default {
props: ['form'],
data() {
return {
}
},
methods: {},
created() {
}
}
解决方案
当您在表 1 中的 axios 请求后获取数据时,您必须发出一个事件,以便父级可以访问它。
所以在表 1 中:
created() {
axios.get('/request-list')
.then(({data}) => {
this.reques = data
this.$emit("dataRecieved",data)
});
}
这将在组件中触发一个事件,该事件可由父级的v-on(或 @)访问:
<table-component-1 @dataRecieved="updateTableTwo" ></table-component-1>
您需要在父对象中创建一个空对象并作为道具传递给表 2,直到收到 axios 请求值:
<table-component-2 :myData="tableData" ></table-component-1>
父级中的updateTableTwo()方法(默认传递事件参数):
updateTableTwo(event) {
this.tableData = event
//event will contain the passed data from table 1
}
推荐阅读
- tensorflow - 如何从 NN 的二进制编码创建自定义 Tensorflow 图
- angular - Rx JS startsWith() 运算符解析 Promise 的结果?
- u-sql - Azure Data Lake U-SQL 并行作业失败
- python - 更改 scipy 稀疏矩阵的对角线或数据属性也会更改该矩阵的所有副本
- go - Golang 捕获 sigterm 并继续申请
- python-sphinx - 警告:toctree 包含对不存在文档的引用
- javascript - 将 span 标签中的值插入 SQL 数据库
- hyperledger-fabric - 链码已实例化,但未出现在实例化代码列表中
- flutter - 如何在 Flutter 的 Scaffold.drawer 中保持小部件的状态?
- android-studio - 在 Android Studio 中创建项目时,由于以下提到的错误,Gradle 同步失败: