主頁 > 前端設計 > SpringBoot實戰開發后臺管理-架構說明和開發

SpringBoot實戰開發后臺管理-架構說明和開發

2021-08-02 08:08:53 前端設計

SpringBoot實戰開發后臺管理-架構說明和開發

1、架構模式

  • 單體架構

  • 技術結構

  • springboot + mybatisplus + mysql

  • 前端技術:jquery / layui

  • 字體圖示庫:iconfont

2、后臺開發的架構模式有哪些?

  • 純企業開發(全部由公司自己內部去設計后臺的頁面和功能控制,影片,js、css的撰寫)
  • 使用開源一些開發模式 (layui、bootstrap、jui、extjs)等等,這些有什么好處呢?快速和方便,里面提供大量的組件和模塊,比如:日期組件、表格、form、按鈕,彈窗等等 95%,
  • 新型的后臺開發模式:vue-admin-element、elementui、antd等都一些基于vue-cli架構提提完成的一種前后端分離的架構模式,(95%

3、后臺開發的選單導航的渲染的問題

有三種比較程式的模式:

1、基于iframe(比較傳統 異步 + 動態頁面渲染)

2、純異步(全部用js來動態拼接和渲染)(比較傳統 異步 + 動態頁面渲染)

3、基于路由跳轉(vue腳手架)-vue-router

4、選單導航的渲染問題

先要把導航欄進行管理控制

思考問題:導航是在每個頁面中,都需要存在,那么我們可以公共的部分用頁面包含的技術進行剝離,然后用定義的語法,在每個頁面中進行匯入,即可

好處就是:方便我們統一維護和后續的升級和控制,

在freemarker或者jsp或者thymeleaf都有這樣的頁面包含的技術,以thymeleaf為例:

新建一個common在里面新建leftnav.html如下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <body>
        <aside id="asideapp" th:fragment="asidebar"
               class="byte-layout-sider byte-layout-sider-light mp-main-sider animated fadeInLeft"
               style="width: 246px;">
            <div class="byte-layout-sider-children">
                <div class="mp-menu-wrapper f-min-scroll f-hover-scroll mp-menu-wrapper-can-scroll">
                    <div class="byte-menu garr-menu">
                        <div class="byte-menu-inline  base_creation_tab">
                            <div class="byte-menu-inline-header">
                                <a href="/admin/index" class="">
                                    <span style="padding-left: 0px; display: block;">
                                        <span title="控制臺" class="ksd-icon-sp iconfont fz20 mr-2 iconhome"></span>
                                        <span>控制臺</span>
                                    </span>
                                </a>
                            </div>
                            <div class="byte-menu-inline-content animated fadeIn"
                                 style="height: auto; display: none;"></div>
                        </div>
                        <div class="byte-menu-inline  base_creation_tab">
                            <div class="byte-menu-inline-header">
                                <a href="javascript:void(0);" class="">
                                    <span style="padding-left: 0px;">
                                        <span title="用戶管理" class="ksd-icon-sp iconfont fz20 mr-2 iconiconzh1"></span>
                                        <span>用戶管理</span>
                                    </span>
                                    <span class="byte-menu-icon-suffix">
                                        <svg viewBox="0 0 1024 1024" width="1em" height="1em" fill="currentColor" class="byte-icon byte-icon-down">
                                        </svg>
                                    </span>
                                </a>
                            </div>
                            <div class="byte-menu-inline-content animated fadeIn" style="display: block;">
                                <div title="用戶管理" data-href="/admin/user/list" class="byte-menu-item">
                                    <span style="padding-left: 24px; display: block;">
                                        <span class="ksd-icon-sp selected-border-right iconfont fz20 mr-2 iconiconzh1"></span>
                                        <a href="javascript:void(0);">用戶管理</a>
                                    </span>
                                </div>
                                <div title="統計模塊" data-href="/admin/state/list" class="byte-menu-item">
                                    <span style="padding-left: 24px; display: block;">
                                        <span class="ksd-icon-sp selected-border-right iconfont fz20 mr-2 iconiconzh1"></span>
                                        <a href="javascript:void(0);">統計模塊</a>
                                    </span>
                                </div>
                            </div>
                        </div>

                        <div class="byte-menu-inline  base_creation_tab">
                            <div class="byte-menu-inline-header">
                                <a href="javascript:void(0);" class="">
                                    <span style="padding-left: 0px; display: block;">
                                        <span title="角色管理" class="ksd-icon-sp iconfont fz20 mr-2 iconorder1"></span>
                                        <span>通知管理</span>
                                    </span>
                                    <span class="byte-menu-icon-suffix is-open">
                                        <svg viewBox="0 0 1024 1024" width="1em" height="1em" fill="currentColor" class="byte-icon byte-icon-down">
                                        </svg>
                                    </span>
                                </a>
                            </div>
                            <div class="byte-menu-inline-content animated fadeIn"
                                 style="height: auto; display: none;">
                                <div title="角色串列" data-href="/admin/permission/list" class="byte-menu-item">
                                    <span style="padding-left: 24px; display: block;">
                                        <span class="ksd-icon-sp selected-border-right iconfont fz20 mr-2 iconorder1"></span>
                                        <a href="javascript:void(0);">角色串列</a>
                                    </span>
                                </div>
                                <div title="權限串列" data-href="/admin/role/list" class="byte-menu-item">
                                    <span style="padding-left: 24px; display: block;">
                                        <span class="ksd-icon-sp selected-border-right iconfont fz20 mr-2 iconorder1"></span>
                                        <a href="javascript:void(0);">權限串列</a>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </aside>
    </body>
</html>

核心代碼

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<aside th:fragment="asidebar"></aside>

th:fragment="asidebar"給頁面模板的具體位置取一個名字:asidebar,這個名字在需要包含的頁面中引入:如下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=Edge">
		<meta http-equiv="Pragma" content="no-cache">
		<meta http-equiv="Cache-Control" content="no-cache">
		<meta http-equiv="Expires" content="0">
		<title>后臺管理</title>
		<meta name="keywords" content="HTML, CSS, XML, XHTML, JavaScript">
		<meta name="description" content="免費的 web 技術教程">
		<!-- 后臺公共css js存放檔案頁面包含 -->
		<div th:replace="~{commons/header::scriptbar}"></div>
	</head>

	<body data-ext-version="3.1" style="">
		<!-- 頭部選單導航 -->
		<div th:replace="~{commons/nav::navbar}"></div>
		<!-- 內容區域 -->
		<div id="root" class="ksd-main">
			<div class="pgc-wrapper pgc-index index-wrapper">
				<div class="pgc-content">
					<section class=" mp-main-contain byte-layout-has-sider">
						<!-- 左側選單導航 -->
						<div th:replace="~{commons/leftnav::asidebar}"></div>
						<!-- 內容表格 -->
						<div id="ksd-mainbox" class="animated fadeIn pr"></div>
					</section>
				</div>
			</div>
		</div>
		<!-- 底部 -->
		<div th:replace="~{commons/footer::footerbar}"></div>
		<!-- 頁面腳本 -->
		<script th:src="@{/js/admin/index.js}"></script>
	</body>
</html>

核心代碼:

<div th:replace="~{commons/leftnav::asidebar}"></div>

th:replace 代表的:就把commons/leftnav.html中的具體代碼塊名字是:th:fragment=“asidebar” 取出來,替換這個div,

在這里插入圖片描述

$(function () {
    // 初始化選單導航
    adminAside.init();
    // 頁面加載提示
    adminLoading.init();
})

// 控制左側選單導航
var adminAside = {
    init:function () {
        this.animate();
        this.menuEvent();
    },

    // 控制左側選單導航的折疊和展開
    animate:function () {
        //點擊選單系結點擊事件,然后控制自己下方的選單進行展開和收起
        $("#asideapp").find(".byte-menu-inline-header").on("click", function () {
            // 控制排它
            $(this).parents(".byte-menu-inline").siblings().find(".byte-menu-inline-content").hide();
            $(this).parents(".byte-menu-inline").siblings().find(".byte-menu-icon-suffix").removeClass("is-open");
            $(this).next().toggle();
            $(this).find(".byte-menu-icon-suffix").toggleClass("is-open");
        })
    },

    // 控制選單點擊渲染右側模板
    menuEvent:function () {
        $("#asideapp").find(".byte-menu-item").on("click", function () {
            var href = $(this).data("href");
            // $.get(href, function (res) {
            //     $("#ksd-mainbox").html(res);
            // })

            $("#ksd-mainbox").load(href);
        })
        // 觸發第一個元素,作為默認的激活專案
        $("#asideapp").find(".byte-menu-item").eq(0).trigger("click");
    }

}

// 頁面加載提示
var adminLoading = {
    init:function () {
        this.animate();
    },
    animate:function () {

    }
}

5、登錄

注意:后臺的開發中處理登錄和退出,是不需要進行登錄,其他的后臺訪問頁面都需要進行登錄攔截才能進行進入,所以我們在做后臺的時候首先考慮的問題之一:登錄攔截

5.1 定義并注冊登錄攔截器

package com.example.config;

import com.example.hander.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Auther: 長頸鹿
 * @Date: 2021/07/26/16:43
 * @Description:
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Bean
    public LoginInterceptor getLoginInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 攔截器注冊
        registry.addInterceptor(getLoginInterceptor())
                // 排除登錄,退出
                .excludePathPatterns("/admin/login",
                        "/admin/logout",
                        "/admin/toLogin")
                // 攔截后臺所有請求
                .addPathPatterns("/admin/**");
    }
}

5.2 在攔截器中排除登錄和退出的路由

@Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 攔截器注冊
        registry.addInterceptor(getLoginInterceptor())
                // 排除登錄,退出
                .excludePathPatterns("/admin/login",
                        "/admin/logout",
                        "/admin/toLogin")
                // 攔截后臺所有請求
                .addPathPatterns("/admin/**");
    }

5.3 已登錄,跳轉到首頁

package com.example.controller;

import com.example.common.constant.RConstants;
import com.example.common.exception.ResultCodeEnum;
import com.example.common.exception.ValidationException;
import com.example.entity.User;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Auther: 長頸鹿
 * @Date: 2021/07/25/11:07
 * @Description:
 */
@Controller
@Slf4j
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String login(HttpSession session){
        // 首先判斷一下,用戶是否已經登錄
        User sessionUser = (User) session.getAttribute(RConstants.SESSION_USER);
        // 如果已經登錄就直接進入到首頁
        if (sessionUser != null) {
            return "redirect:/admin/index";
        }
        // 如果沒有登錄,直接去登錄頁面
        return "login";
    }

    @PostMapping("/toLogin")
    @ResponseBody
    public String toLogin(HttpSession session, String nickname, String password){
        log.info("當前用戶:{},密碼:{}", nickname, password);
        // 根據用戶輸入的nickname,查詢用戶是否存在
        User user = userService.getByUserName(nickname);
        // 如果不存在就回傳fail,代表用戶找不到
        if(user == null){
            throw new ValidationException(ResultCodeEnum.NICKNAME_NO_EXISTENCE);
        }
        // 判斷當前用戶輸入的密碼和資料庫密碼是否一致, 如果一致 登錄成功
        if (user != null && !user.getPassword().equalsIgnoreCase(password)) {
            throw new ValidationException(ResultCodeEnum.PASSWORD_ERROR);
        }
        // 把登錄成功用戶資訊寫入session會話中
        session.setAttribute(RConstants.SESSION_USER, user);
        // 登錄成功回傳success
        return "success";
    }

}

5.4、退出登錄

@GetMapping("/logout")
public String logout(HttpSession session){
    // 退出登錄
    session.invalidate();
    // 退出成功已經,重定向到登錄頁面
    return "redirect:/admin/login";
}
<a th:href="@{/admin/logout}" style="width: 86px;"><i class="iconfont iconai-out"></i>退出登錄</a>

5.5、登錄頁面背景設定

.bgpic{
  	background:url("xxxx.jpg");
    position:fixed;
    top:0;
    left:0;
    bottom:0;
    right:0;
    filter:blur(1px);
    background-size:cover;
}

6、完成串列查詢,搜索,分頁

6.1 分頁攔截器的配置

package com.example.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Auther: 長頸鹿
 * @Date: 2021/07/30/14:53
 * @Description:
 */
@Configuration
@EnableTransactionManagement
public class MyBatisPlusConfig {

    // 分頁插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

6.2 定義分頁邏輯

package com.example.controller.state;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.common.BaseController;
import com.example.entity.State;
import com.example.service.state.StateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Auther: 長頸鹿
 * @Date: 2021/07/28/15:27
 * @Description:
 */
@Controller
public class StateController extends BaseController {

    @Autowired
    private StateService stateService;

    @GetMapping("state/list")
    public String list(ModelMap modelMap){
        Page<State> pageState = stateService.pageList(0, 10);
        // 2: 把資料放入到作用域
        // 每頁顯示的具體資料
        modelMap.put("stateList", pageState.getRecords());
        // 總記錄數
        modelMap.put("total", pageState.getTotal());
        //pageSize是每頁顯示多少條
        modelMap.put("pageSize", pageState.getSize());
        // pageNo 當前頁
        modelMap.put("pageNo", pageState.getCurrent());
        //  pages分了多少頁
        modelMap.put("pages", pageState.getPages());
        // 3: 渲染的視圖模板
        return "state/template";
    }

    @GetMapping("state/listTemplate")
    public String listTemplate(ModelMap modelMap, @RequestParam(name = "pageNo", defaultValue = "1")Integer pageNo,
                               @RequestParam(name = "pageSize", defaultValue = "10")Integer pageSize){
        Page<State> pageState = stateService.pageList(pageNo,pageSize);
        // 2: 把資料放入到作用域
        // 每頁顯示的具體資料
        modelMap.put("stateList", pageState.getRecords());
        // 總記錄數
        modelMap.put("total", pageState.getTotal());
        //pageSize是每頁顯示多少條
        modelMap.put("pageSize", pageState.getSize());
        // pageNo 當前頁
        modelMap.put("pageNo", pageState.getCurrent());
        //  pages分了多少頁
        modelMap.put("pages", pageState.getPages());
        // 3: 渲染的視圖模板
        return "state/listTemplate";
    }
}
package com.example.service.state;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.State;
import com.example.mapper.StateMapper;
import org.springframework.stereotype.Service;

/**
 * @Auther: 長頸鹿
 * @Date: 2021/07/30/13:33
 * @Description:
 */
@Service
public class StateServiceImpl extends ServiceImpl<StateMapper, State> implements StateService {

    @Override
    public Page<State> pageList(int pageNo, int pageSize) {
        Page<State> page = new Page<>(pageNo, pageSize);
        // 查詢分頁
        QueryWrapper<State> queryWrapper = new QueryWrapper<>();
        Page<State> pageState = this.page(page, queryWrapper);
        return pageState;
    }
}
package com.example.service.state;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.State;

/**
 * @Auther: 長頸鹿
 * @Date: 2021/07/30/13:32
 * @Description:
 */
public interface StateService extends IService<State> {

    Page<State> pageList(int pageNo, int pageSize);
}

6.3 頁面初始化分頁

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<main id="appbox" class="byte-layout-content garr-container" style="background: rgb(255, 255, 255);">
    <div class="layui-tab-item layui-show">
        <div id="LAY_preview">
            <div class="layui-border-box">
                <div class="layui-table-tool">
                    <span class="mr-4 fl">共 11 條</span>
                    <div class="layui-table-tool-selfc ml-2 fr">
                        <a href="https://www.kuangstudy.com/bbs" target="_blank" class="layui-btn layui-btn-sm">
                            <span class="iconfont iconhome mr-2 fz12"></span>
                            訪問首頁
                        </a>
                    </div>
                    <div class="layui-table-tool-selfc ml-2 fr">
                        <button class="layui-btn layui-btn-sm">
                            <i class="iconfont iconadd mr-2"></i>
                            添加一級分類
                        </button>
                    </div>
                    <div class="layui-table-tool-selfc fr">
                        <div class="layui-inline">
                            <input title="敲enter鍵盤也可以搜索!" maxlength="100" autocomplete="off"
                                   placeholder="請輸入分類標題..." class="layui-input" style="width: 320px; height: 32px; line-height: 32px;">
                        </div>
                        <button class="layui-btn layui-btn-sm">搜索</button>
                    </div>
                </div>
            </div>
            <div class="layui-table-box">
                <div class="layui-table-body layui-table-main">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table">
                        <thead>
                        <tr>
                            <th class="layui-table-col-special">
                                <div class="layui-table-cell  laytable-cell-numbers">
                                    <span>ID</span>
                                </div>
                            </th>
                            <th>
                                <div class="layui-table-cell"><span>標題</span></div>
                            </th>
                            <th>
                                <div class="layui-table-cell"><span>參與人數</span></div>
                            </th>
                            <th>
                                <div class="layui-table-cell"><span>創建時間</span></div>
                            </th>
                            <th>
                                <div class="layui-table-cell"><span>創建用戶</span></div>
                            </th>
                            <th>
                                <div class="layui-table-cell"><span>狀態</span></div>
                            </th>
                            <th>
                                <div class="layui-table-cell"><span>操作</span></div>
                            </th>
                        </tr>
                        </thead>
                        <tbody id="state-tbody" th:data-total="${total}" th:data-pages="${pages}"
                               th:data-pageSize="${pageSize}">
                            <div th:replace="~{state/listTemplate::stateList}"></div>
                        </tbody>
                    </table>
                </div>
                <!-- 翻頁 -->
                <div class="layui-table-page" id="state-page" style="height: 60px; text-align: center; margin-top: 50px;"></div>
            </div>
        </div>
    </div>

    <script>

        // 1:如何初始化,然后并且獲取每次點擊的頁碼
        // 2:如何根據分頁查詢對應的資料,把對應的資料查詢出來回傳的指定的位置

        var state = {
            page:function (total) {
                var that = this;
                layui.use(['laypage', 'layer'], function() {
                    var layPage = layui.laypage
                        , layer = layui.layer;

                    //總頁數大于頁碼總數
                    layPage.render({
                        elem: 'state-page'
                        , count: total //資料總數
                        , jump: function (obj) {
                            // 獲取當前用戶點擊當前頁
                            var currentPageNo = obj.curr;
                            // if(currentPageNo > 1){
                                // 點擊每頁渲染的資料
                                that.loadData(currentPageNo);
                            // }
                        }
                    });
                });
            },
            // 加載后臺資料
            // loadData:function (pageNo) {
            //     $.get("/admin/state/listTemplate", {pageNo:pageNo}, function (res) {
            //         // 找到第二頁渲染完畢的tbody的內容
            //         var tbodyHtml = $(res).find("#state-tbody").html();
            //         // 把找到的內容放入當前的tbody中
            //         $("#state-tbody").html(tbodyHtml);
            //     })
            // }
            loadData:function(pageNo){
                $.get("/admin/state/listTemplate",{pageNo:pageNo},function(res){
                    $("#state-tbody").html(res);
                })
            }
        };

        $(function () {
            // 獲取總記錄數
            var total = $("#state-tbody").data("total");
            // 根據total初始化分頁
            state.page(total);
        })
    </script>

</main>
</body>
</html>

核心代碼

  <tbody id="ksd-tbody" th:data-total="${total}" th:data-pages="${pages}">

1、上面的代碼是通過thymeleaf的模板渲染技術,把后臺作用域中的資料取出來,

// 每頁顯示的具體資料
modelMap.put("stateList", pageState.getRecords());
// 總記錄數
modelMap.put("total", pageState.getTotal());
//pageSize是每頁顯示多少條
modelMap.put("pageSize", pageState.getSize());
// pageNo 當前頁
modelMap.put("pageNo", pageState.getCurrent());
//  pages分了多少頁
modelMap.put("pages", pageState.getPages());

2、然后通過ajax渲染到左側位置,然后在加載JS,通過jQuery的data語法去重總記錄數,然后初始化分頁即可,

// 當前所有的節點,不包括image和iframe加載完畢,立即觸發的入口函式 
$(function(){
     // 1、把總記錄數從ksd-tbody屬性上取出來
     var total = $("#ksd-tbody").data("total");
     // 2、根據total初始化分頁即可
     ksdState.page(total);
 })

核心代碼

  <tbody id="ksd-tbody" th:data-total="${total}" th:data-pages="${pages}">

1、上面的代碼是通過thymeleaf的模板渲染技術,把后臺作用域中的資料取出來,

// 每頁顯示的具體資料
modelMap.put("stateList", pageState.getRecords());
// 總記錄數
modelMap.put("total", pageState.getTotal());
//pageSize是每頁顯示多少條
modelMap.put("pageSize", pageState.getSize());
// pageNo 當前頁
modelMap.put("pageNo", pageState.getCurrent());
//  pages分了多少頁
modelMap.put("pages", pageState.getPages());

2、然后通過ajax渲染到左側位置,然后在加載JS,通過jQuery的data語法去重總記錄數,然后初始化分頁即可,

// 當前所有的節點,不包括image和iframe加載完畢,立即觸發的入口函式 
$(function(){
     // 1、把總記錄數從ksd-tbody屬性上取出來
     var total = $("#ksd-tbody").data("total");
     // 2、根據total初始化分頁即可
     ksdState.page(total);
 })

7、洗掉

7.1 實作資料的洗掉

洗掉邏輯一般都是根據主鍵id進行處理和洗掉,洗掉分為邏輯洗掉和物理洗掉,

  • 一般開發中一般都是邏輯洗掉,執行的是 :update修改表的狀態,定義欄位is_delete 0代表未洗掉 1 洗掉
  • 物理洗掉,直接把表的資料直接刪掉,執行:delete from table where id = xxx

7.2 layui組件的引入

  • 下載layui組件:https://www.layui.com/

  • 把下載的layui組件解壓到專案的static目錄

  • 在header.html頁面引入layui.js

  • <script th:fragment="scriptbar" src="../js/jquery-3.5.1.min.js"></script>
      <script th:fragment="scriptbar" src="../layui/layui.js"></script>
    
  • 使用layui即可

    https://www.layui.com/demo/
    

7.3 后臺邏輯方法(物理洗掉)

@ResponseBody
@PostMapping("state/delete/{id}")
public int deleteState(@PathVariable("id")Integer id){
    boolean flag = stateService.removeById(id);
    return flag ? 1 : 0;
}
	delete:function () {
        var mainThat = this;
        $("#state-tbody").on("click", ".state-delete-btn", function () {
            var that = this; // that代表當前你點擊的每一個洗掉按鈕
            // 拿到快取的資料id
            var opId = $(this).data("opid");
            // 發送異步請求
            $.post("/admin/state/delete/"+opId, function (res) {
            	if(res.code == 200){
                	// 洗掉成功進行異步洗掉 $(that).parents("tr") 獲取當前洗掉按鈕的所在的tr行
                    $(that).parents("tr").fadeOut("slow", function () {
                    	//  $(this) 這個代表的當前行tr,洗掉當前行
                    	$(this).remove();
                        // 洗掉一行,總數就減去1次
                        var total = $("#state-tbody").data("total");
                        var newTotal = total-1;
                        $("#state-tbody").data("total",newTotal);
                        $(".state-total-num").text(newTotal);
                         // 如果當前頁面的資料已經全部洗掉完畢
                        var length = $("#state-tbody").children().length;
                        if(length == 0){
                            // 重新加載資料
                            mainThat.page(newTotal);
                      	}
                	})
            	}
        	})
    	})
	}

7.4 layui組件的引入和洗掉邏輯的融合

@ResponseBody
@PostMapping("state/update/{id}")
public int updateState(@PathVariable("id")Integer id){
    State state = stateService.getById(id);
    if(state != null && state.getIsDelete().equals(0)){
        state.setIsDelete(1);
    }else if(state != null && state.getIsDelete().equals(1)){
        state.setIsDelete(0);
    }
    System.out.println(state.getIsDelete());
    boolean flag = stateService.updateById(state);
    System.out.println(state.getIsDelete());
    return flag ? state.getIsDelete() : 0;
}
delete:function () {
    var mainThat = this;
    $("#state-tbody").on("click", ".state-delete-btn", function () {
        var that = this; // that代表當前你點擊的每一個洗掉按鈕
        // 拿到快取的資料id
        var opId = $(this).data("opid");
        // 發送異步請求
        layui.use('layer', function(){
            var layer = layui.layer;
            layer.confirm('確定洗掉?', {
                btn: ['確定','取消'] //按鈕
            }, function(){
                // 發送洗掉異步請求
                $.post("/admin/state/update/" + opId,function(res){
                    if(res.code == 200){
                        layer.msg('洗掉成功');
                        if(res.data == 1) {
                            $(that).parents("tr").find(".state-isDelete").removeClass("green").addClass("red").text("已洗掉");
                        }else{
                            $(that).parents("tr").find(".state-isDelete").removeClass("red").addClass("green").text("未洗掉");
                        }
                    }
                });
            }, function(){
                layer.msg('這個是取消按鈕事件');
            });
        });
    })
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/291326.html

標籤:其他

上一篇:JavaScript基礎大總結

下一篇:【讀書筆記】《你不知道的JavaScript(上卷)》——第二部分 this和物件原型(四)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • vue移動端上拉加載

    可能做得過于簡單或者比較low,請各位大佬留情,一起探討技術 ......

    uj5u.com 2020-09-10 04:38:07 more
  • 優美網站首頁,頂部多層導航

    一個個人用的瀏覽器首頁,可以把一下常用的網站放在這里,平常打開會比較方便。 第一步,HTML代碼 <script src=https://www.cnblogs.com/szharf/p/"js/jquery-3.4.1.min.js"></script> <div id="navigate"> <ul> <li class="labels labels_1"> ......

    uj5u.com 2020-09-10 04:38:47 more
  • 頁面為要加<!DOCTYPE html>

    最近因為寫一個js函式,需要用到$(window).height(); 由于手寫demo的時候,過于自信,其實對前端方面的認識也不夠體系,用文本檔案直接敲出來的html代碼,第一行沒有加上<!DOCTYPE html> 導致了$(window).height();的結果直接是整個document的高 ......

    uj5u.com 2020-09-10 04:38:52 more
  • WordPress網站程式手動升級要做好資料備份

    WordPress博客網站程式在進行升級前,必須要做好網站資料的備份,這個問題良家佐言是遇見過的;在剛開始接觸WordPress博客程式的時候,因為升級問題和博客網站的修改的一些嘗試,良家佐言是吃盡了苦頭。因為購買的是西部數碼的空間和域名,每當佐言把自己的WordPress博客網站搞到一塌糊涂的時候 ......

    uj5u.com 2020-09-10 04:39:30 more
  • WordPress程式不能升級為5.4.2版本的原因

    WordPress是一款個人博客系統,受到英文博客愛好者和中文博客愛好者的追捧,并逐步演化成一款內容管理系統軟體;它是使用PHP語言和MySQL資料庫開發的,用戶可以在支持PHP和MySQL資料庫的服務器上使用自己的博客。每一次WordPress程式的更新,就會牽動無數WordPress愛好者的心, ......

    uj5u.com 2020-09-10 04:39:49 more
  • 使用CSS3的偽元素進行首字母下沉和首行改變樣式

    網頁中常見的一種效果,首字改變樣式或者首行改變樣式,效果如下圖。 代碼: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, ......

    uj5u.com 2020-09-10 04:40:09 more
  • 關于a標簽的講解

    什么是a標簽? <a> 標簽定義超鏈接,用于從一個頁面鏈接到另一個頁面。 <a> 元素最重要的屬性是 href 屬性,它指定鏈接的目標。 a標簽的語法格式:<a href=https://www.cnblogs.com/summerxbc/p/"指定要跳轉的目標界面的鏈接">需要展示給用戶看見的內容</a> a標簽 在所有瀏覽器中,鏈接的默認外觀如下: 未被訪問的鏈接帶 ......

    uj5u.com 2020-09-10 04:40:11 more
  • 前端輪播圖

    在需要輪播的頁面是引入swiper.min.js和swiper.min.css swiper.min.js地址: 鏈接:https://pan.baidu.com/s/15Uh516YHa4CV3X-RyjEIWw 提取碼:4aks swiper.min.css地址 鏈接:https://pan.b ......

    uj5u.com 2020-09-10 04:40:13 more
  • 如何設定html中的背景圖片(全屏顯示,且不拉伸)

    1 <style>2 body{background-image:url(https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture); 3 background-size:cover;background ......

    uj5u.com 2020-09-10 04:40:16 more
  • Java學習——HTML詳解(上)

    HTML詳解 初識HTML Hyper Text Markup Language(超文本標記語言) 1 <!--DOCTYPE:告訴瀏覽器我們要使用什么規范--> 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <!--meta 描述性的標簽,描述一些 ......

    uj5u.com 2020-09-10 04:40:33 more
最新发布
  • 我的第一個NPM包:panghu-planebattle-esm(胖虎飛機大戰)使用說明

    好家伙,我的包終于開發完啦 歡迎使用胖虎的飛機大戰包!! 為你的主頁添加色彩 這是一個有趣的網頁小游戲包,使用canvas和js開發 使用ES6模塊化開發 效果圖如下: (覺得圖片太sb的可以自己改) 代碼已開源!! Git: https://gitee.com/tang-and-han-dynas ......

    uj5u.com 2023-04-20 07:59:23 more
  • 生產事故-走近科學之消失的JWT

    入職多年,面對生產環境,盡管都是小心翼翼,慎之又慎,還是難免捅出簍子。輕則滿頭大汗,面紅耳赤。重則系統停擺,損失資金。每一個生產事故的背后,都是寶貴的經驗和教訓,都是專案成員的血淚史。為了更好地防范和遏制今后的各類事故,特開此專題,長期更新和記錄大大小小的各類事故。有些是親身經歷,有些是經人耳傳口授 ......

    uj5u.com 2023-04-18 07:55:04 more
  • 記錄--Canvas實作打飛字游戲

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 打開游戲界面,看到一個畫面簡潔、卻又富有挑戰性的游戲。螢屏上,有一個白色的矩形框,里面不斷下落著各種單詞,而我需要迅速地輸入這些單詞。如果我輸入的單詞與螢屏上的單詞匹配,那么我就可以獲得得分;如果我輸入的單詞錯誤或者時間過長,那么我就會輸 ......

    uj5u.com 2023-04-04 08:35:30 more
  • 了解 HTTP 看這一篇就夠

    在學習網路之前,了解它的歷史能夠幫助我們明白為何它會發展為如今這個樣子,引發探究網路的興趣。下面的這張圖片就展示了“互聯網”誕生至今的發展歷程。 ......

    uj5u.com 2023-03-16 11:00:15 more
  • 藍牙-低功耗中心設備

    //11.開啟藍牙配接器 openBluetoothAdapter //21.開始搜索藍牙設備 startBluetoothDevicesDiscovery //31.開啟監聽搜索藍牙設備 onBluetoothDeviceFound //30.停止監聽搜索藍牙設備 offBluetoothDevi ......

    uj5u.com 2023-03-15 09:06:45 more
  • canvas畫板(滑鼠和觸摸)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>canves</title> <style> #canvas { cursor:url(../images/pen.png),crosshair; } #canvasdiv{ bo ......

    uj5u.com 2023-02-15 08:56:31 more
  • 手機端H5 實作自定義拍照界面

    手機端 H5 實作自定義拍照界面也可以使用 MediaDevices API 和 <video> 標簽來實作,和在桌面端做法基本一致。 首先,使用 MediaDevices.getUserMedia() 方法獲取攝像頭媒體流,并將其傳遞給 <video> 標簽進行渲染。 接著,使用 HTML 的 < ......

    uj5u.com 2023-01-12 07:58:22 more
  • 記錄--短視頻滑動播放在 H5 下的實作

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 短視頻已經無數不在了,但是主體還是使用 app 來承載的。本文講述 H5 如何實作 app 的視頻滑動體驗。 無聲勝有聲,一圖頂百辯,且看下圖: 網址鏈接(需在微信或者手Q中瀏覽) 從上圖可以看到,我們主要實作的功能也是本文要講解的有: ......

    uj5u.com 2023-01-04 07:29:05 more
  • 一文讀懂 HTTP/1 HTTP/2 HTTP/3

    從 1989 年萬維網(www)誕生,HTTP(HyperText Transfer Protocol)經歷了眾多版本迭代,WebSocket 也在期間萌芽。1991 年 HTTP0.9 被發明。1996 年出現了 HTTP1.0。2015 年 HTTP2 正式發布。2020 年 HTTP3 或能正... ......

    uj5u.com 2022-12-24 06:56:02 more
  • 【HTML基礎篇002】HTML之form表單超詳解

    ??一、form表單是什么

    ??二、form表單的屬性

    ??三、input中的各種Type屬性值

    ??四、標簽 ......

    uj5u.com 2022-12-18 07:17:06 more