代码之家  ›  专栏  ›  技术社区  ›  Maulik Kakadiya

handsontable中的更新内容数据问题

  •  7
  • Maulik Kakadiya  · 技术社区  · 6 年前

    我正在努力实现 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>
    

    通过上面的代码,我的数据成功地从服务器接收到,但是我无法更新中的数据 可握手 ,如以下截图所示:

    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

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

    1 回复  |  直到 6 年前
        1
  •  2
  •   tony19 thanksd    6 年前

    我看到两个问题:

    • handsontable 似乎无法处理动态 settings (见 console errors 设置 不应是计算属性。因为唯一需要更新的设置属性是 settings.data ,该属性本身就应该进行变异(即,不要重置 设置 ).

      设置 进入之内 data() ,正在初始化 null 所以它仍然是反应性的:

      data() {
        settings: {
          data: null,
          colHeaders: [...],
          ...
        }
      },
      computed: {
        // settings() { }  // DELETE THIS
      }
      
    • getSelectApp 是一个不正确异步的计算属性(即,在本例中,它获取数据并稍后处理响应)。计算属性不能是异步的,因此这个计算属性实际上返回 undefined return 在computed属性内部调用时,返回不会设置computed属性的值,因为它位于 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

      似乎这个计算属性的目的是更新 基于所选选项的值。要做到这一点,你必须使用 watcher selectedOption ,这会改变 设置.数据 .

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

    demo