ElementUI Cascader not rendering properly

Utilizing elementUI's cascading selector to present data, I have crafted this code in accordance with the official documentation.

  <el-cascader
    v-model="address"
    :options="addressOptions"
    :props="{expandTrigger: 'hover'}"
  ></el-cascader>

  data() {
    return {
      address: '',
      addressOptions: [
        {
          value: 'Beijing',
          label: 'Beijing',
          children: this.getOptions("Beijing")
        },
        {
          value: 'Shanghai',
          label: 'Shanghai',
          children: this.getOptions("Shanghai")
        }
      ]
    }
  },
  methods: {
    getOptions(val) {
      let res = [];
      for(let i=1; i<=5; i++) {
        let floor = Object.create(null);
        floor.value = i;
        floor.label = i;
        floor.children = [];
        for(let j=1; j<=5; j++) {
          let obj = Object.create(null);
          obj.value = j < 10 ? `0${j}` : `${j}`;
          obj.label = j < 10 ? `0${j}` : `${j}`;
          floor.children.push(obj);
        }
        res.push(floor);
      }
      return res;
    }
  }

Although it appears to be correct, when selecting an option, the first and second level data remain unchanged. See below for illustration.

After prolonged contemplation, I am still unable to discern the reason behind this peculiar behavior. What baffles me further is that combining third-level data with prior parameters yields normal results.

  // Can be displayed normally
  // obj.value = j < 10 ? `0${j}${val}` : `${j}${val}`;
  // obj.label = j < 10 ? `0${j}${val}` : `${j}${val}`;

  // This cannot be displayed normally.
  obj.value = j < 10 ? `0${j}` : `${j}`;
  obj.label = j < 10 ? `0${j}` : `${j}`;

If you have insights or solutions, kindly share them. Much appreciated!

You may test it out here: https://jsfiddle.net/DangoSky/7osfp265/1/

Answer №1

After investigating the issue, it appears that when selecting the hierarchy Shanghai > 3 > 03, the library searches from top to bottom where the value 03 is located. It seems that the first place where 03 is found is in Beijing > 1 > 03, hence displaying the incorrect label. This behavior could be attributed to how the library was designed or implemented. Interestingly, using the handleChange method and logging the value shows the correct output.

One potential solution to bypass this problem would be to ensure that all values are unique across options. For example, assigning a value like 'Shanghai+3+03' allows for easy parsing and retrieval of the original values.

You can view a functional example in this fiddle https://jsfiddle.net/n365ecuk/

   getOptions(val) {
     let res = [];
  for(let i=1; i<=5; i++) {
    let floor = Object.create(null);
    floor.value = val + '+' + i;
    floor.label = i;
    floor.children = [];
    for(let j=1; j<=5; j++) {
      let obj = Object.create(null);
      // Can be displayed normally
      // obj.value = j < 10 ? `0${j}${val}` : `${j}${val}`;
      // obj.label = j < 10 ? `0${j}${val}` : `${j}${val}`;
      // This cannot be displayed normally.
      obj.value = val + '+' + i + '+' + j;
      obj.label = j < 10 ? `0${j}` : `${j}`;
      floor.children.push(obj);
    }
    res.push(floor);
 }
 return res;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Right-align SELECT-OPTIONS text

Here are the screenshots of the form I'm currently working on. I am aiming to create a select box in the form where the text within the options is aligned to the right. After an option is selected, the chosen text should be displayed as illustrated i ...

Saving MongoDB query results to a file using the built-in Node.js driver

I have been attempting to save the output of a MongoDB query to a file using the native Node.js driver. Below is my code (which I found on this post: Writing files in Node.js): var query = require('./queries.js'); var fs = require('fs' ...

Error encountered while attempting to download PDF file (blob) using JavaScript

Recently, I have been utilizing a method to download PDF files from the server using Laravel 8 (API Sanctum) and Vue 3. In my Vue component, I have implemented a function that allows for file downloads. const onDownloadDocument = (id) => { ...

Is there a way for me to position my arrow beside the header while still keeping the toggle function intact?

Can anyone assist me in positioning my arrow next to the header text, which I toggle on click? I attempted placing the arrow <div> into the H1 tag, but then the toggle function stops working. Any help would be greatly appreciated! Please includ ...

Determine if Param is empty or not within the context of express.js

I am looking to create a table and populate it with all the parameters that are not empty in the parameter list provided below: http://localhost:5002/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="407535732b7008121931190539142 ...

Is there a way to replicate ajaxStart and ajaxStop functions without using jQuery?

After reviewing the extensive jQuery code, I'm wondering if this task would be simple to accomplish. Any suggestions on how to approach it? I'm interested in using this not for a webpage, but for a C# application that needs to monitor ajax activ ...

How can I add seconds to the jquery datetimepicker plugin?

Looking to implement the datetimepicker plugin from here, but I'm having trouble finding information on how to include the seconds part. Is there a way to add seconds to this plugin? It's a crucial requirement, so any suggestions for another good ...

Issue with Axios not including cookies in the headers, irrespective of the use of withCredentials: true | Backend with FastAPI

Here is my axios configuration I am able to receive the cookie successfully using Postman, so it seems to be an issue with axios. const api = axios.create({ baseURL: 'http://localhost:8000/', withCredentials: true, headers: { crossDomain: ...

What is the method for placing one object within another object?

This is a sample of my data collection: [ { "column1":"data1", "column2":"data2", "column3":[ { "column31":"data31", "column32& ...

Javascript for Cordova utilizing WebSocket and incorporating client side certificates

I am looking to establish a secure SSL/TLS connection between my client and server, with only a specific authorized client allowed to connect. To achieve this, I have generated a client certificate using OpenSSL on the server for mutual authorization. The ...

Encountered an issue when attempting to create meanjs using yo

After installing yeoman globally, I encountered an error while trying to generate meanjs with yo. Unfortunately, I was unable to identify the root cause of the problem. view image here Any help would be greatly appreciated. I attempted using sudo as well ...

Ways to retrieve form information from a POST request

I received a POST request from my payment gateway with the following form data: Upon trying to fetch the data using the code snippet below, I encountered errors and gibberish content: this.http .post<any>('https://xyz.app/test', { ti ...

Guide to selecting the radio button in group2 by clicking on the radio button in group1

Here is my scenario: I have two sets of radio buttons as shown in the code snippet below: <form name="form" method="post" action=""> <label><input type="radio" name="RadioGroup1" value="radio" id="Radio1">Radio 1</label><br> ...

Utilize Javascript/Jquery to categorize JSON data based on the days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

A function is provided below that retrieves data for a chart. The output produced by this function is structured as follows: [Object { date=Date, value=112, volume=1469}, Object { date=Date, value=124, volume=539}, Object { date=Date, value=114, vo ...

What is the best way to iterate over an array of objects?

I have an Array of Objects that I need to use in order to create an HTML Table: Array(5) 0: Object id: 4 name: Sand Jane address: Green Sand Street ... ... ... 1: Object 2: Object ... ... ... Currently, I am able to perform a search wit ...

Displaying or concealing an input field in vue.js depending on the selection of a radio button

I am currently working on customizing a pre-built form in Vue.js, where certain inputs are displayed or hidden based on the selection of two radio buttons: <b-form-group label-class="ssrv-form-control"> <div class="ssrv-5"& ...

What is the correct way to include a variable such as /variable/ in a MongoDB query?

I need help passing in a searchTerm from a variable, what is the best way to do this? const mongoquery = { description: { $in: [ /searchTerm/ ] } }; I attempted it like this initially: const mongoquery = { description: { $in: [ `/${searchTerm}/` ] } }; H ...

What is the best way to handle multiple responses in Ajax within a single function?

Here is a simple code snippet: $.ajax({ url:'action.php', method: 'POST', data:{getcart:1}, success:function(response){ $('#getcart').html(response);//want to ...

Vue.js computed property fails to initiate

Why is the Vue JS computed property not triggered with this markup? <!-- language: lang-html --> <p>£{{plant_price}}</p> <div v-if="selected.plant.variations.length > 0 "> <select v-model="selected.plant.selected_ ...

Is there a more efficient method to execute this AJAX request?

$('#request_song').autocomplete({ serviceUrl: '<%= ajax_path("trackName") %>', minChars:1, width: 300, delimiter: /(,|;)\s*/, deferRequestBy: 0, //miliseconds params: { artists: 'Yes' }, onSelect: functi ...