EditTable.vue 5.82 KB
Newer Older
zhengxiaokui committed
1
<template>
Wuyuqiu committed
2
  <div class="clearfix">
3
    <c-col :span="isAdd ? 24 : 24" style="margin-bottom: 18px" >
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"
      >
14
        <el-table-column v-if="isIndex" type="index" width="50"> 
zhengxiaokui committed
15
        </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
              ></el-input>
nanrui committed
35 36 37 38 39 40 41
              <el-checkbox
                v-else-if="item.show === 'checkbox'"
                v-model="scope.row[item.dataIndex]"
                :placeholder="`请输入${item.title}`"
                :disabled="disabledAll"
                checked
              ></el-checkbox>
zhengxiaokui committed
42 43 44 45 46
              <span v-else>{{ scope.row[item.dataIndex] }}</span>
            </template>
          </el-table-column>
        </template>
        <slot></slot>
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
        <el-table-column v-if="isAdd" :span="1" :offset="1" label="" prop="det" width="150px" fixed="right">
          <template slot-scope="scope" slot="header">
            <c-button
              circle
              style="padding: 4px"
              class="el-icon-plus"
              size="mini"
              @click="handleAdd(scope)"
            >
            </c-button>
            <c-button
              style="padding: 4px"
              circle
              class="el-icon-minus"
              size="mini"
              @click="handleDelete(scope)"
            >
            </c-button>
          </template>
          <template slot-scope="scoped">
            <c-button
              style="margin-left: 0"
              size="small"
              type="primary"
              icon="el-icon-info"
              @click="detail1(scoped.$index, scoped.row)"
            ></c-button>
          </template>
        </el-table-column>
      <!--div class="button_contains" >
77 78
        <span class="add_del_button add_button" @click="handleAdd" >+</span>
        <span class="add_del_button" @click="handleDelete" >-</span>
79 80 81 82 83 84 85 86
      </div-->




    

      </c-table>
zhengxiaokui committed
87
    </c-col>
88
    
zhengxiaokui committed
89
  </div>
zhengxiaokui committed
90 91
</template>
<script>
wangren committed
92
import commonProcess from "~/mixin/commonProcess";
zhengxiaokui committed
93
import _ from "~/utils/Lodash.js";
zhengxiaokui committed
94
import Table from "./Table.vue";
zhengxiaokui committed
95 96

export default {
zhengxiaokui committed
97
  components: { "c-table": Table },
zhengxiaokui committed
98
  inject: ["root"],
wangren committed
99
  mixins: [commonProcess],
zhengxiaokui committed
100 101 102 103

  data() {
    return {
      dataSource: _.get(this.model, this.urls, []),
zhengxiaokui committed
104
      rowIndex: null,
zhengxiaokui committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    };
  },

  props: {
    model: {
      type: Object,
      default: undefined,
    },
    urls: {
      //data数据所在model中的路径
      type: String,
      default: "",
    },
    columns: {
      //列信息
      type: Array,
      default: function () {
        return [];
      },
    },
    paginationShow: {
      type: Boolean,
      default: false,
    },
129 130 131 132 133
    disabledAll:{
      //是否全部灰显
      type: Boolean,
      default: false,
    },
zhengxiaokui committed
134 135 136 137 138 139

    border: {
      //是否有边框
      type: Boolean,
      default: true,
    },
zhengxiaokui committed
140 141 142 143
    highlight: {
      type: Boolean,
      default: true,
    },
zhengxiaokui committed
144 145 146 147
    isIndex: {
      type: Boolean,
      default: false,
    },
zhengxiaokui committed
148 149 150 151 152
    isAdd: {
      //添加删除列
      type: true,
      default: true,
    },
zhengxiaokui committed
153
  },
zhengxiaokui committed
154
  computed: {},
zhengxiaokui committed
155

zhengxiaokui committed
156 157 158 159 160 161 162 163 164 165 166
  // 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
167 168 169
  methods: {
    //获取表格数据索引,方便删除
    tableRowClassName({ row, rowIndex }) {
zhengxiaokui committed
170 171 172 173 174 175
      Object.defineProperty(row, "row_index", {
        value: rowIndex,
        configurable: true,
        writable: true,
        enumerable: false, //不可枚举
      });
zhengxiaokui committed
176 177
    },

zhengxiaokui committed
178
    // 记录选中行索引
zhengxiaokui committed
179
    handleClick(row, column, event) {
zhengxiaokui committed
180
      console.log(row);
zhengxiaokui committed
181
      this.rowIndex = row.row_index;
zhengxiaokui committed
182
      console.log("选择:" + this.rowIndex);
zhengxiaokui committed
183 184 185 186 187 188 189 190 191 192
    },

    handleAdd() {
      let arr = {};
      for (let it of this.columns) {
        arr[it.dataIndex] = "";
      }
      this.dataSource.push({ ...arr });
    },
    handleDelete() {
zhengxiaokui committed
193
      if (this.dataSource.length === 0) return;
zhengxiaokui committed
194 195 196 197 198 199 200 201
      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();
    },
  },
nanrui committed
202 203 204 205 206 207 208
  mounted() {
    this.$watch(() => {
      return  _.get(this.model, this.urls, [])
    },(newVal, oldVal) => {
      this.dataSource = newVal
    })
  },
zhengxiaokui committed
209 210 211 212
  created: function () {},
};
</script>
<style>
zhengxiaokui committed
213 214 215 216 217
.button_contains {
  margin-top: 18px;
  margin-left: 2px;
  display: flex;
  flex-direction: column;
Wuyuqiu committed
218
  align-items: flex-end;
zhengxiaokui committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
}
.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
239
</style>