Home >PHP Framework >ThinkPHP >Use ThinkPHP6 to realize data table display
With the development of Internet technology, the demand for data is increasing, especially for institutions such as enterprises and organizations that need to manage and analyze data. In this context, data tables have become a very important and commonly used way of displaying data, so it has become very necessary to master how to use ThinkPHP6 to display data tables.
ThinkPHP is a popular PHP development framework that uses MVC architecture and object-oriented programming ideas. Through class library encapsulation and modular design, it can greatly improve code readability, maintainability and development. efficiency. When using ThinkPHP6 to display data tables, it also provides some convenient methods and tools. Let's introduce how to use ThinkPHP6 to display data tables.
First, we need to create tables in the database and create database model classes to call and operate these data. For example, we create a student information table, which contains four fields: id, name, age, and gender. Then we create these fields in the database, and at the same time create a StudentModel.php data model class in the Model directory of ThinkPHP to call and operate these data. The code is as follows:
namespace appmodel; use thinkModel; class StudentModel extends Model { protected $table = 'student'; public function getStudents() { return $this->field('id,name,age,gender')->order('id')->select(); } }
Next, we need to create a Controller class StudentController.php in the Controller directory of ThinkPHP to handle data logic and render the page. Among them, we can use the assign method of the Controller class to assign the data obtained from the database to the template variable for use in the view.
In terms of views, we can use front-end frameworks such as layui to render and display data tables. The idea is to provide data in json format to the front end, and then render the data into a table. For example, the following is a student information table display page implemented using layui:
The controller code is as follows:
namespace appcontroller; use appmodelStudentModel; use thinkController; class StudentController extends Controller { public function index() { $studentModel = new StudentModel(); $students = $studentModel->getStudents(); $this->assign('students', $students); return $this->fetch('index'); } }
The view code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生信息表格</title> <link rel="stylesheet" href="/layui/css/layui.css"> <script src="/layui/layui.js"></script> </head> <body> <table class="layui-table"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> {% for student in students %} <tr> <td>{{ student.id }}</td> <td>{{ student.name }}</td> <td>{{ student.age }}</td> <td>{{ student.gender }}</td> </tr> {% endfor %} </tbody> </table> </body> </html>
3. Add paging and search functions
We can also add paging and search functions to the student information form to optimize the user experience. In ThinkPHP6, it is very convenient to implement these two functions. The following are the specific steps:
(1) Pagination function
We can use the paginate() method provided by ThinkPHP6 to display the obtained student information data according to the given page display data size. Pagination, the specific code is as follows:
$students = $studentModel->paginate(10);
In the view, we can use front-end frameworks such as layui to display pagination, for example, add:
<div id="page" style="margin-top: 30px; text-align: center;"></div> <!-- 定义js --> <script> layui.use(['laypage'], function () { var laypage = layui.laypage; laypage.render({ elem: 'page', //注意,这里的 test1 是 ID,不用加 # 号 count: {{ students.total }}, //数据总数 limit: {{ students.list_rows }}, //显示的条数 curr: {{ students.currentPage }}, //当前页数 theme: '#1E9FFF', jump: function (obj, first) { //首次不执行 if (!first) { //跳转到新页面 window.location.href = '?page=' + obj.curr; } } }); }); </script>
(2) Search function# in the body
##We can use the where() method provided by ThinkPHP6 to filter student information data based on search keywords. The specific code is as follows:$keyword = input('get.keyword'); $students = $studentModel ->where('name', 'like', "%{$keyword}%") ->paginate(10);In the view, we can use front-end plug-ins such as TableFilter to Realize search display and function implementation, for example, add in the header:
<form class="layui-form" action="/" method="get" style="margin-bottom: 20px; text-align: right;"> <input type="text" name="keyword" id="keyword" placeholder="请输入姓名" autocomplete="off" class="layui-input" style="width: 200px; display: inline-block;"> <button class="layui-btn" lay-submit lay-filter="search">搜索</button> </form> <!-- 定义js --> <script> layui.use('tableFilter', function () { var tableFilter = layui.tableFilter; var tf = new tableFilter('studentTable', { filters: [{ field: 1, mode: 'like' }] }); }); </script>SummaryUsing ThinkPHP6 to realize data table display is versatile, convenient and fast, for enterprises and those who want to conduct data on the Internet Management and analysis provide extremely valuable tools to organizations and individuals. The above is how to use ThinkPHP6 to display data tables. Mastering these skills, we can easily realize various display needs and improve the efficiency of data display, management and analysis.
The above is the detailed content of Use ThinkPHP6 to realize data table display. For more information, please follow other related articles on the PHP Chinese website!