index.vue 5.01 KB
<template>
  <div class="table-content">
    <!-- 采用的原生表格 -->
    <table align="center">
      <thead>
        <tr>
          <th></th>
          <th>Key</th>
          <th>Value</th>
          <th>是否必须</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in tableArray.content" :key="index">
          <td>
            {{ index + 1 }}
          </td>
          <td>
            <Select
              v-model:value="item.key"
              placeholder="请选择"
              style="width: 14rem"
              :options="selectOptions"
              @change="handleChange(item.key)"
              allowClear
            />
          </td>
          <td>
            <a-input
              :disabled="item.editDisabled"
              placeholder="请输入"
              v-model:value="item.value"
              style="width: 14rem"
            />
          </td>
          <td>
            <a-switch v-model:checked="item.required" />
          </td>
          <td>
            <div>
              <Button type="dashed" @click="add(item, index)">
                <template #icon><PlusOutlined /></template
              ></Button>
              <Button type="dashed" style="margin-left: 5px" @click="remove(item, index)">
                <template #icon> <MinusOutlined /></template>
              </Button>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
<script lang="ts" setup name="editCellTable">
  import { reactive, ref, onMounted, nextTick } from 'vue';
  import { Select, Button } from 'ant-design-vue';
  import { findDictItemByCode } from '/@/api/system/dict';
  import { PlusOutlined, MinusOutlined } from '@ant-design/icons-vue';

  const props = defineProps({
    method: {
      type: String,
    },
  });

  onMounted(() => {
    getSelectOptions();
  });

  const getSelectOptions = async () => {
    const res = await findDictItemByCode({ dictCode: 'dataview_builtin_params' });
    selectOptions.value = res.map((m) => ({ label: m.itemText, value: m.itemValue }));
    selectOptions.value.push({
      label: '自定义',
      value: 'scope',
    });
    if (props.method === '2')
      selectOptions.value = selectOptions.value.filter((f) => f.value !== 'scope');
  };

  const selectOptions = ref<{ label: string; value: string; disabled?: boolean }[]>([]);

  type defaultItem = {
    key: null | string;
    value: string;
    editDisabled: boolean;
    required: boolean;
  };

  const tableArray = reactive<{
    content: defaultItem[];
  }>({
    content: [
      {
        key: null,
        value: '',
        editDisabled: false,
        required: false,
      },
    ],
  });

  // 新增
  const add = (_, index: number) => {
    tableArray.content.splice(index + 1, 0, {
      key: null,
      value: '',
      editDisabled: false,
      required: false,
    });
  };

  // 减少
  const remove = (item, index: number) => {
    if (tableArray.content.length !== 1) {
      selectOptions.value.forEach((ele) => {
        if (ele.value == item.key) {
          ele.disabled = false;
        }
      });
      tableArray.content.splice(index, 1);
    }
  };

  //Select互斥
  const handleChange = (value: string) => {
    console.log(value);
    selectOptions.value.forEach((ele) => {
      ele.disabled = false;
      tableArray.content.forEach((element) => {
        if (element.key === 'scope') {
          element.editDisabled = false;
        } else {
          element.editDisabled = true;
        }
        if (element.key === ele.value && element.key !== 'scope') {
          ele.disabled = true;
        }
      });
    });
  };

  //获取数据
  const getValue = () => {
    return tableArray.content;
  };

  //设置数据
  const setValue = (data) => {
    nextTick(() => (tableArray.content = data));
    nextTick(() =>
      setTimeout(() => {
        tableArray.content.forEach((it: any) => {
          handleChange(it.key);
        });
      }, 20)
    );
  };

  //重置数据
  const resetValue = () => {
    tableArray.content = [];
    tableArray.content.push({
      key: null,
      value: '',
      editDisabled: false,
      required: false,
    });
    nextTick(() => {
      tableArray.content.forEach((it: any) => {
        handleChange(it.key);
      });
    });
  };
  defineExpose({
    getValue,
    setValue,
    resetValue,
  });
</script>

<style scoped lang="less">
  @table-color: #e5e7eb;

  .table-border-color {
    border: 1px solid #e5e7eb;
    text-align: center;
  }

  .table-content {
    overflow-x: auto;

    table {
      border-collapse: collapse;
      width: 100%;
      &:extend(.table-border-color);
    }

    table td {
      padding: 5px;
      white-space: nowrap;
      &:extend(.table-border-color);
    }

    table th {
      padding: 5px;
      &:extend(.table-border-color);
    }
  }
</style>