当前位置: 代码迷 >> 综合 >> uniapp for循环显示的数据,无法动态修改数据的解决方案
  详细解决方案

uniapp for循环显示的数据,无法动态修改数据的解决方案

热度:43   发布时间:2023-12-28 00:15:22.0

 重点:替换数组中的指定位置的元素(把修改后的元素赋值给新声明的变量,进行list.splice(0, 1, new_data);

splice传入的第一个参数代表要替换的数组下标,第二个参数代表替换的个数,第三个参数代表要替换的元素

<view class=""><block v-for="(item,index) in list" ><view class="w-710 h-160 m-auto border-bottom-gray3-2 dis-flex ali-center jus-spa"><image class="w-120 h-120 m-left-20" :src="item.goods_image?host+item.goods_image:'../../static/image/zfb48.png'" mode=""></image><view class="w-550 m-left-20 f-30"><view class="m-bottom-15">{
   {item.goods_name}}</view><view class="dis-flex ali-center jus-spa m-top-15"><view class=""><text class="col-pink">¥{
   {item.discount_price}}</text><text class="col-gray2">/{
   {item.unit?item.unit:'个'}}</text></view><view class="dis-flex ali-center m-right-20"><image v-if="item.now_count > 0" class="w-48 h-48" src="../../static/image/jian48.png" mode=""></image><input v-if="item.now_count > 0" class="w-80 t-center" type="text" :value="item.now_count" maxlength="3" @input="onNumInput($event,index)"/><image class="w-48 h-48" src="../../static/image/jia48.png" mode="" @tap="addShop(index)"></image></view></view></view></view></block>
</view>
addShop(index){var that = this;that.list[index].now_count ++;var new_data = that.list[index];that.list.splice(index, 1, new_data);console.log('nnn:'+JSON.stringify(that.list));  
}

  相关解决方案