javascript - 如何在制表符选项中访问 vue 数据变量或方法
问题描述
我正在使用带有 vue 的制表符。包:tabulator-tables 和 vue-tabulator。
我正在尝试在制表符行上设置点击事件,但需要在点击事件中访问 vue 数据变量。
脚本如下所示:
import axios from "axios";
import qs from "qs";
import { TabulatorComponent } from "vue-tabulator";
export default {
components: {
name: "Main",
CustomerData: TabulatorComponent
},
data() {
return {
tab: "",
customer_data: "",
customers: null,
cdata: [
{
f_name: "",
l_name: ""
}
],
customer_options: {
rowClick: function(e, row) {
axios
.post(
"php/getcustomer.php",
qs.stringify({
id: row.getCell("id").getValue()
})
)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
},
layout: "fitColumns",
columns: [
{
title: "ID",
field: "id",
sorter: "string",
visible: false,
},
{
title: "First",
field: "f_name",
sorter: "string",
editor: false,
},
{
title: "Last",
field: "l_name",
sorter: "string",
editor: false,
}
]
}
};
},
methods: {},
created() {
axios.get("php/getcustomers.php").then(response => {
this.cdata = JSON.parse(JSON.stringify(response)).data;
});
}
};
如何从“rowClick”事件处理程序中访问 vue 变量“customer_data”?
解决方案
你rowClick
的不是一种方法。您需要将其移到methods
密钥下,然后才能访问this.customer_data
.
// ...Rest of the code
customer_options: {
rowClick: this.rowClick
},
// Rest of the data
methods:{
rowClick(e, row){
// Do your thing with this.customer_data
}
}
另外,为什么要在customer_options
. 这似乎很奇怪。
推荐阅读
- html - fb:app_id 还需要吗?
- node.js - 是否有解决方案将项目推送到节点 js 中的异步函数中的数组?
- docker - 在 WSL2 权限被拒绝的詹金斯容器内运行 docker 命令
- data-visualization - 在 Tableau 中规范化表格
- java - 您如何处理在 JTable 中选择单元格的事件?
- asp.net - ASP.NET Core 5:创建 Web 主机时发生致命错误
- python - 如何将 lambda 应用程序作为本地 API 运行
- javascript - JS CSS HTML - 创建一个独立于文本数量保持相同宽度的按钮
- swi-prolog - 为什么 sort/4 会这样?
- r - 将 SQL 数据连接到 R