spa项目开发之动态树功能&&右侧文章的分页查询功能

前言

(54条消息) spa项目完成登录注册_笑看人生十余载的博客-CSDN博客

(54条消息) SPA项目开发首页导航&&左侧菜单_笑看人生十余载的博客-CSDN博客

即前两篇博客的基础上在进行开发,今天要分享的知识是spa项目开发动态树功能以及右侧文章分页查询功能

码字不易,转载请说明!!!


目录

一、数据库结构

二、动态树功能(树的渲染)

LeftNav.vue

①定义一个数组类型的标签来存放在后台拿到的数据

扫描二维码关注公众号,回复: 13611276 查看本文章

②实现前后端交互,将从数据库拿到的结果放入menus中

③动态树的渲染

三、右侧文章的分页查询功能

①首先得设置锚点,点击树菜单可以跳转相应内容界面  AppMain.vue

②根据数据库结构建立目录和组件

③定义路由和映射

④在Articles.vue组件中定义样式(搜索、列表,分页条) 

⑤首先获取表的值,一步一步来

定义listData标签来装内容,然后取到值 this.listData = resp.data.result; 然后在前台进行渲染

⑥ 加上分页,和搜索框,来进行查询和分页功能

首先定义属性

写方法和获取值

调用方法,把doSearch当做一个公共的方法,方便处理传参和不传参的问题

总代码

运行效果:带参查询

无参查询


一、数据库结构

树表

文章表

二、动态树功能(树的渲染)

LeftNav.vue

<template>
  <el-menu router :default-active="$route.path"  class="el-menu-vertical-demo" background-color="#334157" text-color="#fff"
    active-text-color="#ffd04b" :collapse="collapsed">
    <!-- <el-menu default-active="2" :collapse="collapsed" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b"> -->
    <div class="logobox">
      <img class="logoimg" src="../assets/img/logo.png" alt="">
    </div>
    <el-submenu :index="'id_'+m.treeNodeId" v-for="m in menus">
      <template slot="title">
        <i :class="m.icon"></i>
        <span>{
   
   {m.treeNodeName}}</span>
      </template>
      <el-menu-item :key="'id_'+m2.treeNodeId" :index="m2.url" v-for="m2 in m.children">
        <template slot="title">
          <i :class="m2.icon"></i>
          <span>{
   
   {m2.treeNodeName}}</span>
        </template>
      </el-menu-item>
    </el-submenu>
  </el-menu>
</template>
<script>
  export default {
    data() {
      return {
        collapsed: false,
        menus: []
      }
    },
    created() {
      this.$root.Bus.$on("collapsed-aside", (v) => {
        this.collapsed = v;
      })

      let url = this.axios.urls.SYSTEM_MENU_TREE;
      //箭头函数解决了this指针污染问题
      this.axios.post(url, {}).then((resp) => {
        console.log(resp);
        this.menus = resp.data.result;
      }).catch(function(error) {
        console.log(error);
      });
    }
  }
</script>
<style>
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 240px;
    min-height: 400px;
  }

  .el-menu-vertical-demo:not(.el-menu--collapse) {
    border: none;
    text-align: left;
  }

  .el-menu-item-group__title {
    padding: 0px;
  }

  .el-menu-bg {
    background-color: #1f2d3d !important;
  }

  .el-menu {
    border: none;
  }

  .logobox {
    height: 40px;
    line-height: 40px;
    color: #9d9d9d;
    font-size: 20px;
    text-align: center;
    padding: 20px 0px;
  }

  .logoimg {
    height: 40px;
  }
</style>

定义一个数组类型的标签来存放在后台拿到的数据

实现前后端交互,将从数据库拿到的结果放入menus中

③动态树的渲染

效果展示 

三、右侧文章的分页查询功能

①首先得设置锚点,点击树菜单可以跳转相应内容界面  AppMain.vue

根据数据库结构建立目录和组件

定义路由和映射

在Articles.vue组件中定义样式(搜索、列表,分页条) 

<template>
  <div>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索:">
        <el-input size="small" v-model="formInline.title" placeholder="输入文章标题"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table size="small" :data="listData" highlight-current-row border element-loading-text="拼命加载中"
      style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="文章ID" width="300">
      </el-table-column>
      <el-table-column sortable prop="title" label="文章标题" width="300">
      </el-table-column>
      <el-table-column sortable prop="body" label="文章内容" width="300">
      </el-table-column>
    </el-table>
    <!-- 分页条 -->
    <el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100"
      layout="total, sizes, prev, pager, next, jumper" :total="formInline.total">
    </el-pagination>
  </div>
</template>

首先获取表的值,一步一步来

定义listData标签来装内容,然后取到值 this.listData = resp.data.result; 然后在前台进行渲染

<script>
  export default {
    name: 'Articles',
    data() {
      return {
        listData: []
      }
    }
    created() {
        // 请求地址
        let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
        // 前后端数据交互
        this.axios.post(url, param).then((resp) => {
          console.log(resp);
          // 取值
          this.listData = resp.data.result;
        }).catch(function(error) {
          console.log(error);
        });
        this.$root.Bus.$on("collapsed-aside", (v) => {
          this.collapsed = v;
        });
      }
    }
  }
</script>

运行效果

⑥ 加上分页,和搜索框,来进行查询和分页功能

首先定义属性

写方法和获取值

调用方法,把doSearch当做一个公共的方法,方便处理传参和不传参的问题

总代码

<script>
  export default {
    name: 'Articles',
    data() {
      return {
        listData: [],
        formInline: {
          page: 1,
          rows: 10,
          total: 0,
          title:''
        }
      }
    },
    methods: {
      handleSizeChange(rows) {
        console.log("当前查询数据量:" + rows);
        this.formInline.page = 1;
        this.formInline.rows = rows;
        this.search();
      },
      handleCurrentChange(page) {
        console.log("当前页为:" + page);
        this.formInline.page = page;
        this.search();
      },
      doSearch(param) {
        // 请求地址
        let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
        // 前后端数据交互
        this.axios.post(url, param).then((resp) => {
          console.log(resp);
          // 取值
          this.listData = resp.data.result;
          // 查询total
          this.formInline.total = resp.data.pageBean.total;
        }).catch(function(error) {
          console.log(error);
        });
        this.$root.Bus.$on("collapsed-aside", (v) => {
          this.collapsed = v;
        });
      },
      // 带查询条件
      // 利用钩子函数提高代码复用性
      search(){
        this.doSearch(this.formInline);
      }
    },
    // 不带查询条件
    created() {
      this.doSearch({});
    }
  }
</script>

运行效果:带参查询

无参查询

到这里就结束了,欢迎大佬指点  

猜你喜欢

转载自blog.csdn.net/weixin_56069070/article/details/121283158