gpt4 book ai didi

vue.js - handsontable 中更新内容数据的问题

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:02 26 4
gpt4 key购买 nike

我正在尝试实现handsontable。根据我的要求,我想通过更改下拉值重新呈现 handsontable,但是在下拉选择时,handsontable 没有正确更新。下面是我的代码:

Handsontable.vue:

<template>
<div id="hot-preview">
<HotTable :settings="settings" :ref="referenceId"></HotTable>
<div></div>
</div>
</template>

<script>
import { HotTable } from '@handsontable-pro/vue';

export default {
components: {
HotTable
},
props: ['settings', 'referenceId'],
}
</script>

<style>
#hot-preview {
max-width: 1050px;
height: 400px;
overflow: hidden;
}
</style>

父组件:

<template>
<div id="provisioning-app">
<v-container grid-list-xl fluid>
<v-select
:items="selectList"
item-text="elementName"
item-value="elementName"
label="Standard"
v-model="selected"></v-select>
<handsontable :settings.sync="settings" :referenceId="referenceId"></handsontable>
</v-container>
</div>
</template>

<script>
import Handsontable from '@/components/Handsontable';
import PrevisioningService from '@/services/api/PrevisioningService';

export default {
components: {
Handsontable
},
data: () => ({
selectList: [],
selectApp: [],
selectedOption: '',
referenceId: 'provision-table',
}),

created(){
PrevisioningService.getProvisioningList(this.$session.get('userId'), this.$session.get('customerId')).then(response => {
this.provisioningList = response;
});
},

beforeUpdate() {
this.provisioningApp = this.getProvisioningAppList;
},
computed: {
settings () {
return {
data: this.getSelectApp,
colHeaders: ["Data Uploaded on", "Duration in Minutes", "Start Time", "Shift","Description","Next Day Spill Over", "Site Name"],
columns: [
{type: 'text'},
{type: 'text'},
{type: 'text'},
{type: 'text'},
{type: 'text'},
{type: 'text'},
{type: 'text'}
],
rowHeaders: true,
dropdownMenu: true,
filters: true,
rowHeaders: true,
search: true,
columnSorting: true,
manualRowMove: true,
manualColumnMove: true,
contextMenu: true,
afterChange: function (change, source) {
alert("after change");
},
beforeUpdate: function (change, source) {
alert("before update");
}
}
},

getSelectApp () {
if(this.selectedOption !== undefined && this.selectedOption !== null && this.selectedOption !== ''){
PrevisioningService.getProvisioningAppList(this.selectedOption, this.$session.get('userId'), this.$session.get('customerId')).then(response => {
this.provisioningApp = response;
return this.provisioningApp;
});
}
}
},
method: {
getSelected () {
return this.selectedOption;
}
}
};
</script>

使用上面的代码,我的数据从服务器成功接收,但是我无法更新handsontable中的数据,如下截图所示:

Here how to show Handsontable when No any value selected on drop-down

And 2nd image show how the table looks after change dropdown value

如何在下拉选择后正确呈现表格?

最佳答案

我看到两个问题:

  • handsontable似乎无法处理动态 settings (参见 console errors ),所以 settings不应该是计算属性。由于唯一需要更新的设置属性是 settings.data , 应该单独改变该属性(即不要重置 settings 的值)。

    要解决此问题,请移动 settings进入data() , 正在初始化 settings.datanull这样它仍然是 react 性的:

     data() {
    settings: {
    data: null,
    colHeaders: [...],
    ...
    }
    },
    computed: {
    // settings() { } // DELETE THIS
    }
  • getSelectApp是一个错误异步的计算属性(即,在这种情况下,它获取数据并稍后处理响应)。计算属性不能是异步的,所以这个计算属性实际上返回 undefined .虽然有一个 return在计算属性内部调用,返回不设置计算属性的值,因为它在 Promise callback 内部:

     PrevisioningService.getProvisioningAppList(/*...*/).then(response => {
    this.provisioningApp = response;
    return this.provisioningApp; // DOES NOT SET COMPUTED PROPERTY VALUE
    });

另请注意 side effect来自 this.provisioningApp = response .好像没有this.provisionApp在任何情况下都需要此代码,因此应将其删除以进行清理。

这个计算属性的目的似乎是更新 settings.data基于所选选项的值。为此,您必须使用 watcherselectedOption , 这会改变 settings.data .

    watch: {
selectedOption(val) {
PrevisioningService.getProvisioningAppList(/*...*/).then(response => {
this.settings.data = response;
});
}
},

demo

关于vue.js - handsontable 中更新内容数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52880602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com