EditTable.vue 4.32 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 27 28 29 30 31 32 33 34 35 36 37 38 39
        <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"
              ></el-select>
              <el-input
                v-else-if="item.show === 'input'"
                v-model="scope.row[item.dataIndex]"
                :placeholder="`请输入${item.title}`"
              ></el-input>
              <span v-else>{{ scope.row[item.dataIndex] }}</span>
            </template>
          </el-table-column>
        </template>
        <slot></slot>
      </c-table>
    </c-col>
zhengxiaokui committed
40
    <c-col v-if="isAdd" :span="1">
zhengxiaokui committed
41 42 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>
      </div>
    </c-col>
zhengxiaokui committed
46
  </div>
zhengxiaokui committed
47 48
</template>
<script>
wangren committed
49
import commonProcess from "~/mixin/commonProcess";
zhengxiaokui committed
50
import _ from "~/utils/Lodash.js";
zhengxiaokui committed
51
import Table from "./Table.vue";
zhengxiaokui committed
52 53

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

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

  props: {
    model: {
      type: Object,
      default: undefined,
    },
    urls: {
      //data数据所在model中的路径
      type: String,
      default: "",
    },
    columns: {
      //列信息
      type: Array,
      default: function () {
        return [];
      },
    },
    paginationShow: {
      type: Boolean,
      default: false,
    },

    border: {
      //是否有边框
      type: Boolean,
      default: true,
    },
zhengxiaokui committed
92 93 94 95
    highlight: {
      type: Boolean,
      default: true,
    },
zhengxiaokui committed
96 97 98 99
    isIndex: {
      type: Boolean,
      default: false,
    },
zhengxiaokui committed
100 101 102 103 104
    isAdd: {
      //添加删除列
      type: true,
      default: true,
    },
zhengxiaokui committed
105
  },
zhengxiaokui committed
106
  computed: {},
zhengxiaokui committed
107

zhengxiaokui committed
108 109 110 111 112 113 114 115 116 117 118
  // 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
119 120 121
  methods: {
    //获取表格数据索引,方便删除
    tableRowClassName({ row, rowIndex }) {
zhengxiaokui committed
122 123 124 125 126 127
      Object.defineProperty(row, "row_index", {
        value: rowIndex,
        configurable: true,
        writable: true,
        enumerable: false, //不可枚举
      });
zhengxiaokui committed
128 129
    },

zhengxiaokui committed
130
    // 记录选中行索引
zhengxiaokui committed
131
    handleClick(row, column, event) {
zhengxiaokui committed
132
      console.log(row);
zhengxiaokui committed
133
      this.rowIndex = row.row_index;
zhengxiaokui committed
134
      console.log("选择:" + this.rowIndex);
zhengxiaokui committed
135 136 137 138 139 140 141 142 143 144
    },

    handleAdd() {
      let arr = {};
      for (let it of this.columns) {
        arr[it.dataIndex] = "";
      }
      this.dataSource.push({ ...arr });
    },
    handleDelete() {
zhengxiaokui committed
145
      if (this.dataSource.length === 0) return;
zhengxiaokui committed
146 147 148 149 150 151 152 153
      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
154 155 156 157 158

  created: function () {},
};
</script>
<style>
zhengxiaokui committed
159 160 161 162 163
.button_contains {
  margin-top: 18px;
  margin-left: 2px;
  display: flex;
  flex-direction: column;
zhengxiaokui committed
164
  /* text-align: right; */
zhengxiaokui committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
}
.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
185
</style>