Layui实现二级数据表格

本文档展示了如何使用Layui框架来创建一个包含学院宿舍资源分配信息的表格,并实现数据的查询功能。表格通过指定id和lay-filter进行标识,配置了请求数据接口、分页、默认工具栏和自定义查询。JavaScript部分定义了查询提交事件,通过form提交触发查询。后端使用Java Spring MVC和MyBatis进行数据处理,提供查询接口,返回分页数据。
摘要由CSDN通过智能技术生成

1.html代码

详细参考layui官网文档

注意 table标签的id 和lay-filter 必须填写 后面做查询要用到

url 请求数据 接口

https://layui.itze.cn/doc/modules/table.html#parseTablela

  <table id="datagrid" lay-filter="datagrid" class="layui-table"
               lay-data="{
                id:'exportTable',
                title: '学校宿舍资源分配信息',
                height:315,
                request: {pageName: 'current', limitName: 'size'},
                url:'/dorm/allot/page',
                page:true,toolbar: '#datagrid-toolbar',
                defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10}"
              >
            <thead>
            <tr>
                <th lay-data="{type: 'checkbox',rowspan: 2}"></th>
                <th lay-data="{field:'yx', width: 200,rowspan: 2,align: 'center'}">学院名称</th>
                <th lay-data="{width: 200,colspan: 2,align: 'center'}">学院人数</th>
                <th lay-data="{width: 200,colspan: 2,align: 'center'}">已分配人数</th>
                <th lay-data="{colspan: 2,align:'center'}">未分配人数</th>
                <th lay-data="{field:'',rowspan: 2,align:'center'}">操作</th>
                <th lay-data="{field:'',rowspan: 2,align:'center'}">状态</th>
             </tr>
            <tr>
                <th lay-data="{field:'man'}">男</th>
                <th lay-data="{field:'woman'}">女</th>
                <th lay-data="{field:'manacd'}">男</th>
                <th lay-data="{field:'womanacd'}">女</th>
                <th lay-data="{field:'mannacd'}">男</th>
                <th lay-data="{field:'womannacd'}">女</th>
            </tr>
            </thead>
        </table>

2.js代码 (不做查询可不要)

 form.on('submit(datagrid-query)', function(data) {
            table.reload('datagrid', {
                page: {curr: 1},
                where : {
                    // 查询条件传值
                    xqdm : $('#xqdm').val(),
                    ssqdm : $('#ssqdm').val(),
                    sslmc : $('#sslmc').val()
                }
            },'data')
            return false;
        });

3.java代码

controller

package vip.jzbao.dhu.pc.controller.dorm;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.jzbao.dhu.common.dto.LayuiGrid;
import vip.jzbao.dhu.common.student.dto.StudentRoomAllotDto;
import vip.jzbao.dhu.common.student.service.IStudentRoomCarefulService;
import java.util.List;

/**
 * 学院宿舍资源分配情况
 */
@Controller
@RequestMapping("/dorm/allot")
@PreAuthorize("hasAuthority('dorm:allot')")
public class AllotController {

    @Autowired
    private IStudentRoomCarefulService studentRoomCarefulService;

    @GetMapping("/list")
    public void list() {
    }

    @GetMapping("/add")
    public void add() {

    }

    @PostMapping("/queryList")
    @ResponseBody
    public List<StudentRoomAllotDto> queryList() {
        return studentRoomCarefulService.queryList();
    }

    @ResponseBody
    @RequestMapping("/page")
    public LayuiGrid selectPage(Page<StudentRoomAllotDto> page) {
        Page<StudentRoomAllotDto> pages = studentRoomCarefulService.selectStudentRoomAllot(page);
        return new LayuiGrid(pages);
    }

}

service

package vip.jzbao.dhu.common.student.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import vip.jzbao.dhu.common.student.dto.StudentNewDormDto;
import vip.jzbao.dhu.common.student.dto.StudentRoomAllotDto;
import vip.jzbao.dhu.common.student.entity.StudentRoomCareful;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author 唐小恕
 * @since 2022-03-04
 */
public interface IStudentRoomCarefulService extends IService<StudentRoomCareful> {
    /**
     * 新生住宿信息
     * @param page
     * @return
     */
    Page<StudentNewDormDto> selectStudentNewDorm(Page<StudentNewDormDto> page);

    /**
     * 查询学生宿舍分配
     * @param page
     * @return
     */
    Page<StudentRoomAllotDto> selectStudentRoomAllot(Page<StudentRoomAllotDto> page);

    List<StudentRoomAllotDto> queryList();

    /**
     * 新生住宿情况 导出
     * @return
     */
    List<StudentNewDormDto> queryNewList();
}

serviceImpl

package vip.jzbao.dhu.pc.controller.dorm;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.jzbao.dhu.common.dto.LayuiGrid;
import vip.jzbao.dhu.common.student.dto.StudentRoomAllotDto;
import vip.jzbao.dhu.common.student.service.IStudentRoomCarefulService;
import java.util.List;

/**
 * 学院宿舍资源分配情况
 */
@Controller
@RequestMapping("/dorm/allot")
@PreAuthorize("hasAuthority('dorm:allot')")
public class AllotController {

    @Autowired
    private IStudentRoomCarefulService studentRoomCarefulService;

    @GetMapping("/list")
    public void list() {
    }

    @GetMapping("/add")
    public void add() {

    }

    @PostMapping("/queryList")
    @ResponseBody
    public List<StudentRoomAllotDto> queryList() {
        return studentRoomCarefulService.queryList();
    }

    @ResponseBody
    @RequestMapping("/page")
    public LayuiGrid selectPage(Page<StudentRoomAllotDto> page) {
        Page<StudentRoomAllotDto> pages = studentRoomCarefulService.selectStudentRoomAllot(page);
        return new LayuiGrid(pages);
    }

}

mapper

package vip.jzbao.dhu.common.student.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import vip.jzbao.dhu.common.student.dto.StudentNewDormDto;
import vip.jzbao.dhu.common.student.dto.StudentRoomAllotDto;
import vip.jzbao.dhu.common.student.entity.StudentRoomCareful;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 唐小恕
 * @since 2022-03-04
 */
@Mapper
public interface StudentRoomCarefulMapper extends BaseMapper<StudentRoomCareful> {
    @Select("select  c.fprq , c.xq,c.ssl,c.lc,c.fjh,s.cwh,c.xh,c.xm,c.pycc,c.yx,c.zy,c.bj,c.stay " +
            "from student_room_careful  c left join student_stay s  on  c.xsbh = s.xsbh")
    Page<StudentNewDormDto> selectStudentNewDorm(Page<StudentNewDormDto> page);

  @Select("SELECT\n" +
          "yx,\n" +
          "COUNT( CASE WHEN XB = '男' THEN '男' END ) MAN,\n" +
          "count( CASE WHEN xb = '女' THEN '女' END ) woman,\n" +
          "COUNT( CASE WHEN stay = '1' AND XB = '男' THEN '1' END ) manacd,\n" +
          "COUNT( CASE WHEN stay = '1' AND XB = '女' THEN '1' END ) womanacd,\n" +
          "COUNT( CASE WHEN stay = '0' AND XB = '男' THEN '0' END ) mannacd,\n" +
          "COUNT( CASE WHEN stay = '0' AND XB = '女' THEN '0' END ) womannacd \n" +
          "FROM\n" +
          "student_room_careful \n" +
          "GROUP BY\n" +
          "yx")
    Page<StudentRoomAllotDto> selectStudentRoomAllot(Page<StudentRoomAllotDto> page);
    @Select("SELECT\n" +
            "yx,\n" +
            "COUNT( CASE WHEN XB = '男' THEN '男' END ) MAN,\n" +
            "count( CASE WHEN xb = '女' THEN '女' END ) woman,\n" +
            "COUNT( CASE WHEN stay = '1' AND XB = '男' THEN '1' END ) manacd,\n" +
            "COUNT( CASE WHEN stay = '1' AND XB = '女' THEN '1' END ) womanacd,\n" +
            "COUNT( CASE WHEN stay = '0' AND XB = '男' THEN '0' END ) mannacd,\n" +
            "COUNT( CASE WHEN stay = '0' AND XB = '女' THEN '0' END ) womannacd \n" +
            "FROM\n" +
            "student_room_careful \n" +
            "GROUP BY\n" +
            "yx")
    List<StudentRoomAllotDto> queryList();
    @Select("select  c.fprq , c.xq,c.ssl,c.lc,c.fjh,s.cwh,c.xh,c.xm,c.pycc,c.yx,c.zy,c.bj,c.stay " +
            "from student_room_careful  c left join student_stay s  on  c.xsbh = s.xsbh")
    List<StudentNewDormDto> queryNewList();
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好,关于layui如何实现左侧表头的表格,可以使用layui表格的fixed属性来实现。具体步骤如下: 1.在HTML中定义表格,设置表格的id和lay-filter属性,用于后续的渲染和操作。 2.在JavaScript中配置表格的列属性,包括列名、列宽、列对齐方式等。需要注意的是,在左侧表头的列中,需要设置fixed属性为'left',以实现左侧表头的固定。 3.通过layui的table.render方法渲染表格,并传入之前定义的表格id、列属性等信息。 下面是一个示例代码,供参考: HTML代码: ``` <table id="demo" lay-filter="test"></table> ``` JavaScript代码: ``` //配置表格列属性 var cols = [[ {field: 'id', title: 'ID', width: 80, fixed: 'left', align: 'center'}, {field: 'username', title: '用户名', width: 120, align: 'center'}, {field: 'email', title: '邮箱', width: 200, align: 'center'}, {field: 'sex', title: '性别', width: 80, align: 'center'}, {field: 'city', title: '城市', width: 150, align: 'center'}, {field: 'sign', title: '签名', align: 'center'} ]]; //渲染表格 layui.use('table', function(){ var table = layui.table; table.render({ elem: '#demo', cols: cols, data: data }); }); ``` 其中,data是一个数组,包含了表格数据。 ### 回答2: Layui是一款轻量级、模块化的前端开发框架,可以帮助我们快速开发美观且功能强大的网页界面。要实现左侧表头的表格,在Layui中有两种方法可以实现。 第一种方法是使用Layui表格模块。我们可以通过layui.table.render()方法来渲染表格,并设置表头和表格内容。在表头中设置type为checkbox,并通过fixed属性将其固定在左侧,即可实现左侧的表头。具体代码如下: ```javascript layui.use('table', function(){ var table = layui.table; table.render({ elem: '#table', data: [ {id: 1, name: '张三', age: 18}, {id: 2, name: '李四', age: 20}, {id: 3, name: '王五', age: 22}, ], cols: [[ {type: 'checkbox', fixed: 'left'}, {field: 'id', title: 'ID'}, {field: 'name', title: '姓名'}, {field: 'age', title: '年龄'}, ]] }); }); ``` 第二种方法是使用Layui表格重载方法。我们可以在HTML中定义一个带有左侧表头的表格,然后使用table.reload()方法来重新加载表格数据,并设置表头位置为left,即可实现左侧表头。具体代码如下: ```html <div id="table" lay-filter="table"></div> ``` ```javascript layui.use('table', function(){ var table = layui.table; table.reload('table', { url: 'data.json', cols: [[ {type: 'checkbox', fixed: 'left'}, {field: 'id', title: 'ID'}, {field: 'name', title: '姓名'}, {field: 'age', title: '年龄'}, ]], initSort: { field: 'id', type: 'asc', }, done: function(){ // 数据加载完成后的回调函数 } }); }); ``` 以上就是使用Layui实现左侧表头的表格的两种方法。通过这些方法,我们可以方便地创建出符合需求的表格,并且能够灵活地对表格进行数据加载和操作。 ### 回答3: Layui是一款轻量级的前端框架,可以快速地实现各种常见的网页效果。要实现左侧表头的表格,可以按照以下步骤进行操作。 首先,在HTML文件中引入Layui的相关文件,包括CSS和JavaScript文件。可以从Layui官网下载最新版本的文件,并将它们放置在项目的相应目录下。 接下来,创建一个具有固定表头和固定列的表格结构。可以使用HTML的table标签来创建一个基本的表格,然后在相应的列上添加'Layui-table-fixed'类来实现固定列的效果。例如,可以在第一列和第一行上添加'Layui-table-fixed-left'类来实现左侧表头的效果。 在JavaScript文件中,使用Layui的table模块来初始化表格。通过调用'layui.table.render'方法并传入相应的参数,例如表格id、表头数据表格数据等,来渲染表格。 最后,根据需要可以对表格进行一些自定义的样式或功能的调整。Layui提供了丰富的API和样式类,可以根据自己的需求对表格进行进一步的个性化定制。 总结起来,实现左侧表头的表格可以通过Layui框架提供的功能和API来完成,只需要按照一定的结构和设置,即可轻松地实现这一效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值