riophae / vue-treeselect

A multi-select component with nested options support for Vue.js
https://vue-treeselect.js.org/
MIT License
2.89k stars 505 forks source link

Unable to change options after async options load. #535

Open 43Laboratories opened 11 months ago

43Laboratories commented 11 months ago

The issue is when I load the initial options and make a query in this case i query NO1. I then update the options. After updating the options (clearing them) no1 still appears in the options until i use a non-cached query. So for example if I query o1 it will not show no1 in the options after clearing, because i did not query o1. I think the issue is caching or there might be errors in the code. Either way I am struggling to change the options properly using async mode.

I am simply trying to clear the options when this event is emitted. eventBus.$on('remove_all_options', this.changeOptions);

TreeSelect Component HTML

<div id="app" class="outerSelect">
  <div id="TS_1" class="TS_1" > 
  <treeselect v-model="value" :multiple="true" :options="optionsData" placeholder="Markets"
  :async="true"
  :load-options="loadOptions"
  ref="treeselect"
  > 

TreeselectJS
const simulateAsyncOperation = fn => {
  setTimeout(fn, 750)
}

 new Vue({
el: '#app',
data: {
  value: null,
  optionsData: [
  ], 
  },
created() {
  eventBus.$on('remove_all_options', this.changeOptions);
     },

Methods:

    methods: {

        triggerLoadOptions(){
            console.log("should be making a callback")
            this.$refs.treeselect.loadOptions({ callback: () => {} });
        },

method2 loads the initial options

  loadOptions(params) {

      console.log("load options called")
      console.log("called = ", window.glob_called)

    if (window.glob_called === true) {

      this.optionsData = []

      this.value = null;

      if (params && typeof params.callback === 'function') {
          console.log("Performing callback for update")
        params.callback(null, this.optionsData);
      }
    } else if (window.glob_called === false) {

      simulateAsyncOperation(() => {
        const initialOptions = [
          {
            id: 'No1',
            label: 'No1',
            children: [
              { id: 'suboption1', label: 'No1 Suboption 1' },
              { id: 'suboption2', label: 'No1 Suboption 2' },
            ],
          },
        ];

        this.optionsData = initialOptions
        if (params && typeof params.callback === 'function') {
          console.log("performing callback for load")
          params.callback(null, this.optionsData);
        }
      });
    }
  },

method3 changes the options

        changeOptions() {

      window.glob_called = true
      this.optionsData = [];
    this.v_search_query = null;
    this.$refs.treeselect.loadOptions({ callback: () => {} }); 
  },
},

});