• SPA项目开发之动态树+数据表格+分页


    目录

    一、树形菜单功能实现

    1.完成树形菜单的功能

    2.单个左侧菜单样式

     3.改成动态的菜单

     二、文章分页查询功能

    1.文章查询功能 

    ①.列表

    ②. 分页条代码

    ③.查询代码块

    ④.最终所以优化好的代码 


    一、树形菜单功能实现

    现在我们就是需要前后端的项目都要运行,我们现在不用mock.js 

    最终我们登陆进去目前我们的数据都是死数据,我们现在要把它变为活的数据:

    变为我们数据库中的值:

    1.完成树形菜单的功能

    1.要确定静态树形菜单节点的样式排版

    2.要获取树形节点的数据

            请求后台数据:用   this.axios.post   拿数据

    3.通过拿到的数据,渲染树形节点

            通过 v-for 渲染节点        定义渲染节点的变量

    2.单个左侧菜单样式

    1. <el-submenu index="1">
    2. <template slot="title">
    3. <i class="el-icon-location"></i>
    4. <span>导航一</span>
    5. </template>
    6. <el-menu-item index="1-4-1">
    7. <template slot="title">
    8. <i class="el-icon-location"></i>
    9. <span>选项1</span>
    10. </template>
    11. </el-menu-item>
    12. </el-submenu>

    最终效果:LeftNav.vue

    1. <template>
    2. <el-menu router :default-active="$route.path" default-active="2" class="el-menu-vertical-demo" background-color="#334157"
    3. text-color="#fff" active-text-color="#ffd04b" :collapse="collapsed">
    4. <!-- <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"> -->
    5. <div class="logobox">
    6. <img class="logoimg" src="../assets/img/logo.png" alt="">
    7. </div>
    8. <el-submenu v-for="m in menus" :key="'id_'+m.treeNodeId" :index="'id_'+m.treeNodeId">
    9. <template slot="title">
    10. <i :class="m.icon"></i>
    11. <span>{{m.treeNodeName}}</span>
    12. </template>
    13. <el-menu-item v-for="m2 in m.children" :key="'id_'+m2.treeNodeId" :index="m2.url">
    14. <i :class="m2.iocn"></i>
    15. <span>{{m2.treeNodeName}}</span>
    16. </el-menu-item>
    17. </el-submenu>
    18. </el-menu>
    19. </template>
    20. <script>
    21. export default {
    22. data() {
    23. return {
    24. collapsed:false,
    25. menus:[]
    26. }
    27. },
    28. created(){
    29. // 从总线上取出 this.collapsed变量
    30. this.$root.Bus.$on("collapsed-side",v => {
    31. this.collapsed = v;
    32. });
    33. // 向后台拿树形节点的数据
    34. let url = this.axios.urls.SYSTEM_MENU_TREE;
    35. // this指的是vue实例
    36. this.axios.post(url,{}).then(resp => { //代表成功
    37. console.log(resp);
    38. // 给menus赋值
    39. this.menus = resp.data.result;
    40. }).catch(function(){ //代表失败
    41. });
    42. }
    43. }
    44. </script>
    45. <style>
    46. .el-menu-vertical-demo:not(.el-menu--collapse) {
    47. width: 240px;
    48. min-height: 400px;
    49. }
    50. .el-menu-vertical-demo:not(.el-menu--collapse) {
    51. border: none;
    52. text-align: left;
    53. }
    54. .el-menu-item-group__title {
    55. padding: 0px;
    56. }
    57. .el-menu-bg {
    58. background-color: #1f2d3d !important;
    59. }
    60. .el-menu {
    61. border: none;
    62. }
    63. .logobox {
    64. height: 40px;
    65. line-height: 40px;
    66. color: #9d9d9d;
    67. font-size: 20px;
    68. text-align: center;
    69. padding: 20px 0px;
    70. }
    71. .logoimg {
    72. height: 40px;
    73. }
    74. </style>

    效果如图所示:

     3.改成动态的菜单

    <el-menu router :default-active="$route.path" default-active="2" class="el-menu-vertical-demo" background-color="#334157" text-color="#fff"

     active-text-color="#ffd04b" :collapse="collapsed">

    :index="m2.url" v-for="m2 in m.children">

    我们加上  router :default-active="$route.path"   动态的菜单就可以跳转页面了如图所示:

     接着就是跳转到AppMian中的界面里面去

    1.定义锚点

    2.定义组件

     3.定义路由

    index.js

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. // 3.定义路由与组件的对应关系
    4. import HelloWorld from '@/components/HelloWorld'
    5. import AppMain from '@/components/AppMain'
    6. import LeftNav from '@/components/LeftNav'
    7. import TopNav from '@/components/TopNav'
    8. import Login from '@/views/Login'
    9. import Reg from '@/views/Reg'
    10. import Articles from '@/views/sys/Articles'
    11. Vue.use(Router)
    12. // 4.生成路由对象
    13. export default new Router({
    14. routes: [{
    15. path: '/',
    16. // 这个可要可不用
    17. name: 'Login',
    18. component: Login
    19. },
    20. {
    21. path: '/Login',
    22. name: 'Login',
    23. component: Login
    24. },
    25. {
    26. path: '/Reg',
    27. name: 'Reg',
    28. component: Reg
    29. },
    30. {
    31. path: '/AppMain',
    32. name: 'AppMain',
    33. component: AppMain,
    34. children: [{
    35. path: '/LeftNav',
    36. name: 'LeftNav',
    37. component: LeftNav
    38. },
    39. {
    40. path: '/TopNav',
    41. name: 'TopNav',
    42. component: TopNav
    43. },
    44. {
    45. path: '/sys/Articles',
    46. name: 'Articles',
    47. component: Articles
    48. }
    49. ]
    50. }
    51. ]
    52. })

    最终效果如图所示:

     二、文章分页查询功能

    1.文章查询功能 

    1.el-table 列表组件 

    2.利用axios调用后台的文章查询接口  ---> created        没有传参的

    3.el-pageination 列表组件 分页

            size-change        页大小改变调用的事件

            current-change        页码改变调用的事件

    4.优化        将调用后台的文章查询接口的代码就进行封装,为了复用

    5.el-form   查询的筛选条件        传参了

    this.doSearch  共用的查询方法

    this.search 带了查询条件的方法 

     首先我们要查询的数据库中的表是如图所示:

    ①.列表

    1. <!--列表-->
    2. <el-table size="small" :data="listData" highlight-current-row style="width: 100%;">
    3. <el-table-column align="center" type="selection" width="60">
    4. </el-table-column>
    5. <el-table-column sortable prop="id" label="文章ID" width="300">
    6. </el-table-column>
    7. <el-table-column sortable prop="title" label="文章标题" width="300">
    8. </el-table-column>
    9. <el-table-column sortable prop="body" label="文章内容" width="300">
    10. </el-table-column>
    11. <el-table-column align="center" label="操作" min-width="300">
    12. <template slot-scope="scope">
    13. <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
    14. <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
    15. </template>
    16. </el-table-column>
    17. </el-table>

    ②. 分页条代码

    1. <!-- 分页条 -->
    2. <el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
    3. :current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100" layout="total, sizes, prev, pager, next, jumper"
    4. :total="formInline.total">
    5. </el-pagination>

    ③.查询代码块

    1. <!-- 搜索筛选 -->
    2. <el-form :inline="true" :model="formInline" class="user-search">
    3. <el-form-item label="搜索:">
    4. <el-input size="small" v-model="formInline.title" placeholder="输入文章标题"></el-input>
    5. </el-form-item>
    6. <el-form-item>
    7. <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
    8. <!-- <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button> -->
    9. </el-form-item>
    10. </el-form>

    ④.最终所以优化好的代码 

     Articles.vue

    1. <template>
    2. <div>
    3. <el-form :inline="true" :model="formInline" class="user-search">
    4. <el-form-item label="搜索:">
    5. <el-input size="small" v-model="formInline.title" placeholder="输入文章标题">el-input>
    6. el-form-item>
    7. <el-form-item>
    8. <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索el-button>
    9. el-form-item>
    10. el-form>
    11. <el-table size="small" :data="listData" highlight-current-row style="width: 100%;">
    12. <el-table-column align="center" type="selection" width="60">
    13. el-table-column>
    14. <el-table-column sortable prop="id" label="文章ID" width="300">
    15. el-table-column>
    16. <el-table-column sortable prop="title" label="文章标题" width="300">
    17. el-table-column>
    18. <el-table-column sortable prop="body" label="文章内容" width="300">
    19. el-table-column>
    20. <el-table-column align="center" label="操作" min-width="300">
    21. <template slot-scope="scope">
    22. <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑el-button>
    23. <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除el-button>
    24. template>
    25. el-table-column>
    26. el-table>
    27. <el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
    28. :current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100" layout="total, sizes, prev, pager, next, jumper"
    29. :total="formInline.total">
    30. el-pagination>
    31. div>
    32. template>
    33. <script>
    34. export default {
    35. name:'Articles',
    36. data() {
    37. return {
    38. listData:[],
    39. formInline:{
    40. page:1,
    41. total:10,
    42. title:''
    43. }
    44. };
    45. },
    46. methods:{
    47. handleSizeChange(rows) {
    48. console.log("页大小发生改变时触发!!!");
    49. this.formInline.page = 1;
    50. this.formInline.rows = rows;
    51. this.search();
    52. },
    53. handleCurrentChange(page){
    54. console.log("当前页码发生改变时触发!!!");
    55. this.formInline.page = page;
    56. this.search();
    57. },
    58. // 是为了代码复用
    59. doSearch(param){
    60. // 获取树形节点的数据
    61. let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
    62. // this指的是vue实例
    63. this.axios.post(url,param).then(resp => {
    64. console.log(resp);
    65. this.listData = resp.data.result;
    66. this.formInline = resp.data.pageBean;
    67. }).catch(function(){
    68. });
    69. },
    70. search(){
    71. // 按照条件进行查询
    72. this.doSearch(this.formInline);
    73. }
    74. },
    75. created() {
    76. this.doSearch({});
    77. }
    78. }
    79. script>
    80. <style>
    81. style>

     运行结果如图所示:

  • 相关阅读:
    ubuntu 下使用flatpak的一些记录
    亚商投资顾问 早餐FM/1027新航季国际客运航班量840班
    『第四章』一见倾心:初识小雨燕(上)
    【容器】Dockerfile学习1
    搭建一个QQ机器人叫女友起床
    ffmpeg-go库的介绍
    使用 Python 中的小波变换信号驾驭股票价格的波动
    前端工程师笔试题【校招】
    Wasserstein Slim GAIN with Clipping Penalty(WSGAIN-CP)介绍及代码实现——基于生成对抗网络的缺失数据填补
    【web-解析目标】(1.2.3)解析应用程序:确定服务器端功能
  • 原文地址:https://blog.csdn.net/weixin_67465673/article/details/126826475