question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<select> bound with array performs incorrectly after splice()

See original GitHub issue

Consider the following code:

<div id="demo">
  <select v-model="selected" number>
    <option v-for="opt in options" :value="$index" number>{{opt}}</option>
  </select>
</div>

In <select>, each option value is bound with $index. If we perform splice() on options, like, options.splice(0,1), $index does not correctly sync with options.

For example, let options=['a','b'] and the rendered HTML looks like (but not really):

<div id="demo">
  <select selectedIndex="0">
    <option value="0">a</option>
    <option value="1">b</option>
  </select>
</div>

After options.splice(0,1), then options=['b'], but the rendered HTML becomes:

<div id="demo">
  <select selectedIndex="0">
    <option value="1">b</option>
  </select>
</div>

The value of ‘b’ option does not become 1, which should be 0 because of $index. This may not be a bug, but this is kind of odd.

Below is the live demo of this issue: https://jsfiddle.net/peteranny/trwp98g9/4/

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fnlctrlcommented, Oct 2, 2016

Tested on 2.0 and it worked as expected: https://jsfiddle.net/dycmgzcm/ So I’m marking this as a bug of 1.x

2reactions
fnlctrlcommented, Oct 2, 2016

@peteranny It may be a bug because selected remained 0 while the options changed, and the dom is supposed to reflect this change, but I’m not sure…

For now, you can try adding selected prop as @defcc suggested, (note that mustache binding for props/attrs is deprecated in 2.0, so please use :selected="$index == selected" instead) Or you can add a track-by prop to <options>, which probably made vue think it’s safe to update the selected value in dom. https://jsfiddle.net/74ncq90w/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Returning an array without a removed element? Using splice ...
I know splice() returns the removed element and changes the array, but is there function to return a new array with the element...
Read more >
Let's clear up the confusion around the slice( ), splice( ), & split ...
Slice( ) and splice( ) methods are for arrays. The split( ) method is used for strings. It divides a string into substrings...
Read more >
Binding items not updated unless it's new array #166 - GitHub
If the items contained in myArray change (remove or add items), the changes are not reflected to the ng-select . A workaround is...
Read more >
Array.prototype.splice() - JavaScript - MDN Web Docs
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Read more >
How to Index, Slice and Reshape NumPy Arrays for Machine ...
After completing this tutorial, you will know: ... Specifying integers too large for the bound of the array will cause an error.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found