蓝桥到家第三课

2022-10-07  本文已影响0人  风中凌乱的男子

接第二课继续往下说,下面讲调用批量删除接口删除商品

/**
 * /api/del/all/goods 批量删除商品
 */
 export function delAllShopFun(data) {
  return request({
    url: '/api/del/all/goods',
    method: 'delete',
    data
  })
}
import { delAllShopFun } from "@/api/user";
{
   "ids":["62f5f109c30baf69c6bd8c61","62f5f10ac30baf69c6bd8c63"]
}
image.png
<el-table
     :data="tableData"
     style="width: 100%"
     size="small"
     border
     @selection-change="handleSelectionChange"
     :height="tableHeight"
     >
     ......
     ......
     ......
</el-table>
methods:{
  handleSelectionChange(e){ 
      console.log(e);
  }
}
image.png
data() {
    return {
      ids:[],//定义一个空数组
    };
  },
handleSelectionChange(e){
      console.log(e);
      this.ids = e.map(item=>{
        return item._id //返回每条数据的_id
      })
      console.log(this.ids);
    }
<el-button type="danger" size="small" icon="el-icon-delete" @click="handelDelAll">批量删除</el-button>
handelDelAll(){
      if(this.ids.length==0){
        alert("请先选中要删除的商品")
        return
      }
      delAllShopFun({ids:this.ids}).then(res=>{
        alert("删除成功")
        this.getShopListFun()
      })
    }

继续往下说,下面讲实现switch开发控制商品上下架,调用update接口

change  switch 状态发生变化时的回调函数
<el-switch
   v-model="scope.row.status"
   :active-value="1"
   :inactive-value="2"
   active-color="#13ce66"
   inactive-color="#eee"
   @change="changeSwitch"
   >
 </el-switch>
changeSwitch(e){
   console.log(e);
}
image.png
/**
 * /api/update/goods/:id 更新商品信息
 */
 export function updateShopFun(data,id) {
  return request({
    url: '/api/update/goods/'+id,
    method: 'put',
    data
  })
}
import { updateShopFun } from "@/api/user";
<el-switch
   v-model="scope.row.status"
   :active-value="1"
   :inactive-value="2"
   active-color="#13ce66"
   inactive-color="#eee"
   @change="changeSwitch(scope.row)"
   >
 </el-switch>
changeSwitch(e){
      console.log(e);
      updateShopFun({status:e.status},e._id).then(res=>{
        alert('更新成功')
      })
    }
上一篇 下一篇

猜你喜欢

热点阅读