EditTable.vue 4.49 KB
Newer Older
zhengxiaokui committed
1
<template>
zhengxiaokui committed
2
  <div>
zhengxiaokui committed
3
    <c-col :span="isAdd ? 21 : 22" style="margin-bottom: 18px" :offset="1">
zhengxiaokui committed
4 5 6 7 8 9 10 11 12 13
      <c-table
        style="text-align: center"
        :ref="urls"
        :list="dataSource"
        :paginationShow="paginationShow"
        :border="border"
        :highlight-current-row="highlight"
        :row-class-name="tableRowClassName"
        @row-click="handleClick"
      >
zhengxiaokui committed
14 15
        <el-table-column v-if="isIndex" type="index" width="50">
        </el-table-column>
zhengxiaokui committed
16 17 18 19 20 21 22 23 24 25 26
        <template v-for="item in columns">
          <el-table-column
            :label="item.title"
            :min-width="item.width"
            :key="item.dataIndex"
          >
            <template slot-scope="scope">
              <el-select
                v-if="item.show === 'select'"
                v-model="scope.row[item.dataIndex]"
                :code="item.code"
27
                :disabled="disabledAll"
zhengxiaokui committed
28 29 30 31 32
              ></el-select>
              <el-input
                v-else-if="item.show === 'input'"
                v-model="scope.row[item.dataIndex]"
                :placeholder="`请输入${item.title}`"
33
                :disabled="disabledAll"
zhengxiaokui committed
34 35 36 37 38 39 40 41
              ></el-input>
              <span v-else>{{ scope.row[item.dataIndex] }}</span>
            </template>
          </el-table-column>
        </template>
        <slot></slot>
      </c-table>
    </c-col>
zhengxiaokui committed
42
    <c-col v-if="isAdd" :span="1">
43 44 45
      <div class="button_contains" >
        <span class="add_del_button add_button" @click="handleAdd" >+</span>
        <span class="add_del_button" @click="handleDelete" >-</span>
zhengxiaokui committed
46 47
      </div>
    </c-col>
zhengxiaokui committed
48
  </div>
zhengxiaokui committed
49 50
</template>
<script>
wangren committed
51
import commonProcess from "~/mixin/commonProcess";
zhengxiaokui committed
52
import _ from "~/utils/Lodash.js";
zhengxiaokui committed
53
import Table from "./Table.vue";
zhengxiaokui committed
54 55

export default {
zhengxiaokui committed
56
  components: { "c-table": Table },
zhengxiaokui committed
57
  inject: ["root"],
wangren committed
58
  mixins: [commonProcess],
zhengxiaokui committed
59 60 61 62

  data() {
    return {
      dataSource: _.get(this.model, this.urls, []),
zhengxiaokui committed
63
      rowIndex: null,
zhengxiaokui committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    };
  },

  props: {
    model: {
      type: Object,
      default: undefined,
    },
    urls: {
      //data数据所在model中的路径
      type: String,
      default: "",
    },
    columns: {
      //列信息
      type: Array,
      default: function () {
        return [];
      },
    },
    paginationShow: {
      type: Boolean,
      default: false,
    },
88 89 90 91 92
    disabledAll:{
      //是否全部灰显
      type: Boolean,
      default: false,
    },
zhengxiaokui committed
93 94 95 96 97 98

    border: {
      //是否有边框
      type: Boolean,
      default: true,
    },
zhengxiaokui committed
99 100 101 102
    highlight: {
      type: Boolean,
      default: true,
    },
zhengxiaokui committed
103 104 105 106
    isIndex: {
      type: Boolean,
      default: false,
    },
zhengxiaokui committed
107 108 109 110 111
    isAdd: {
      //添加删除列
      type: true,
      default: true,
    },
zhengxiaokui committed
112
  },
zhengxiaokui committed
113
  computed: {},
zhengxiaokui committed
114

zhengxiaokui committed
115 116 117 118 119 120 121 122 123 124 125
  // watch: {
  //   dataSource: {
  //     handler(val, oldVal) {
  //       console.log("数据变化:", val, this.model);
  //       // _.set(this.model, this.urls, val);
  //       // console.log(this.model);
  //     },
  //     immediate: true,
  //     deep: true,
  //   },
  // },
zhengxiaokui committed
126 127 128
  methods: {
    //获取表格数据索引,方便删除
    tableRowClassName({ row, rowIndex }) {
zhengxiaokui committed
129 130 131 132 133 134
      Object.defineProperty(row, "row_index", {
        value: rowIndex,
        configurable: true,
        writable: true,
        enumerable: false, //不可枚举
      });
zhengxiaokui committed
135 136
    },

zhengxiaokui committed
137
    // 记录选中行索引
zhengxiaokui committed
138
    handleClick(row, column, event) {
zhengxiaokui committed
139
      console.log(row);
zhengxiaokui committed
140
      this.rowIndex = row.row_index;
zhengxiaokui committed
141
      console.log("选择:" + this.rowIndex);
zhengxiaokui committed
142 143 144 145 146 147 148 149 150 151
    },

    handleAdd() {
      let arr = {};
      for (let it of this.columns) {
        arr[it.dataIndex] = "";
      }
      this.dataSource.push({ ...arr });
    },
    handleDelete() {
zhengxiaokui committed
152
      if (this.dataSource.length === 0) return;
zhengxiaokui committed
153 154 155 156 157 158 159 160
      if (this.rowIndex === null) {
        this.rowIndex = this.dataSource[this.dataSource.length - 1].row_index; //没有选中删除最后一行
      }
      this.dataSource.splice(this.rowIndex, 1);
      this.rowIndex = null;
      //this.$refs[this.urls].clearSelection();
    },
  },
zhengxiaokui committed
161 162 163 164 165

  created: function () {},
};
</script>
<style>
zhengxiaokui committed
166 167 168 169 170
.button_contains {
  margin-top: 18px;
  margin-left: 2px;
  display: flex;
  flex-direction: column;
zhengxiaokui committed
171
  /* text-align: right; */
zhengxiaokui committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
}
.add_del_button {
  display: block;
  height: 15px;
  width: 26px;
  flex: 0;
  line-height: 15px;
  text-align: center;
  font-size: 12px;
  border-radius: 5px;
  border: 1px solid rgb(194, 192, 192);
  background-color: rgb(240, 240, 240);
}
.add_button {
  margin-bottom: 2px;
}
.add_del_button:hover {
  cursor: pointer;
  background-color: rgb(230, 227, 227);
}
zhengxiaokui committed
192
</style>