SpecsSelect.vue 3.97 KB
Newer Older
张成 committed
1 2 3 4 5
<template>
  <div>
    <el-table :data="list" :row-key="(row) => row.id">
      <el-table-column label="默认" width="50" align="center" prop="isDefault">
        <template slot-scope="scope">
lixiaomin committed
6
          <el-checkbox @change="(e) => changeDefault(e, scope.$index)"
lixiaomin committed
7
            v-model="scope.row.isDefault" :checked="scope.row.isDefault===1" true-label="1" false-label="0"></el-checkbox>
张成 committed
8 9 10 11 12 13 14
        </template>
      </el-table-column>
      <el-table-column label="推荐" width="50" align="center" prop="isRecommend">
        <template slot-scope="scope">
          <el-checkbox true-label="1" false-label="0" v-model="scope.row.isRecommend"></el-checkbox>
        </template>
      </el-table-column>
lixiaomin committed
15
      <el-table-column label="名称" width="120" align="center" prop="name">
张成 committed
16
        <template slot-scope="scope">
lixiaomin committed
17
          <el-input placeholder="名称" v-model="scope.row.name" wi></el-input>
张成 committed
18 19
        </template>
      </el-table-column>
lixiaomin committed
20
      <el-table-column label="价格" width="120" align="center" prop="amount">
张成 committed
21 22 23 24 25 26 27 28 29 30 31
        <template slot-scope="scope">
          <el-input placeholder="价格" v-model="scope.row.amount"> </el-input>
        </template>
      </el-table-column>

      <el-table-column label="原料" align="center" prop="specRuleMaterials">
        <template slot-scope="scope">
          <div @click="addMaterial(scope.$index)" v-if="
            scope.row.specRuleMaterials &&
            scope.row.specRuleMaterials.length > 0
          ">
lixiaomin committed
32 33
            <span v-for="item in scope.row.specRuleMaterials" :key="item.id" style="color:blue">
              {{ item.materialName }}* {{ item.quantity || "" }}
张成 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            </span>
          </div>
          <el-button v-else type="primary" @click="addMaterial(scope.$index)" size="mini">添加原料
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.$index)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" class="add" @click="add">添加</el-button>
    <SelectMaterial ref="SelectMaterial" @callback="selectMaterial" />
  </div>
</template>

<script>
import SelectMaterial from "@/components/SelectMaterial";
export default {
  components: { SelectMaterial },
  props: ["initData"],
  watch: {
    initData(data) {
      this.list = data;
    },
  },
  data() {
    return {
      list: [],
      index: "",
    };
  },
  mounted() {
    this.list = this.initData;
  },
  methods: {
    changeDefault(def, data) {
      this.list = this.list.map((item, index) => {
        item.isDefault = "0";
        if (def == "1" && data == index) item.isDefault = "1";
        return item;
      });
    },
    handleDelete(index) {
      this.list.splice(index, 1);
lixiaomin committed
81
      if(this.list.length>=1){
lixiaomin committed
82 83
        for(let i=0;i<this.list.length;i++){
          if(i==0){
lixiaomin committed
84
             this.$set(this.list[i],"isDefault","1")
lixiaomin committed
85 86 87 88
            return;
          }
        }
      }
张成 committed
89 90 91 92 93 94 95 96 97 98 99
    },
    addMaterial(index) {
      this.index = index;
      this.$refs.SelectMaterial.openModal();
    },
    selectMaterial(data) {
      this.list[this.index].specRuleMaterials = JSON.parse(
        JSON.stringify(data)
      );
    },
    add() {
lixiaomin committed
100
      let obj={
张成 committed
101 102 103
        name: "",
        amount: "",
        specRuleMaterials: [],
lixiaomin committed
104
        isDefault:""
lixiaomin committed
105
      }
lixiaomin committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      if(this.list.length==0){
        obj.isDefault="1";
        this.list.push(obj);
      }else{
        let p=0;       
        for(let i=0;i<this.list.length;i++){
          if(this.list[i].isDefault=="1"){        
            p=1;
            this.list.push(obj);
            return;
          }
        }
        //没有选择默认项,默认第一条
        if(p==0){
         this.$set(this.list[0],"isDefault","1")
        }              
lixiaomin committed
122
      }
lixiaomin committed
123
    }, 
张成 committed
124 125 126 127 128 129 130 131 132 133 134 135
    clearMaterial() {
      this.list = [];
    },
  },
};
</script>

<style>
.add {
  margin-top: 10px;
}
</style>