element icon indicating copy to clipboard operation
element copied to clipboard

select选择器,label值不变

Open tdida opened this issue 10 years ago • 12 comments

maps=[{
  "name": "福建",
  "city": [{
    "name": "福州"
  },{
    "name": "厦门"
  }]
},{
  "name": "广东",
  "city": [{
    "name": "广州"
  },{
    "name": "深圳"
  }]
}]

computed: {
    city: function() {
      for (let province of this.maps) {
        if (this.onProvince == province.name) {
          return province.city
        }
      }
    }
  },

<el-select v-model="onProvince">
     <el-option
        v-for="(item, index) in maps"
        :label="item.name"
        :key="index"
        :value="item.name">
        </el-option>
     </el-select>
 <el-select v-model="onCity">
     <el-option
         v-for="(item, index) in city"
        :label="item.name"
        :key="index"
        :value="item.name">
    </el-option>
</el-select>

这是一个地区联动选择器,省级选择器选择后,市级的el-select选择器会出现第一个el-option的label值不变,value有变的情况

tdida avatar Sep 14 '16 03:09 tdida

由于设置了 :key="index",当 onProvince 变化后 index 的值没有变化,所以 Vue 2.0 不会重新渲染。建议改为 :key="item.name",同时在 onProvince 变化后处理一下 onCity 的值。

Leopoldthecoder avatar Sep 14 '16 03:09 Leopoldthecoder

@Leopoldthecoder 多谢,解决了

tdida avatar Sep 14 '16 04:09 tdida

@Leopoldthecoder 写文档里?

QingWei-Li avatar Sep 14 '16 04:09 QingWei-Li

@QingWei-Li Vue本身的不需要写文档吧?

csvwolf avatar Sep 14 '16 05:09 csvwolf

其实文档里提了一句,可能不太明显,我想想怎么改一下

Leopoldthecoder avatar Sep 14 '16 05:09 Leopoldthecoder

@csvwolf 不写的话出问题就赖 Element 🌚

QingWei-Li avatar Sep 14 '16 05:09 QingWei-Li

@QingWei-Li -0-(逃2333

csvwolf avatar Sep 14 '16 05:09 csvwolf

Leopoldthecoder ,按照你的方法,我同时在 onProvince 变化后处理一下 onCity 的值,发现出现第一个el-option的label值不变,还是不变, "element-ui": "^1.0.0-rc.4", "vue": "2.0.0-rc.6",

computed: { city () { console.log('cicici', this.onCity) for (const province of this.maps) { if (this.onProvince == province.name) { this.onCity = '' return province.city } } } }

perfectyang avatar Sep 27 '16 08:09 perfectyang

代码如下:

perfectyang avatar Sep 27 '16 08:09 perfectyang

@perfectyang 我按你说的写了个 Demo:https://jsfiddle.net/gL6mf2te/ ,看起来是正常的。你说的 label 值不变是指什么?

Leopoldthecoder avatar Oct 05 '16 02:10 Leopoldthecoder

抱歉, 我也有同样的问题。似乎在mac os 12.4里才会发生。 chrome 58 , element-ui网站上都ok,上面jsfiddle的例子也ok,就是自己使用不行,select后label显示不变。 同样的代码在el captian上就似乎没有问题。 我使用laravel。 多谢!

maverickchangithub avatar May 02 '17 07:05 maverickchangithub

el-select组件的onchange方法,执行this.$forceUpdate(); 重新渲染实例就好了

tomastong avatar Jun 17 '20 07:06 tomastong