testEditBodyCellTable.vue 8.01 KB
<!-- eslint-disable vue/valid-v-model -->
<template>
  <div class="table-content">
    <!-- TODO: 待优化测试表格 -->
    <table align="center">
      <thead>
        <tr>
          <th v-for="item in editTestCellTableTHeadConfig" :key="item">{{ item }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in tableTestArray.content" :key="index">
          <td style="width: 7.5vw">
            <Select
              :disabled="true"
              v-model:value="item.key"
              placeholder="请选择"
              :options="selectOptions"
            />
          </td>
          <td style="width: 6.5vw">
            <a-input v-if="item.key === 'scope'" placeholder="请输入" v-model:value="item.value" />
            <a-tree-select
              v-else-if="item.key === 'organizationId'"
              v-model:value="item.value"
              show-search
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              placeholder="请选择组织"
              allow-clear
              tree-default-expand-all
              :tree-data="treeData"
              @change="handleOrgnationChange(item)"
            />
            <Select
              v-else-if="item.key === 'entityId'"
              v-model:value="item.value"
              placeholder="请选择"
              notFoundContent="请选择"
              :options="entityOptions"
              allowClear
            />
            <Select
              v-else-if="item.key === 'keys'"
              v-model:value="item.value"
              placeholder="请选择"
              notFoundContent="请选择"
              :options="attributeOptions"
              mode="multiple"
              allowClear
            />
            <div v-else-if="item.key === 'date_range'">
              <a-button disabled type="primary">{{ item.value?.split(',').at(-2) }}</a-button>
              <span>~</span>
              <a-button disabled type="primary">{{ item.value?.split(',').at(-1) }}</a-button>
            </div>
            <div v-else-if="item.key === 'fixed_date'">
              <a-button disabled type="primary">{{ item.value }}</a-button>
            </div>
            <Select
              v-else
              v-model:value="item.value"
              placeholder="请选择"
              notFoundContent="请选择"
              :options="valueOptions"
              allowClear
              @change="handleValueChange(item)"
            />
          </td>
          <td style="width: 6.5vw">
            <a-input
              v-if="item.key === 'scope'"
              placeholder="请输入"
              v-model:value="item.keyValue"
            />
            <a-date-picker
              v-else-if="item.key === 'fixed_date'"
              style="width: 6.5vw"
              v-model:value="item.keyValue"
              :show-time="{ format: 'HH:mm' }"
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="日期时间"
            />
            <a-range-picker
              v-else-if="item.key == 'date_range'"
              style="width: 6.5vw"
              v-model:value="item.dateValue"
              :show-time="{ format: 'HH:mm' }"
              format="YYYY-MM-DD HH:mm"
              :placeholder="['开始', '结束']"
            />
            <a-input v-else disabled placeholder="请输入" v-model:value="item.keyValue" />
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
<script lang="ts" setup name="editCellTable">
  import { reactive, ref, onMounted } from 'vue';
  import { Select } from 'ant-design-vue';
  import { findDictItemByCode } from '/@/api/system/dict';
  import { getAllDeviceByOrg } from '/@/api/dataBoard';
  import { getDeviceAttributes } from '/@/api/dataBoard';
  import { useApi } from '../../../hooks/useApi';
  import { cloneDeep } from 'lodash-es';
  import { tableItems, selectType } from '../../../types';
  import { editTestCellTableTHeadConfig } from '../../../config';

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

  onMounted(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<selectType[]>([]);

  const valueOptions = ref<selectType[]>([]);

  const entityOptions = ref<selectType[]>([]);

  const attributeOptions = ref<selectType[]>([]);

  const treeData = ref([]);

  const tableTestArray = reactive<{
    content: tableItems[];
  }>({
    content: [
      {
        key: undefined,
        value: undefined,
        keyValue: null,
        editDisabled: false,
        dateValue: null,
      },
    ],
  });

  //设置数据
  const setTableArray = (data) => {
    const list = cloneDeep(data);
    list?.forEach((it) => {
      if (it.key === 'keys') it.value = [];
    });
    if (Array.isArray(list)) (tableTestArray.content = list) && getApi(list);
    if (list.hasOwnProperty('form-data') && Array.isArray(list['form-data']))
      (tableTestArray.content = list['form-data']) && getApi(list['form-data']);
    if (
      list.hasOwnProperty('x-www-form-urlencoded') &&
      Array.isArray(list['x-www-form-urlencoded'])
    )
      (tableTestArray.content = list['x-www-form-urlencoded']) &&
        getApi(list['x-www-form-urlencoded']);
  };

  const getApi = (list) => {
    list?.forEach(async (it) => {
      if (it.key === 'deviceProfileId') {
        const { options } = await useApi(it.key);
        valueOptions.value = options;
      } else if (it.key === 'organizationId') {
        const { options } = await useApi(it.key);
        treeData.value = options as any;
      }
    });
  };

  const handleOrgnationChange = async (e) => {
    let deviceProfileId = '';
    tableTestArray.content.forEach((f: any) => {
      if (f.key === 'entityId') {
        f.value = null;
      }
      if (f.key === 'deviceProfileId') deviceProfileId = f.value;
    });
    getEntityOptions(e.value, deviceProfileId);
  };

  const getEntityOptions = async (organizationId: string, deviceProfileId?: string) => {
    const res = await getAllDeviceByOrg(organizationId, deviceProfileId);
    entityOptions.value = res.map((item) => ({
      label: item.name,
      value: item.tbDeviceId,
    }));
  };

  const getAttributeOptions = async (params) => {
    const res = await getDeviceAttributes(params);
    if (Object.keys(res).length === 0) return (attributeOptions.value.length = 0);
    attributeOptions.value = res?.map((item) => ({ label: item.name, value: item.identifier }));
  };

  const handleValueChange = (e) => {
    let organizationId = '';
    if (e.key === 'deviceProfileId') {
      tableTestArray.content.forEach((f: any) => {
        if (f.key === 'keys') {
          f.value = [];
        }
        if (f.key === 'organizationId') organizationId = f.value;
        if (f.key === 'entityId') f.value = null;
      });
      getAttributeOptions({ deviceProfileId: e.value });
      if (organizationId !== '') {
        getEntityOptions(organizationId, e.value);
      }
    }
  };

  //获取数据
  const getValue = () => {
    return tableTestArray.content;
  };
  defineExpose({
    getValue,
    setTableArray,
  });
</script>

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

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

  .table-content {
    table {
      width: 31vw;
      &:extend(.table-border-color);
    }

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

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