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.

https://i.sstatic.net/XrtAo.gif

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}`;

https://i.sstatic.net/vhHdR.gif

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

The Vuejs code functions properly in the overall application, but is not functioning within the specific component

Before, my Vue.js app worked flawlessly until I tried putting it into a Vue component. It started throwing the following error: [Vue warn]: Error compiling template. Here's the code for the component (components.php): <script> Vue.component(&ap ...

Maintain the proportion of the browser window when reducing its size

<html> <head> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <img src="spacer--1200x800.png" width="1200" height="800" /> </body> </html> This section contains CSS ...

AngularJS - Using filters to extract specific options from multiple <select> components

There are N "select" components generated based on JSON data, with the "options" populated by an external API. I am trying to implement a custom filter that ensures when an option is selected in one "select" component, it should not appear in the other com ...

Incorporate relationships while inserting data using Sequelize

vegetable.js ... var Vegetable = sequelize.define('Vegetable', { recipeId: { allowNull: false, ... }, name: { ... }, }); Vegetable.association = models => { Vegetable.belongsTo(models.Recipe); }; ... recipe.js ... var Recipe = sequeliz ...

Show or hide side menu depending on when a div reaches the top of the

I have a side menu that opens when a specific div reaches the top of the window. The menu includes a toggle button for opening and closing it. However, I am encountering an issue where the script keeps closing the menu on scroll even after manually openi ...

Ways to trigger a popup when a link is clicked in Vue.js?

<label class="label-set"> <input type="checkbox" class="" name="sameadr" /> I agree to all statements of <a href="#" style="color: #ee1d24"> Terms of Use </a> </label> Is there a way to trigger a popup when a user clic ...

Unable to detect errors using React/Redux

I encountered a login error handling issue with redux that is structured like this: export const login = (params: any) => async (dispatch: Dispatch) => { try { const authData = await API.post("login", params); sessionStorage.setIt ...

Avoid displaying identical items when rendering a page from JSON data

I am using ajax and json to render a page. The structure of my json is as follows: {"status":"ok","rewards":[{"id":201,"points":500},{"id":202,"points":500}]}. I want to load the data using ajax only once if 'points' have duplicates in any of the ...

Leveraging jQuery to automatically fill in a time field while excluding other buttons

I've been working on using jQuery to automatically fill in a time input field. The functionality is working properly, but the time is also being applied to my other button. How can I make sure it only gets assigned to the time input field? I would re ...

Angular directive ceases to trigger

I am currently working on implementing an infinite scrolling directive. Initially, when the page loads and I start scrolling, I can see the console log. However, after the first scroll, it stops working. It seems like it only triggers once. Can anyone poi ...

Tips for relocating a div panel below all other div panels when a button is clicked with JQuery and CSS

There are several panels stacked below each other. Each panel has a button that, when clicked by the user, should move the panel to the bottom of the stack. For example: If there are 10 panels aligned sequentially and the user wants to remove Panel 2 by ...

Switching the body's background image dynamically using javascript

I'm attempting to switch up the background image using JavaScript. Here's how I've defined the background: body { background: black; overflow-x: hidden; overflow-y: hidden; } body:before { overflow-x: hidden; overflow ...

Potential Scope Problem in Angular JS Controller

The HTML code snippet I have is as follows: <body ng-controller = "Control as control"> <button ng-click = "control.prepareAction()">Do Action </button> <div id="one" ng-hide = "!control.showOne" > <div> <h6> ...

Altering content using jQuery

Trying to create a dynamic quiz using HTML, CSS, and jQuery. I am having trouble changing the <p id=question></P> element with jQuery code as nothing happens. Below is my HTML and jQuery code: <!DOCTYPE html> <html lang='es' ...

Storing HTML labels in an array using a jQuery selector in JavaScript can be a

My code snippet in HTML looks like this: <p style:"text-align:center"><label>Question1</label></p> <p style:"text-align:center"><label>Question2</label></p> <p style:"text-align:center"><label>Qu ...

Exploring the possibilities of utilizing JavaScript within TypeScript

My dynamic javascript object holds all the resources (translation strings) for my app. Here's how it is structured: var ResourceManager = (function () { function ResourceManager() { var currentLanguage = $('#activeLanguage').htm ...

Populate List Items until Maximum Capacity is Reached and Increase the

Is there a way to make a ListItem fill the list vertically while also being able to overflow it if needed? I'm looking for a solution that would allow me to access the height of the empty space in the list, which I believe would be the minHeight. Som ...

Switching from map view to satellite view on Google Maps allows you to see detailed aerial

Is there a way to switch from map view to satellite view on a Google Map using JavaScript after zooming in 100%? If so, how can it be achieved within the following JavaScript code? DEMO:http://jsfiddle.net/keL4L2h0/ // Load Google Map ///////////////// ...

Sharing properties across various components with Next.js

I'm still getting the hang of using Next.js and encountering issues with sharing data between components. Currently, I have a setup involving three components: //index.js function App() { const choices = [] for (let i = 1; i < 101; i++) { ...

Having difficulty entering text in the modal text box and updating it with a new state

Within my render method, I am utilizing the following model: { showEditModal && <Modal toggleModal={this.togglePageModal} pageModal={true}> <h2 style={{ textAlign: "center", width: "100%" }}> ...