国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼)

這篇具有很好參考價(jià)值的文章主要介紹了前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

目錄

一、前言

1.什么是表格

2.表格的使用范圍

二、案例實(shí)現(xiàn)

1.案例分析

①根據(jù)需求找到文檔源碼

②查詢結(jié)果在實(shí)體中沒有該屬性

2.dao層編寫

①BaseDao工具類

②UserDao編寫

3.Servlet編寫

①R工具類的介紹

②Useraction編寫

4.jsp頁面搭建

①userManage.jsp

?②userEdit.jsp

③userManage.js

④userEdit.js

5.案例效果演示

①查詢

②新增

③修改

④刪除


一、前言

Layui之動(dòng)態(tài)選項(xiàng)卡&iframe使用(附源碼)

在上一次前端框架Layui分享選項(xiàng)卡的使用中,可以看到點(diǎn)擊左側(cè)右側(cè)找不到路徑(因?yàn)檫€未編寫),所以這次基于上次的內(nèi)容進(jìn)行編寫對(duì)用戶管理頁面進(jìn)行增刪改查操作。

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

因?yàn)檫@里的url頁面我項(xiàng)目中還沒有編寫,所以404是必然的,不用管他!?。?/span>

1.什么是表格

表格(Table)是Web開發(fā)中常用的一種數(shù)據(jù)展示方式,用于以行和列的形式呈現(xiàn)結(jié)構(gòu)化數(shù)據(jù)。表格由單元格(cell)組成,每個(gè)單元格可以包含文本、圖像、鏈接等內(nèi)容。

HTML中的表格通過<table>標(biāo)簽來定義,<tr>表示表格的行,<td>表示表格的單元格??梢允褂?lt;th>標(biāo)簽定義表格的表頭(header)。

下面是一個(gè)簡單的HTML表格示例:

<table>
  <tr>
    <th>姓名</th>
    <th>年齡</th>
  </tr>
  <tr>
    <td>小明</td>
    <td>20</td>
  </tr>
  <tr>
    <td>小紅</td>
    <td>22</td>
  </tr>
</table>

在這個(gè)示例中,表格有兩列(姓名和年齡),第一行是表頭,下面兩行是數(shù)據(jù)行。每個(gè)單元格中的內(nèi)容被包含在<td>標(biāo)簽中,而表頭單元格則使用<th>標(biāo)簽。

通過使用表格,我們可以清晰地組織和顯示大量的數(shù)據(jù),例如展示員工列表、商品價(jià)格表、課程時(shí)間表等。可以使用CSS對(duì)表格進(jìn)行樣式化,調(diào)整表格的邊框樣式、背景顏色、字體樣式等,以滿足特定的設(shè)計(jì)要求。

需要注意的是,在使用表格時(shí),應(yīng)根據(jù)具體的需求選擇合適的數(shù)據(jù)結(jié)構(gòu)和布局方式。在某些情況下,使用表格可能不是最佳的解決方案,可以考慮使用其他的數(shù)據(jù)展示方式,例如列表、卡片布局等。

2.表格的使用范圍

表格在Web開發(fā)中被廣泛應(yīng)用于不同場(chǎng)景,以下是一些表格的常見使用范圍:

  • 1. 數(shù)據(jù)展示:表格是一種常見的數(shù)據(jù)展示形式,適用于展示結(jié)構(gòu)化的數(shù)據(jù)集合。例如,可以使用表格展示面向用戶的數(shù)據(jù),如員工列表、產(chǎn)品目錄、訂單信息等。
  • 2. 數(shù)據(jù)分析:表格可以用于以表格形式展示、比較和分析數(shù)據(jù)。通過對(duì)表格進(jìn)行排序、過濾和計(jì)算,可以更好地理解和解讀數(shù)據(jù)。數(shù)據(jù)分析工具和報(bào)表系統(tǒng)通常使用表格來呈現(xiàn)結(jié)果。
  • 3. 排名和比較:表格可以用于比較不同項(xiàng)的屬性或指標(biāo),從而進(jìn)行排名和排序。例如,可以使用表格展示運(yùn)動(dòng)員的成績排名、學(xué)生的考試成績等。表格可以根據(jù)特定的字段對(duì)數(shù)據(jù)進(jìn)行排序,方便查找和對(duì)比。
  • 4. 表單輸入:表格常用于用戶輸入和編輯數(shù)據(jù)的場(chǎng)景。例如,可以使用表格來創(chuàng)建注冊(cè)表單、調(diào)查問卷等。每個(gè)表格單元格可以包含各種輸入字段(如文本框、復(fù)選框、下拉菜單等),方便用戶輸入和提交數(shù)據(jù)。
  • 5. 日程安排和時(shí)間表:表格可以用于展示日程安排、課程時(shí)間表、會(huì)議日程等。每個(gè)單元格可以表示一個(gè)時(shí)間段,每一行代表一個(gè)時(shí)間點(diǎn)或一個(gè)實(shí)體(如人員、地點(diǎn)等)。
  • 6. 響應(yīng)式布局:表格可以通過CSS和響應(yīng)式設(shè)計(jì)技術(shù)進(jìn)行適應(yīng)性布局,使其在不同屏幕尺寸下保持良好的顯示效果。通過使用滾動(dòng)、隱藏列、自適應(yīng)寬度等功能,可以在小屏幕設(shè)備上顯示較大的表格。

需要注意的是,在使用表格時(shí)應(yīng)考慮數(shù)據(jù)的復(fù)雜性和可讀性。對(duì)于大型數(shù)據(jù)集或復(fù)雜的數(shù)據(jù)結(jié)構(gòu),可以使用分頁、篩選和搜索功能來改善用戶體驗(yàn)。另外,合理地使用樣式和交互效果可以提升表格的可用性和美觀性。

二、案例實(shí)現(xiàn)

1.案例分析

①根據(jù)需求找到文檔源碼

Layui?

這一次我們運(yùn)用的組件是表格所以在官網(wǎng)中找到想要的組件內(nèi)容進(jìn)行“cv”操作即可。

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

②查詢結(jié)果在實(shí)體中沒有該屬性

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

我們可以觀察到表中有字段rid(角色身份),但是數(shù)據(jù)展示的時(shí)候需要將數(shù)字轉(zhuǎn)換成相對(duì)應(yīng)的字符,所以我們查詢表的時(shí)候就要追加一個(gè)字段用來顯示角色身份,但是我們eclipse的實(shí)體中并沒有該屬性,如果再寫一個(gè)的以后遇到這種問題還是需要進(jìn)行編寫比較麻煩,所以我們可以采用Map集合的方式進(jìn)行保存數(shù)據(jù)。

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

跟多細(xì)節(jié)在編寫過程中慢慢講解,下面請(qǐng)欣賞小編所寫代碼?。?!

2.dao層編寫

①BaseDao工具類

package com.zking.util;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所有Dao層的父類 BookDao UserDao OrderDao ...
 * 
 * @author Administrator
 *
 * @param <T>
 */
public class BaseDao<T> {

	/**
	 * 適合多表聯(lián)查的數(shù)據(jù)返回
	 * @param sql
	 * @param pageBean
	 * @return
	 * @throws SQLException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public List<Map<String, Object>> executeQuery(String sql, PageBean pageBean)
			throws SQLException, InstantiationException, IllegalAccessException {

		List<Map<String, Object>> list = new ArrayList<>();
		Connection con = DBAccess.getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;

		/*
		 * 是否需要分頁? 無需分頁(項(xiàng)目中的下拉框,查詢條件教員下拉框,無須分頁) 必須分頁(項(xiàng)目中列表類需求、訂單列表、商品列表、學(xué)生列表...)
		 */
		if (pageBean != null && pageBean.isPagination()) {
			// 必須分頁(列表需求)
			String countSQL = getCountSQL(sql);
			pst = con.prepareStatement(countSQL);
			rs = pst.executeQuery();
			if (rs.next()) {
				pageBean.setTotal(String.valueOf(rs.getObject(1)));
			}

			// 挪動(dòng)到下面,是因?yàn)樽詈蟛盘幚矸祷氐慕Y(jié)果集
			// -- sql=SELECT * FROM t_mvc_book WHERE bname like '%圣墟%'
			// -- pageSql=sql limit (page-1)*rows,rows 對(duì)應(yīng)某一頁的數(shù)據(jù)
			// -- countSql=select count(1) from (sql) t 符合條件的總記錄數(shù)
			String pageSQL = getPageSQL(sql, pageBean);// 符合條件的某一頁數(shù)據(jù)
			pst = con.prepareStatement(pageSQL);
			rs = pst.executeQuery();
		} else {
			// 不分頁(select需求)
			pst = con.prepareStatement(sql);// 符合條件的所有數(shù)據(jù)
			rs = pst.executeQuery();
		}

		// 獲取源數(shù)據(jù)
		ResultSetMetaData md = rs.getMetaData();
		int count = md.getColumnCount();
		Map<String, Object> map = null;
		while (rs.next()) {
			map = new HashMap<>();
			for (int i = 1; i <= count; i++) {
//				map.put(md.getColumnName(i), rs.getObject(i));
				map.put(md.getColumnLabel(i), rs.getObject(i));
			}
			list.add(map);
		}
		return list;

	}

	/**
	 * 
	 * @param sql
	 * @param attrs
	 *            map中的key
	 * @param paMap
	 *            jsp向后臺(tái)傳遞的參數(shù)集合
	 * @return
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public int executeUpdate(String sql, String[] attrs, Map<String, String[]> paMap) throws SQLException,
			NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		Connection con = DBAccess.getConnection();
		PreparedStatement pst = con.prepareStatement(sql);
		for (int i = 0; i < attrs.length; i++) {
			pst.setObject(i + 1, JsonUtils.getParamVal(paMap, attrs[i]));
		}
		return pst.executeUpdate();
	}

	/**
	 * 批處理
	 * @param sqlLst
	 * @return
	 */
	public static int executeUpdateBatch(String[] sqlLst) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = DBAccess.getConnection();
			// 設(shè)置不自動(dòng)提交
			conn.setAutoCommit(false);
			for (String sql : sqlLst) {
				stmt = conn.prepareStatement(sql);
				stmt.executeUpdate();
			}
			conn.commit();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw new RuntimeException(e1);
			}
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			DBAccess.close(conn, stmt, null);
		}
		return sqlLst.length;
	}

	/**
	 * 通用的增刪改方法
	 * 
	 * @param book
	 * @throws Exception
	 */
	public int executeUpdate(String sql, T t, String[] attrs) throws Exception {
		// String[] attrs = new String[] {"bid", "bname", "price"};
		Connection con = DBAccess.getConnection();
		PreparedStatement pst = con.prepareStatement(sql);
		// pst.setObject(1, book.getBid());
		// pst.setObject(2, book.getBname());
		// pst.setObject(3, book.getPrice());
		/*
		 * 思路: 1.從傳進(jìn)來的t中讀取屬性值 2.往預(yù)定義對(duì)象中設(shè)置了值
		 * 
		 * t->book f->bid
		 */
		for (int i = 0; i < attrs.length; i++) {
			Field f = t.getClass().getDeclaredField(attrs[i]);
			f.setAccessible(true);
			pst.setObject(i + 1, f.get(t));
		}
		return pst.executeUpdate();
	}

	/**
	 * 通用分頁查詢
	 * 
	 * @param sql
	 * @param clz
	 * @return
	 * @throws Exception
	 */
	public List<T> executeQuery(String sql, Class<T> clz, PageBean pageBean) throws Exception {
		List<T> list = new ArrayList<T>();
		Connection con = DBAccess.getConnection();
		;
		PreparedStatement pst = null;
		ResultSet rs = null;

		/*
		 * 是否需要分頁? 無需分頁(項(xiàng)目中的下拉框,查詢條件教員下拉框,無須分頁) 必須分頁(項(xiàng)目中列表類需求、訂單列表、商品列表、學(xué)生列表...)
		 */
		if (pageBean != null && pageBean.isPagination()) {
			// 必須分頁(列表需求)
			String countSQL = getCountSQL(sql);
			pst = con.prepareStatement(countSQL);
			rs = pst.executeQuery();
			if (rs.next()) {
				pageBean.setTotal(String.valueOf(rs.getObject(1)));
			}

			// 挪動(dòng)到下面,是因?yàn)樽詈蟛盘幚矸祷氐慕Y(jié)果集
			// -- sql=SELECT * FROM t_mvc_book WHERE bname like '%圣墟%'
			// -- pageSql=sql limit (page-1)*rows,rows 對(duì)應(yīng)某一頁的數(shù)據(jù)
			// -- countSql=select count(1) from (sql) t 符合條件的總記錄數(shù)
			String pageSQL = getPageSQL(sql, pageBean);// 符合條件的某一頁數(shù)據(jù)
			pst = con.prepareStatement(pageSQL);
			rs = pst.executeQuery();
		} else {
			// 不分頁(select需求)
			pst = con.prepareStatement(sql);// 符合條件的所有數(shù)據(jù)
			rs = pst.executeQuery();
		}

		while (rs.next()) {
			T t = clz.newInstance();
			Field[] fields = clz.getDeclaredFields();
			for (Field f : fields) {
				f.setAccessible(true);
				f.set(t, rs.getObject(f.getName()));
			}
			list.add(t);
		}
		return list;
	}

	/**
	 * 將原生SQL轉(zhuǎn)換成符合條件的總記錄數(shù)countSQL
	 * 
	 * @param sql
	 * @return
	 */
	private String getCountSQL(String sql) {
		// -- countSql=select count(1) from (sql) t 符合條件的總記錄數(shù)
		return "select count(1) from (" + sql + ") t";
	}

	/**
	 * 將原生SQL轉(zhuǎn)換成pageSQL
	 * 
	 * @param sql
	 * @param pageBean
	 * @return
	 */
	private String getPageSQL(String sql, PageBean pageBean) {
		// (this.page - 1) * this.rows
		// pageSql=sql limit (page-1)*rows,rows
		return sql + " limit " + pageBean.getStartIndex() + "," + pageBean.getRows();
	}
}

小貼士:

結(jié)合上面的案例需求,這里重構(gòu)了executeQuery,可以發(fā)現(xiàn)這里的返回值不再是集合實(shí)體而是list集合里面存放Map集合,這樣就算來再多字段我們也不用重新編寫我們的實(shí)體類,直接調(diào)用該方法,利用Map進(jìn)行存儲(chǔ)即可。

②UserDao編寫

package com.xw.dao;

import java.util.List;
import java.util.Map;

import com.xw.entity.User;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;

/**
 * 用戶
 * 
 * @author 索隆
 *
 */
public class UserDao extends BaseDao<User> {
	// 分頁模糊查詢
	public List<User> list(User user, PageBean pageBean) throws Exception {
		// sql語句編寫
		String sql = "select * from t_oa_user where 1=1";
		// 獲取參數(shù)
		long id = user.getId();
		String name = user.getName();
		// 進(jìn)行參數(shù)非空判斷
		if (StringUtils.isNotBlank(name)) {
			sql += " and name like '%" + name + "%'";
		}
		// 如果bid不等于0說明傳遞過來了bid,此時(shí)的bid是有值的
		if (id != 0) {
			sql += "and id=" + id;
		}
		return super.executeQuery(sql, User.class, pageBean);
	}

	// 新增
	public int add(User User) throws Exception {
		int n=0;
		// sql語句編寫
		String sql = "insert into t_oa_user (name,loginName,pwd)values(?,?,?)";
		 n = super.executeUpdate(sql, User, new String[] { "name", "loginName", "pwd" });
		return n;
	}

	// 刪除
	public int del(User User) throws Exception {
		// sql語句編寫
		String sql = "delete from t_oa_user where id=?";
		return super.executeUpdate(sql, User, new String[] { "id" });
	}

	// 修改
	public int edit(User User) throws Exception {
		// sql語句編寫
		String sql = "update t_oa_user set name=?,loginName=?,pwd=? where id=?";
		return super.executeUpdate(sql, User, new String[] { "name", "loginName", "pwd", "id" });
	}

	// 登錄校驗(yàn)
	public User login(User User) throws Exception {
		// sql語句編寫
		String sql = "select * from t_oa_user where loginName='" + User.getLoginName() + "' and pwd='" + User.getPwd()+ "' ";
		List<User> list = super.executeQuery(sql, User.class, null);
		if (list != null && list.size() == 1) {
			return list.get(0);
		} else {
			return null;
		}
	}

	
	//不包含原表字段的分頁模糊查詢
	public List<Map<String, Object>> userRole(User user, PageBean pageBean) throws Exception {
		// sql語句編寫
		String sql = "select u.*,\r\n" + 
				"(CASE \r\n" + 
				"	WHEN u.rid='1' THEN	'管理員'\r\n" + 
				"	WHEN u.rid='2' THEN	'發(fā)起者'\r\n" + 
				"	WHEN u.rid='3' THEN	'審批者'\r\n" + 
				"	WHEN u.rid='4' THEN	'參與者'\r\n" + 
				"	WHEN u.rid='5' THEN	'會(huì)議室管理員'\r\n" + 
				"\r\n" + 
				"	ELSE\r\n" + 
				"		'其他'\r\n" + 
				"END ) rname\r\n" + 
				"from t_oa_user u WHERE 1=1";
		// 獲取參數(shù)
		String name = user.getName();
		// 進(jìn)行參數(shù)非空判斷
		if (StringUtils.isNotBlank(name)) {
			sql += " and name like '%" + name + "%'";
		}
		return super.executeQuery(sql,pageBean);
	}

}

注意:這里的返回就是已經(jīng)重構(gòu)的executeQuery不要搞混了哦??!

3.Servlet編寫

①R工具類的介紹

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

我們可以查看官網(wǎng)的表格內(nèi)容顯示的JSON格式是什么樣子的,發(fā)現(xiàn)有以上幾個(gè)屬性:

code,msg,count,data

  • code:只能填寫“0”(別人的Layui規(guī)則)
  • mag:沒實(shí)際含義,可隨意填寫
  • count:查詢到的條數(shù)
  • data:實(shí)體內(nèi)容

我們想要在頁面顯示數(shù)據(jù),那么就必須要滿足它的JSON格式,我們就可以利用R工具類來簡化我們的代碼,不然每次遇到這種場(chǎng)景就需要手動(dòng)創(chuàng)建Map集合然后.put添加屬性的方式造數(shù)據(jù)。

package com.zking.util;

import java.util.HashMap;

public class R extends HashMap{
	public R data(String key, Object value) {
		this.put(key, value);
		return this;
	}
	
	public static R ok(int code, String msg) {
		R r = new R();
		r.data("success", true).data("code", code).data("msg", msg);
		return r;
	}
	
	public static R error(int code, String msg) {
		R r = new R();
		r.data("success", false).data("code", code).data("msg", msg);
		return r;
	}
	
	public static R ok(int code, String msg,Object data) {
		R r = new R();
		r.data("success", true).data("code", code).data("msg", msg).data("data", data);
		return r;
	}
	
	public static R ok(int code, String msg, long count, Object data) {
		R r = new R();
		r.data("success", true).data("code", code).data("msg", msg).data("count", count).data("data", data);
		return r;
	}
}

?這里也重構(gòu)了和多次ok的方法,方便我們利用于不同的場(chǎng)景。

②Useraction編寫

package com.xw.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xw.entity.User;
import com.xw.dao.UserDao;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.PageBean;
import com.zking.util.R;
import com.zking.util.ResponseUtil;

public class Useraction extends ActionSupport implements ModelDriver<User> {
	User user = new User();
	UserDao userdao = new UserDao();

	/**
	 * 登錄驗(yàn)證
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User login = userdao.login(user);
		// 保存域?qū)ο?		if (login != null) {
			HttpSession session = req.getSession();
			session.setAttribute("user", login);
		}
		ResponseUtil.writeJson(resp, login);

	}


	/**
	 * 注冊(cè)新增用戶
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void register(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int i = userdao.add(user);
		if (i > 0) {
			ResponseUtil.writeJson(resp, i);
		} else {
			ResponseUtil.writeJson(resp, null);
		}
	}
	
	
	
	/**帶分類管理的模糊查詢
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void UserRole(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		// 初始化pageBean
		PageBean pageBean = new PageBean();
		pageBean.setRequest(req);
		//調(diào)用帶分頁查詢的方法
		List<Map<String, Object>> userRole = userdao.userRole(user, pageBean);
		//利用R工具類制造出符合要求的json格式數(shù)據(jù)
		 ResponseUtil.writeJson(resp, R.ok(0, "查詢成功", pageBean.getTotal(), userRole));

	}
	
	
	/**刪除用戶
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void del(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//調(diào)用刪除的方法
		int del = userdao.del(user);
		//利用R工具類制造出符合要求的json格式數(shù)據(jù)
		 ResponseUtil.writeJson(resp, R.ok(0, "刪除成功"));

	}
	
	
	/**修改用戶
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//調(diào)用修改的方法
		int del = userdao.edit(user);
		//利用R工具類制造出符合要求的json格式數(shù)據(jù)
		 ResponseUtil.writeJson(resp, R.ok(0, "修改成功"));

	}
	
	
	/**新增用戶
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//調(diào)用新增的方法
		int del = userdao.add(user);
		//利用R工具類制造出符合要求的json格式數(shù)據(jù)
		 ResponseUtil.writeJson(resp, R.ok(0, "新增成功"));

	}
	
	
	

	@Override
	public User getModel() {
		return user;
	}

}

4.jsp頁面搭建

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

一定要注意我們的層級(jí)結(jié)構(gòu)

①userManage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@ include file="/common/static.jsp"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用戶管理</title>
<script type="text/javascript" src="static/js/system/userManage.js"></script>
</head>

<body>
<!-- 搜索欄 -->
	<div class="layui-form-item">
	  <div class="layui-inline">
	    <label class="layui-form-label">用戶名:</label>
	    <div class="layui-input-inline">
	      <input type="text" id="name" placeholder="請(qǐng)輸入用戶名" autocomplete="off" class="layui-input">
	    </div>
	  </div>
	  
	  <div class="layui-inline">
	    <div class="layui-input-inline">
	      <button id="btn_search" type="button" class="layui-btn layui-btn-normal">
	      	<i class="layui-icon layui-icon-search"></i>
	      	查詢
	      </button>
	      <button id="btn_add" type="button" class="layui-btn">新增</button>
	    </div>
	  </div>
	  
	</div>
 
<table class="layui-hide" id="test" lay-filter="test"></table>
 

 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">編輯</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">刪除</a>
</script>
              
          
 
<script>

</script>

</body>
</html>

?②userEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/static.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="static/js/system/userEdit.js"></script>
<title>用戶新增</title>
</head>
<style>
.layui-form-select dl{
	max-height:150px;
}
</style>
<body>
<div style="padding:10px;">
    <form class="layui-form layui-form-pane" lay-filter="user">
        <input type="hidden" name="id"/>
        <div class="layui-form-item">
            <label class="layui-form-label">用戶名稱</label>
            <div class="layui-input-block">
                <input type="text" id="name" name="name" autocomplete="off" placeholder="請(qǐng)輸入用戶名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用戶角色</label>
            <div class="layui-input-block">
                <select name="rid">
                    <option value="">---請(qǐng)選擇---</option>
                    <option value="1">管理員</option>
                    <option value="2">發(fā)起者</option>
                    <option value="3">審批者</option>
                    <option value="4">參與者</option>
                    <option value="5">會(huì)議管理員</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">登錄賬號(hào)</label>
            <div class="layui-input-block">
                <input type="text" name="loginName" lay-verify="required" placeholder="請(qǐng)輸入賬號(hào)" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">登錄密碼</label>
            <div class="layui-input-block">
                <input type="password" name="pwd" placeholder="請(qǐng)輸入密碼" autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>
</body>
</html>

將JSP頁面的邏輯代碼分離

③userManage.js

var table,$,layer;
var row;

layui.use(['table','jquery','layer'], function(){
  table = layui.table
  ,layer=layui.layer
  ,$=layui.jquery;
  
  initTable();
  
  //為查詢按鈕設(shè)置點(diǎn)擊事件
  $("#btn_search").click(function(){
	  query();
  })
  
  //為新增按鈕設(shè)置點(diǎn)擊事件
  $("#btn_add").click(function(){
	  row=null;
	  edit('新增');
  })
  
});

//新增/修改的點(diǎn)擊事件
function edit(title){
	  layer.open({
	       type: 2,                    //layer提供了5種層類型。可傳入的值有:0(信息框,默認(rèn))1(頁面層)2(iframe層)3(加載層)4(tips層)
	       title:title,
	       area: ['660px', '340px'],   //寬高
	       skin: 'layui-layer-rim',    //樣式類名
	       content:  'jsp/system/userEdit.jsp', //書本編輯頁面
	       btn:['保存','關(guān)閉'],
	       yes: function(index, layero){
	    	   //調(diào)用子頁面中提供的getData方法,快速獲取子頁面的form表單數(shù)據(jù)
	           let data= $(layero).find("iframe")[0].contentWindow.getData();
	           console.log(data);
	           //判斷title標(biāo)題
	           let methodName="add";
	           if(title=="編輯")
	        	   methodName="edit";
	           $.post('user.action?methodName='+methodName,
	        		   data,function(rs){
	        	   if(rs.success){
	        		   //關(guān)閉對(duì)話框
	        		   layer.closeAll();
	        		   //調(diào)用查詢方法刷新數(shù)據(jù)
	        		   query();
	        	   }else{
	        		   layer.msg(rs.msg,function(){});
	        	   }
	           },'json');
	       },
	       btn2: function(index, layero){
	    	   layer.closeAll();
	       }
	    });
}



//查詢的點(diǎn)擊事件
function query(){
	//表格的重載
	table.reload('test', {
		  where: { //設(shè)定異步數(shù)據(jù)接口的額外參數(shù),任意設(shè)
		    name: $("#name").val()
		  }
	//默認(rèn)limit需要改成我們自己工具類的名字
		,request: {
	    pageName: 'page ' //頁碼的參數(shù)名稱,默認(rèn):page
	    ,limitName: 'rows' //每頁數(shù)據(jù)量的參數(shù)名,默認(rèn):limit
	  }
		}); //只重載數(shù)據(jù)
}




//將分頁查詢方法封裝
function initTable(){
	 table.render({
		    elem: '#test'
		    ,url:'user.action?methodName=UserRole'
		    ,toolbar: '#toolbarDemo' //開啟頭部工具欄,并為其綁定左側(cè)模板
		    ,defaultToolbar: ['filter', 'exports', 'print', { //自定義頭部工具欄右側(cè)圖標(biāo)。如無需自定義,去除該參數(shù)即可
		      title: '提示'
		      ,layEvent: 'LAYTABLE_TIPS'
		      ,icon: 'layui-icon-tips'
		    }]
		    ,title: '用戶數(shù)據(jù)表'
		    ,cols: [[
		      {type: 'checkbox', fixed: 'left'}
		      ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
		      ,{field:'loginName', title:'賬戶名', width:120, edit: 'text'}
		      ,{field:'name', title:'真實(shí)姓名', width:150, edit: 'text', templet: function(res){
		        return '<em>'+ res.name +'</em>'
		      }}
		      ,{field:'pwd', title:'密碼', width:80, edit: 'text', sort: true}
		      ,{field:'rname', title:'身份', width:100}
		      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
		    ]]
		    ,page: true
		  });
		  
		  
		  
		  //監(jiān)聽行工具事件
		  table.on('tool(test)', function(obj){
		     row = obj.data;
		    if(obj.event === 'del'){
		    	  layer.confirm('確認(rèn)刪除嗎?', {icon: 3, title:'提示'}, function(index){
					  $.post('user.action',{
						  'methodName':'del',
						  'id':row.id
					  },function(rs){
						  if(rs.success){
			        		   //調(diào)用查詢方法刷新數(shù)據(jù)
			        		   query();
			        	   }else{
			        		   layer.msg(rs.msg,function(){});
			        	   }
					  },'json');
					  layer.close(index);
					});
		    } else if(obj.event === 'edit'){
		    	 edit('編輯');
		    }
		  });
}

④userEdit.js

let layer,form,$;
layui.use(['layer','form','jquery'],function(){
	layer=layui.layer,form=layui.form,$=layui.jquery;
	initData();
});

function initData(){
	console.log(parent.row);
	if(null!=parent.row){
	     //因?yàn)閘ayui.each內(nèi)部的邏輯問題導(dǎo)致的所以要先深拷貝一份然后再去val
	     //parent.row:表格行對(duì)象
	     form.val('user',$.extend({}, parent.row||{}));
	     $('#name').attr('readonly','readonly');
	}
}

function getData(){
    return form.val('user');
}

5.案例效果演示

①查詢

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

②新增

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

③修改

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

④刪除

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查

到這里我的分享就結(jié)束了,歡迎到評(píng)論區(qū)探討交流?。?/span>

如果覺得有用的話還請(qǐng)點(diǎn)個(gè)贊吧??? ?

前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼),Layui教學(xué),layui,java,前端,管理系統(tǒng),表格,增刪改查文章來源地址http://www.zghlxwxcb.cn/news/detail-567957.html

到了這里,關(guān)于前端框架Layui實(shí)現(xiàn)動(dòng)態(tài)表格效果用戶管理實(shí)例(對(duì)表格進(jìn)行CRUD操作-附源碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包