使用栅格分区后报错,页面无法正常显示

来源:8-9 文档管理页面布局修改

午夜小学徒_Z

2022-04-14

<template>
  <a-layout>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <a-row>
        <!-- 左侧栏 -->
        <a-col :span="12">
          <p>
            <a-form layout="inline" :model="param">
              <a-form-item>
                <a-input v-model:value="param.name" placeholder="名称">
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-button type="primary" @click="handleQueryDoc()">
                  查询
                </a-button>
              </a-form-item>
              <a-form-item>
                <a-button type="primary" @click="add()">
                  新增
                </a-button>
              </a-form-item>
            </a-form>
          </p>

          <a-table
              :columns="columns"
              :row-key="record => record.id"
              :data-source="level1"
              :loading="loading"
              :pagination="false"
          >
            <template #cover="{ text: cover }">
              <img v-if="cover" :src="cover" alt="avatar"/>
            </template>
            <template v-slot:action="{ text, record }">
              <a-space size="small">
                <a-button type="primary" @click="edit(record)">
                  编辑
                </a-button>
                <a-popconfirm
                    title="删除后不可恢复,确认删除?"
                    ok-text="是"
                    cancel-text="否"
                    @confirm="handleDelete(record.id)"
                >
                  <a-button type="danger">
                    删除
                  </a-button>
                </a-popconfirm>

              </a-space>
            </template>
          </a-table>
        </a-col>

        <!-- 右侧栏 -->
        <a-col :span="12">
          <a-form :model="doc" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
            <!-- 名称 -->
            <a-form-item label="名称">
              <a-input v-model:value="doc.name"/>
            </a-form-item>

            <!-- 父文档 -->
            <a-form-item label="父文档">
              <a-tree-select
                  v-model:value="doc.parent"
                  style="width: 100%"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="treeSelectDate"
                  placeholder="请选择父文档"
                  tree-default-expand-all
                  :replaceFields="{title:'name', key:'id', value: 'id'}"
              >
              </a-tree-select>
            </a-form-item>

            <!-- 顺序 -->
            <a-form-item label="顺序">
              <a-input v-model:value="doc.sort"/>
            </a-form-item>
            <!-- 内容 -->
            <a-form-item label="内容">
              <div id="content"></div>
            </a-form-item>
          </a-form>
        </a-col>
      </a-row>

    </a-layout-content>
  </a-layout>

<!--  <a-modal-->
<!--      title="文档表单"-->
<!--      v-model:visible="modalVisible"-->
<!--      :confirm-loading="modalLoading"-->
<!--      @ok="handleModalOk"-->
<!--  >-->
<!--    -->
<!--  </a-modal>-->

</template>


<script lang="ts">
import {createVNode, defineComponent, onMounted, ref} from 'vue';
import axios from 'axios';
import {message, Modal} from "ant-design-vue";
import {Tool} from '@/util/tool';
import {useRoute} from 'vue-router';
import ExclamationCircleOutlined from "@ant-design/icons-vue/ExclamationCircleOutlined";
import E from 'wangeditor';

export default defineComponent({
  name: 'AdminDoc',
  setup() {
    // 列表的列名
    const columns = [
      {
        title: '名称',
        dataIndex: 'name'
      },
      {
        title: '父文档',
        key: 'parent',
        dataIndex: 'parent'
      },
      {
        title: '顺序',
        dataIndex: 'sort'
      },
      {
        title: 'Action',
        key: 'action',
        slots: {customRender: 'action'}
      }
    ];

    // 声明一个路由内置的变量
    const route = useRoute();
    console.log("路由", route);
    console.log("route.path", route.path);
    console.log("route.query", route.query);
    // console.log("route.params", route.params);
    // console.log("route.fullPath", route.fullPath);
    // console.log("route.name", route.name);
    // console.log("route.meta", route.meta);

    // 声明:查询栏的【名称】查询条件响应式变量
    const param = ref();
    param.value = {};
    // 声明:后端查询文档表doc数据的响应式变量
    const docs = ref();
    // 声明:loading加载效果
    const loading = ref(false);

    /**
     * 一级文档树,children属性就是二级文档
     * [{
     *   id: "",
     *   name: "",
     *   children: [{
     *     id: "",
     *     name: "",
     *   }]
     * }]
     */
    const level1 = ref();
    /**
     * 查询所有文档数据
     **/
    const handleQueryDoc = () => {
      loading.value = true;
      axios.get("/doc/all", {}).then((response) => {
        loading.value = false;
        const data = response.data;

        if (data.success) {
          docs.value = data.content;
          console.log("原始数组", docs.value);

          level1.value = [];
          level1.value = Tool.array2Tree(docs.value, 0);
          console.log("树形结构", level1.value);
        } else {
          message.error(data.message);
        }
      });
    };

    /**
     * -------- 表单 ---------
     */
    // 因为树选择的树形状态,会随当前编辑的节点而变化,所以单独声明一个响应式变量
    const treeSelectDate = ref();
    treeSelectDate.value = {};

    const doc = ref({});
    doc.value = {};
    // 显示model框
    const modalVisible = ref(false);
    // 显示loading效果
    const modalLoading = ref(false);
    const  editor = new E('#content');

    // 展示model框
    // const handleModalOk = () => {
    //   // 显示的loading效果
    //   modalLoading.value = true;
    //   // 保存数据
    //   axios.post('/doc/save', doc.value).then((res) => {
    //     const data = res.data;
    //     if (data.success) {
    //       // 去除model框
    //       modalVisible.value = false;
    //       // 去除loading效果
    //       modalLoading.value = false;
    //
    //       // 重新加载列表
    //       handleQueryDoc();
    //     } else {
    //       // 去除loading效果
    //       modalLoading.value = false;
    //       message.error(data.message);
    //     }
    //   })
    // };

    // 声明待删除集合变量
    const ids: Array<string> = [];
    // 声明待删除名称集合,为提示语句使用
    const deleteNames: Array<string> = [];

    /**
     * 递归:查找整根树枝,获取删除时需要的所有待删除id集合和名称集合
     * @param treeSelectDate
     * @param id
     */
    const getDeleteIds = (treeSelectDate: any, id: any) => {
      // 遍历数组,即遍历某一层节点
      for (let i = 0; i < treeSelectDate.length; i++) {
        const node = treeSelectDate[i];
        if (node.id === id) {
          // 如果当前节点就是目标节点,将目标Id放入结果集
          ids.push(id);
          // 将目标名称放入
          deleteNames.push(node.name)

          // 遍历所有子节点
          const children = node.children;
          if (Tool.isNotEmpty(children)) {
            for (let j = 0; j < children.length; j++) {
              getDeleteIds(children, children[j].id)
            }
          }
        } else {
          // 如果当前节点不是目标节点,则到其他子节点再找找看
          const children = node.children;
          if (Tool.isNotEmpty(children)) {
            getDeleteIds(children, id);
          }
        }
      }

    }

    /**
     *  递归 : 将某节点及其子孙节点全部设置为 disable
     * @param treeSelectDate 树数据
     * @param id
     */
    const setDisable = (treeSelectDate: any, id: any) => {
      // console.log(treeSelectDate,id);
      // 遍历数组,即遍历某一层节点
      for (let i = 0; i < treeSelectDate.length; i++) {
        const node = treeSelectDate[i];
        if (node.id === id) {
          // 如果当前节点就是目标节点
          console.log("disable", node);
          // 将目标节点设置为disable
          node.disabled = true;

          // 遍历所有子节点,即将所有子节点全部加上disable
          const children = node.children;
          if (Tool.isNotEmpty(children)) {
            for (let j = 0; j < children.length; j++) {
              setDisable(children, children[j].id)
            }
          }
        } else {
          // 如果当前节点不是目标节点,则到其他子节点再找找看
          const children = node.children;
          if (Tool.isNotEmpty(children)) {
            setDisable(children, id);
          }
        }
      }

    }

    /**
     * 新增
     */
    const add = () => {
      modalVisible.value = true;
      doc.value = {};
      doc.value = {
        ebookId: route.query.ebookId
      }

      treeSelectDate.value = Tool.copy(level1.value);
      // 为选择树添加一个"无"
      treeSelectDate.value.unshift({id: 0, name: '无'});

      // 延时,异步执行时使用 setTimeout方法,下面是延时100毫秒
      setTimeout(function (){
        // 根据id选择器,创建富文本
        editor.create();
      },100)

    }

    /**
     * 编辑
     */
    const edit = (record: any) => {
      modalVisible.value = true;
      doc.value = Tool.copy(record);

      // 不能选择当前节点及所有子孙节点,作为父节点,会使树断开
      treeSelectDate.value = Tool.copy(level1.value);
      setDisable(treeSelectDate.value, record.id);

      // 为选择树添加一个"无"
      treeSelectDate.value.unshift({id: 0, name: '无'});

      // 延时,异步执行时使用 setTimeout方法,下面是延时100毫秒
      setTimeout(function (){
        // 根据id选择器,创建富文本
        editor.create();
      },100)
    };

    /**
     * 删除
     * @param id
     */
    const handleDelete = (id: number) => {
      // 清空数组,否则多次删除时,数组会一直增加
      ids.length = 0;
      deleteNames.length = 0;
      getDeleteIds(level1.value, id);
      Modal.confirm({
        title: '重要提示:',
        icon: createVNode(ExclamationCircleOutlined),
        content: '将删除:【' + deleteNames.join(", ") + '】删除后不可恢复,确认删除?',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          axios.delete('/doc/delete/' + ids.join(',')).then((res) => {
            const data = res.data;
            if (data.success) {
              // 重新加载列表
              handleQueryDoc();
            }
          })
        },
      })
    };

    // 页面渲染好后,执行此法方法
    onMounted(() => {
      handleQueryDoc();
    });

    // 将结果返回到前端页面上
    return {
      param,
      level1,
      columns,
      loading,
      handleQueryDoc,
      treeSelectDate,

      add,
      edit,
      handleDelete,

      doc,
      modalVisible,
      modalLoading,
      // handleModalOk
    }
  }
});
</script>


<style scoped>
img {
  width: 50px;
  height: 50px;
}
</style>

前段报错,问题截图如下
图片描述

写回答

2回答

午夜小学徒_Z

提问者

2022-04-14

问题解决了,花费一上午的时间,一行行代码注释查找

问题主要是出现在父文档多级下拉框选择上,不清楚老师的操作在点击【文档管理】是直接默认了编辑第一个文档,所以我的在点击文档管理没有默认,导致了在父文档id那里多级下拉框时报错了,解决方法有两个,如下

方法1:在父级下拉框那里加一个判断,代码如下//img.mukewang.com/szimg/6257975a09137add10200528.jpg

方法2:在点击文档管理,加载页面数据完成后,默认编辑文档树结构第一个主文档

//img.mukewang.com/szimg/625797db09f4247512030838.jpg

下载视频          
1
0

甲蛙

2022-04-15

点赞!使用代码注释法来定位问题,是个好方法
下载视频          
0
0

Spring Boot+Vue3前后端分离,实战wiki知识库系统

一课掌握前后端最火框架,更有职场竞争力

2524 学习 · 1671 问题

查看课程