edit-table.vue 18.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
<!--
 * @Author: lide1202@hotmail.com
 * @Date: 2021-5-4 11:04:24
 * @Last Modified by:   lide1202@hotmail.com
 * @Last Modified time: 2021-5-6 11:04:24
 !-->
<template>
  <div v-if="option['hide'] == null || option['hide'] == false"
       class="anji-card">
    <div class="card-head">{{ option.title }}</div>
    <div class="card-body">
      <el-form ref="form"
               :model="form">
        <!-- 表格开始 -->
        <el-table :data="formRecordsUndelete"
                  border
                  :row-class-name="tableRowClassAdapter"
                  @selection-change="handleSelectionChange"
                  @row-click="handleTableRowClick">
          <!-- <el-table-column fixed type="selection" width="50" align="center" /> -->

          <el-table-column label="序号"
                           min-width="50"
                           align="center">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>

          <template v-for="item in option.columns">
            <el-table-column v-if="fieldIsHide(item.tableHide) != true && item.columnType != 'expand'"
                             :key="item.field"
                             :label="item.label"
                             :min-width="item.minWidth || 110"
                             align="center">
              <template slot-scope="scope">
                <el-form-item :prop="'records.' + scope.$index + '.' + item.field"
                              :rules="item.rules">
                  <!-- 输入框 -->
                  <el-input v-if="item.inputType == 'input'"
                            v-model="scope.row[item.field]"
                            size="small"
                            :placeholder="item.placeholder || '请输入'"
                            :clearable="item.clearable !== false"
                            :disabled="saveButtonStatus[scope.$index] == 'inShow' || item.disabled"
                            @change="(value) => tableRowChange(scope.$index, item.field, value)" />
                  <!-- 开关 -->
                  <el-switch v-else-if="item.inputType == 'switch'"
                             v-model="scope.row[item.field]"
                             :disabled="saveButtonStatus[scope.$index] == 'inShow' || item.disabled"
                             :active-value="item.switchOption.disableValue"
                             :inactive-value="item.switchOption.enableValue"
                             @change="(value) => tableRowChange(scope.$index, item.field, value)"
                             active-color="#5887fb"
                             inactive-color="#ccc">
                  </el-switch>
                  <el-input v-else-if="item.inputType == 'input-number'"
                            v-model="scope.row[item.field]"
                            size="small"
                            :min="item.inputNumberOption.min"
                            :max="item.inputNumberOption.max"
                            :placeholder="item.placeholder || '请输入'"
                            :clearable="item.clearable !== false"
                            :disabled="saveButtonStatus[scope.$index] == 'inShow' || item.disabled"
                            @change="(value) => tableRowChange(scope.$index, item.field, value)" />

                  <!-- 自定义input -->
                  <anji-input v-else-if="item.inputType == 'anji-input'"
                              v-model.trim="scope.row[item.field]"
                              :default-value="item.defaultValue"
                              :unit="item.anjiInput.unit"
                              :conversion="item.anjiInput.conversion"
                              :placeholder="item.placeholder || '请输入'"
                              :clearable="item.clearable !== false"
                              :disabled="saveButtonStatus[scope.$index] == 'inShow' || item.disabled"
                              @change="(value) => tableRowChange(scope.$index, item.field, value)" />

                  <!-- 下拉框 -->
                  <anji-select v-else-if="item.inputType == 'anji-select'"
                               v-model.trim="scope.row[item.field]"
                               :multiple="item.anjiSelectOption.multiple"
                               :default-value="item.defaultValue"
                               :dict-code="item.anjiSelectOption.dictCode"
                               :url="item.anjiSelectOption.url"
                               :method="item.anjiSelectOption.method"
                               :query-param="item.anjiSelectOption.queryParam"
                               :option="item.anjiSelectOption.option"
                               :label="item.anjiSelectOption.label"
                               :disabled-options="item.anjiSelectOption.disabledOptions"
                               :disabled="saveButtonStatus[scope.$index] == 'inShow' || item.disabled"
                               @change="(value, option) => tableRowChange(scope.$index, item.field, value, option)" />
                  <!-- 日期时间框  -->
                  <el-date-picker v-else-if="item.inputType.indexOf('date') >= 0"
                                  v-model="scope.row[item.field]"
                                  style="width: 100%"
                                  :placeholder="item.placeholder || '请选择'"
                                  :type="item.inputType"
                                  :clearable="item.clearable !== false"
                                  :disabled="saveButtonStatus[scope.$index] == 'inShow' || item.disabled"
                                  @change="(value) => tableRowChange(scope.$index, item.field, value)" />
                  <!-- 待扩展的表单类型,请自行扩展 -->
                  <el-input v-else
                            placeholder="组件不支持此类型表单请至组件内部自行扩展"
                            disabled />
                </el-form-item>
              </template>
            </el-table-column>
          </template>
          <el-table-column fixed="right"
                           label="操作"
                           width="100">
            <template slot-scope="scope">
              <el-button type="text"
                         size="small"
                         @click="handleAddOrUpdate(scope.row, scope.$index)">{{ getRowEditButton(scope.$index) }}</el-button>
              <el-button type="text"
                         size="small"
                         @click="handleDelete(scope.row, scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 表格结束 -->
      </el-form>
      <button v-if="modelType != 'view'"
              class="table-add-row-button"
              @click="handleAdd">
        <i class="el-icon-plus" />
        <span>新增</span>
      </button>
    </div>
  </div>
</template>

<script>
const ROW_DELETE_FLAG = 'deletedFlag'
export default {
  components: {},
  props: {
    modelType: String, // add view edit
    option: {
      // 界面渲染相关配置json
      type: [Object],
      default: () => {
        return {
          title: '', // 页面标题
          labelWidth: '',
          queryFormFields: [], // 查询表单条件
          buttons: {
            // 按钮
            query: {},
            edit: {},
            delete: {},
            add: {},
          },
          columns: [], // 表格列
        }
      },
    },
    relateData: {
      // 关联的主记录
      type: [Object],
      default: () => {
        return {}
      },
    },
    value: {
      type: [Array],
      default: () => {
        return []
      },
    },
    valueNew: {
      type: [Array],
      default: () => {
        return []
      },
    }
  },
  data () {
    return {
      checkRecords: [], // 表格中当前选中的记录

      form: {
        records: [], // 接口返回的记录列表
        total: 0, // 接口返回的总条数
      },

      saveButtonStatus: [], // 维护表格中每行编辑按钮的状态 inShow inEditing inAdding

      rowIdList: []
    }
  },
  computed: {
    // 主键的列名
    primaryKeyFieldName () {
      var primaryKey = this.option.columns.find((item) => item['primaryKey'] == true)
      if (primaryKey != null) {
        return primaryKey['field']
      } else {
        return null
        console.warn('在columns中查找primaryKey=true失败,会导致查询详情和删除失败')
      }
    },
    // 指定当前实体关联主表的关联字段,孙子关联表,没有该属性
    joinColumn () {
      var columnName = this.option.joinColumn
      if (this.isBlank(columnName)) {
        console.warn('在columns中查找关联字段失败,会导致查询详情和删除失败,孙子关联表忽略该错误')
        columnName = ''
      }
      return columnName
    },
    // 未删除的记录
    formRecordsUndelete () {
      if (this.form.records == null) {
        return []
      }
      return this.form.records.filter((item) => item[ROW_DELETE_FLAG] == null || item[ROW_DELETE_FLAG] == false)
    },
  },
  watch: {},
  created () {
    // 主表 relateData 的关联字段 joinColumn 变动时,触发查询子表的查询,孙子关联儿子的无效
    if (this.isNotBlank(this.joinColumn)) {
      this.$watch(
        function () {
          return this.relateData[this.joinColumn]
        },
        function (newVal, oldVal) {
          // 如果是关联字段发生更新,触发查询
          if (this.isNotBlank(newVal)) {
            this.handleQueryPageList(newVal)
          } else {
            // 如果关联字段为空,清空本表格的数据,如果是父组件(弹出框)关闭时,设置this.relateData = {默认值}时触发
            this.checkRecords = []
            this.form.records = []
            this.form.total = 0
            this.saveButtonStatus = []
          }
        }
      )
    }
  },
  mounted () {
    // 首次打开时,根据主表关联字段查询子表,加载表格数据
    if (this.isNotBlank(this.relateData) && this.isNotBlank(this.relateData[this.joinColumn])) {
      this.handleQueryPageList()
    }
  },
  methods: {
    // 该行是否显示 true/false/ 'hideOnAdd hideOnView hideOnEdit'
    fieldIsHide (tableHide) {
      if (typeof tableHide == 'boolean') {
        return tableHide
      }
      if (typeof tableHide == 'string') {
        if (this.modelType == 'add') {
          return tableHide.indexOf('hideOnAdd') >= 0
        }
        if (this.modelType == 'view') {
          return tableHide.indexOf('hideOnView') >= 0
        }
        if (this.modelType == 'edit') {
          return tableHide.indexOf('hideOnEdit') >= 0
        }
      }
      return false
    },
    // 获取行的提交按钮文字
    getRowEditButton (index) {
      if (this.saveButtonStatus[index] == 'inEditing') {
        return 'btn_savetemp'
      } else if (this.saveButtonStatus[index] == 'inAdding') {
        return 'btn_savetemp'
      } else if (this.saveButtonStatus[index] == 'inShow') {
        return 'btn_edit'
      } else {
        return 'not_permission'
      }
    },
    // 表格行渲染前前置处理
    tableRowClassAdapter ({ row, rowIndex }) {
      row.index = rowIndex
    },
    // 查询
    async handleQueryPageList (joinColumnValue) {
      if (this.isBlank(joinColumnValue)) {
        joinColumnValue = this.relateData[this.joinColumn]
      }
      var params = {}
      params[this.joinColumn] = joinColumnValue
      this.queryPageList(params)
    },
    // 暴露给外部直接调用带参数
    async queryPageList (params) {
      // 默认的排序
      if (this.isNotBlank(this.option.buttons.query.order)) {
        params['sort'] = this.option.buttons.query.sort
        params['order'] = this.option.buttons.query.order
      }
      const { data, code } = await this.option.buttons.query.api(params)
      if (code != '200') return
      this.form.records = data.records
      this.form.total = data.total
      this.$emit('input', this.form.records)
      for (var i = 0; i < this.form.total; i++) {
        this.saveButtonStatus.push('inShow')
      }
    },
    // 选择项改变时
    handleSelectionChange (val) {
      this.checkRecords = val
    },
    // 表格选中某一行时
    handleTableRowClick (row, column, event) {
      // console.log(row)
      // console.log(column)
      // 行点击后,回调option中的tableRowClick事件
      if (typeof this.option.tableRowClick == 'function') {
        this.option.tableRowClick(this.form.records, row, row.index, this.relateData)
      }
    },
    // 行数据更新时回调
    tableRowChange (rowIndex, fieldName, fieldVal, fieldExtend) {
      // 通知外面的组件
      this.$emit('input', this.form.records)

      // 表单变动后,回调option中的tableRowChange事件
      if (typeof this.option.tableChange == 'function') {
        this.option.tableChange(this.form.records, rowIndex, fieldName, fieldVal, fieldExtend, this.relateData)
      }
    },
    // 新增
    handleAdd () {
      this.saveButtonStatus.push('inAdding')
      this.form.records.push({})
    },
    // 父节点Change,子节点表格更新
    handleUpdata () {
      this.saveButtonStatus = []
      this.form.records = []
    },
    // 提交和修改
    handleAddOrUpdate (row, index) {
      // 编辑状态下点击保存提交
      if (this.saveButtonStatus[index] == 'inEditing' || this.saveButtonStatus[index] == 'inAdding') {
        this.handleSaveTemp(row, index)
      } else {
        this.$set(this.saveButtonStatus, index, 'inEditing')
      }
    },
    // 校验表单
    validate (callback) {
      this.$refs['form'].validate(async (valid, obj) => {
        if (callback != null) {
          callback(valid)
        }
      })
    },
    // 暂存
    async handleSaveTemp (row, index) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.isBlank(row[this.primaryKeyFieldName])) {
            // 补全关联属性
            if (typeof this.option.beforeInsert == 'function') {
              this.option.beforeInsert(this.relateData, row)
            }
          } else {
            // 补全关联属性
            if (typeof this.option.beforeUpdate == 'function') {
              this.option.beforeUpdate(this.relateData, row)
            }
          }
          // 将行按钮的文字改成编辑
          this.$set(this.saveButtonStatus, index, 'inShow')
          // 通知外面的组件
          this.$emit('input', this.form.records)
        }
      })
      /*
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 验证通过
          if (this.isBlank(row[this.primaryKeyFieldName])) {
            // 补全关联属性
            if (typeof this.option.beforeInsert == 'function') {
              this.option.beforeInsert(this.relateData, row)
            }
            // 主键为空,新增
            this.option.buttons.add.api(row).then((response) => {
              if (response.code == 200) {
                this.$set(this.saveButtonStatus, index, 'inShow')
                // 保存完成后刷新列表
                this.handleQueryPageList()
              }
            })
          } else {
            // 补全关联属性
            if (typeof this.option.beforeUpdate == 'function') {
              this.option.beforeUpdate(this.relateData, row)
            }
            // 修改
            this.option.buttons.edit.api(row).then((response) => {
              if (response.code == 200) {
                this.$set(this.saveButtonStatus, index, 'inShow')
                // 保存完成后刷新列表
                this.handleQueryPageList()
              }
            })
          }
        } else {
          console.log('valid fail')
          return false
        }
      })
      */
    },
    // 删除
    handleDelete (row, index) {
      this.saveButtonStatus.splice(index, 1) // 清空状态
      // 界面上临时新增出来的一行,还没有提交到数据库,可以直接删除
      if (this.saveButtonStatus[index] == 'inAdding') {
        this.form.records.splice(index, 1)
        this.saveButtonStatus.splice(index, 1)
        this.$emit('input', this.form.records)
        return
      }
      // if (this.isBlank(row) || this.isBlank(row[this.primaryKeyFieldName])) {
      //   return
      // }
      // 将对应的行标识成删除
      // 找出该行在原始记录中的index
      // var realIndex = this.form.records.findIndex((item) => item[this.primaryKeyFieldName] == row[this.primaryKeyFieldName])
      // row[ROW_DELETE_FLAG] = true
      // this.$set(this.form.records, realIndex, row)
      // this.$emit('input', this.form.records)
      this.form.records.splice(index, 1)
      this.rowIdList.push(row.id)
      this.$emit('input', this.form.records)
      this.$emit('update:valueNew', this.rowIdList)
      /*
      之前是直接调用接口删除,后面统一改成在主表接口中增加、更新、删除
      // 已经保存在数据库的的行,要调用删除按钮
      var primaryKey = row[this.primaryKeyFieldName]
      this.$confirm(this.$lang('promptMessage_deleteTip'), this.$lang('promptMessage_deleteTipTitle'), {
        type: 'warning',
        confirmButtonClass: 'delete_sure',
        cancelButtonClass: 'el-button--danger is-plain',
      })
        .then(() => {
          this.option.buttons.delete.api(primaryKey).then((res) => {
            // {code: "200", message: "操作成功", data: true}
            this.checkRecords = []
            this.handleQueryPageList()
          })
        })
        .catch((e) => {
          e
        })
        */
    },
  },
}
</script>

<style scoped lang="scss">
.table-add-row-button {
  width: 100%;
  margin-top: 0px;
  margin-bottom: 0px;
  border-color: #d9d9d9;
  border-style: dashed;
  line-height: 1.499;
  position: relative;
  display: inline-block;
  font-weight: 400;
  white-space: nowrap;
  text-align: center;
  background-image: none;
  border: 1px solid transparent;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.015);
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -ms-touch-action: manipulation;
  touch-action: manipulation;
  height: 32px;
  padding: 0 15px;
  font-size: 14px;
  border-radius: 4px;
  color: rgba(0, 0, 0, 0.65);
  background-color: #fff;
  border-color: #d9d9d9;
}
</style>