<select> bound with array performs incorrectly after splice()
See original GitHub issueConsider 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:
- Created 7 years ago
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Tested on 2.0 and it worked as expected: https://jsfiddle.net/dycmgzcm/ So I’m marking this as a bug of 1.x
@peteranny It may be a bug because
selected
remained 0 while theoptions
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 atrack-by
prop to<options>
, which probably made vue think it’s safe to update the selected value in dom. https://jsfiddle.net/74ncq90w/