当前位置:网站首页>如何使用layui将数据库中的数据以表格的形式展现出来
如何使用layui将数据库中的数据以表格的形式展现出来
2022-07-01 07:46:00 【抗争的小青年】
layui自带的表格数据渲染
下面的所有操作的前提是你
必须引入layui!
一,先写html
<body>
<div class="layui-card">
<div class="layui-card-header">
<span class="layui-breadcrumb">
<a href="">导航管理</a>
<a><cite>查看导航</cite></a>
</span>
</div>
<div class="layui-card-body">
<form class="layui-form" action="">
<div class="layui-inline">
<button type="button" class="layui-btn layui-btn-danger" style="margin-right: 20px;"><i class="layui-icon layui-icon-delete" style="margin-right:5px;"></i>批量删除</button>
<div class="layui-input-inline" style="width: 200px;">
<input type="text" placeholder="请输入搜索内容" autocomplete="off" class="layui-input">
</div>
<button type="button" class="layui-btn"><i class="layui-icon layui-icon-search" style="margin-right:5px;"></i>搜索</button>
</div>
</form>
<!-- 这里是表格标签 -->
<table class="layui-table tableRender"></table>
</div>
</div>
</body>
注意:这里表格标签的内容是空的。
二,在页面中引入js文件,内容为:
layui.use(['element', 'table'], function () {
var {
element, table } = layui
table.render({
elem: '.tableRender',
url: "/admin/nav/query", //后端传数据的接口
cols: [[ //表头
{
field: "nav_id", title: 'ID', width: 80, sort: true, fixed: 'left' },
{
field: 'nav_name', title: '导航名称' },
{
field: 'nav_alias', title: '导航别名' },
{
field: 'nav_sort', title: '导航排序' },
{
field: 'nav_is_show', title: '是否展示' },
{
field: '', title: '操作' },
]],
page: true
});
});
引入
table模块,并解构赋值。
三,写后端数据接口,通过查询数据库,来返回数据
//查看导航页面数据
router.get("/query", (req, res) => {
const sql = "SELECT * FROM nav;"
db.query(sql, (err, results) => {
if (err) {
return res.send({
code: 1,
msg: err.msg
})
}
if (results.length) {
return res.send({
code: 0,
msg: "数据查询成功",
data: results,
count: 12, // 这里是记录总条数
})
} else {
return res.send({
code: 1,
msg: "数据信息查询失败"
})
}
})
})
在对应路由中写接口,返回数据要按照
layui的要求给,具体还的看官方文档。后端数据接口路由使用
express.js搭建。使用了
mysql2模块,实现数据库的交互。这里的db就是封装后的模块,通过引入来使用,就不在这里细说了。
四,页面渲染成功


这里只是带大家入个门,想要更高级的用法,可以去官方文档查看。
边栏推荐
- Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
- 2022危险化学品经营单位主要负责人试题及模拟考试
- Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
- 良心安利万向轮 SolidWorks模型素材网站
- Vhost kick & call principle
- [软件] phantomjs屏幕截图
- 【无标题】
- 软件测试方法和技术 - 基础知识概括
- The database is locked. Is there a solution
- 【编程强训2】排序子序列+倒置字符串
猜你喜欢
![[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched](/img/8d/cf259b9bb8574aa1842280c9661d1e.jpg)
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched

组件的自定义事件①

2022 tea master (intermediate) recurrent training question bank and answers

MySQL and redis consistency solution

2022年茶艺师(中级)复训题库及答案
![[programming training 2] sorting subsequence + inverted string](/img/96/87750c5d3954ef6c39cce073e8b9ae.png)
[programming training 2] sorting subsequence + inverted string

Download xshell and xftp

赌上了绩效,赢了公司CTO,我要搭DevOps平台!

2022 test questions and mock examinations for main principals of hazardous chemicals business units

关系数据库如何工作
随机推荐
[programming training 2] sorting subsequence + inverted string
Subclasses call methods and properties of the parent class with the same name
[R language] two /n data merge functions
[MySQL learning notes 25] SQL statement optimization
Redisson uses the full solution - redisson official documents + comments (Part 2)
kubernetes资源对象介绍及常用命令(二)
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
[kv260] generate chip temperature curve with xadc
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
[target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)
下载Xshell和Xftp
The database is locked. Is there a solution
浏览器本地存储
Eigen矩阵运算库快速上手
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
力扣——求一组字符中的第一个回文字符
The computer has a network, but all browser pages can't be opened. What's the matter?
[microservice openfeign] feign's log record
Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
Custom events of components ②