vue——table

知识点

  1. 显示table数据id列,直接是type=“index”
<el-table-column type="index" width="50"></el-table-column>
  1. 过滤器
<el-table-column label="价格">
		<template slot-scope="scope">
	    	 <span>{{scope.row.price | showPrice}}</span>
	   </template>
 </el-table-column>
 filters:{
    showPrice(price){
      return "¥"+price.toFixed(2)
    }
  }
  1. 列加图标
<el-table-column label="购买数量">
   <template slot-scope="scope">
     <i class="el-icon-minus" @click="decrement(scope.$index)" :disabled="scope.row.num<=1"></i>
     <span style="margin: 0 10px">{{ scope.row.num }}</span>
     <i class="el-icon-plus" @click="increment(scope.$index)"></i>
   </template>
 </el-table-column>
 decrement(index){
  if(this.tableData[index].num>1){
     this.tableData[index].num--
   }
 },
 increment(index){
   this.tableData[index].num++
 }
  1. slot-scope中取index
scope.$index
<i class="el-icon-minus" @click="decrement(scope.$index)" :disabled="scope.row.num<=1"></i>
  1. disabled绑定
:disabled="scope.row.num<=1"(里面是true或false)
<i class="el-icon-minus" @click="decrement(scope.$index)" :disabled="scope.row.num<=1"></i>
  1. 计算总价(计算属性)
computed: {
//计算总价
    totalPrice() {
	let totalPrice = 0;
	
	//循环的第一种写法
	  /* this.tableData.forEach((el) => {
	    totalPrice += el.price * el.num;
	  }); */
	  
	//循环的第二种写法
	/* for (let item of this.tableData) {
	 	totalPrice += el.price * el.num;
	} */
	//循环的第三种写法(函数式编程)
	totalPrice = this.tableData.reduce( (p, arr) => {
	  return p + arr.price*arr.num;
	},0);
	return totalPrice;
},

template

<template>
  <div>
   <h1>table</h1>
    <div v-if="tableData.length">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="name" label="书籍名称"></el-table-column>
        <el-table-column prop="date" label="出版日期"></el-table-column>
        <el-table-column label="价格">
          <template slot-scope="scope">
            <span>{{scope.row.price | showPrice}}</span>
          </template>
        </el-table-column>
        <el-table-column label="购买数量">
          <template slot-scope="scope">
            <i class="el-icon-minus" @click="decrement(scope.$index)" :disabled="scope.row.num<=1"></i>
            <span style="margin: 0 10px">{{ scope.row.num }}</span>
            <i class="el-icon-plus" @click="increment(scope.$index)"></i>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small"
            >移除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <h2>总价格:{{totalPrice | showPrice}}</h2>
    </div>
    <h2 v-else>购物车为空</h2>
  </div>
</template>

js

<script>
export default {
  data() {
    return {
      tableData: [
        {
          name: "算法导论",
          date: "2006-9",
          price: 85.0,
          num: 1,
        },
        {
          name: "算法导论1",
          date: "2006-10",
          price: 120.0,
          num: 1,
        },
        {
          name: "算法导论2",
          date: "2020-5",
          price: 45.0,
          num: 1,
        },
        {
          name: "算法导论3",
          date: "2016-9",
          price: 30.0,
          num: 1,
        },
      ],
    };
  },
  methods: {
    decrement(index){
      if(this.tableData[index].num>1){
        this.tableData[index].num--
      }
    },
    increment(index){
      this.tableData[index].num++
    }
  },
  filters:{
    showPrice(price){
      return "¥"+price.toFixed(2)
    }
  },
  computed: {
    //计算总价
    totalPrice() {
      let totalPrice = 0;
      //循环的第一种写法
      /* this.tableData.forEach((el) => {
        totalPrice += el.price * el.num;
      }); */
      //循环的第二种写法
      /* for (let item of this.tableData) {
        totalPrice += el.price * el.num;
      } */
      //循环的第三种写法
      //数组高阶函数
      //编程范式:命令式编程(按步骤来)
      //声明式编程(从数据进入,数据再利用)

      //编程范式:面向对象编程(第一公民:对象)
      // 函数式编程(第一公民:函数)
      //filter/map/reduce
      // 案例
      /* let tAtt = [10,20,102,105];
      //选出小于100的元素
      let re = tAtt.filter(item=>item<100).map(item=>item*2).reduce((p,c)=>p+c)
      console.log(re); */
      // 都乘以2
      // 相加
      totalPrice = this.tableData.reduce( (p, arr) => {
        return p + arr.price*arr.num;
      },0);
      return totalPrice;
    },
  },
};
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值