Home PHP Framework ThinkPHP Use ThinkPHP6 to realize data table display

Use ThinkPHP6 to realize data table display

Jun 21, 2023 am 08:37 AM
thinkphp data table exhibit

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.

  1. Database table creation and data model classes

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();
    }
}
Copy after login
  1. Controller and view

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');
    }
}
Copy after login

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>
Copy after login

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);
Copy after login

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>
Copy after login

(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);
Copy after login

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>
Copy after login
Summary

Using 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

HTML, CSS and jQuery: Make a data table with search functionality HTML, CSS and jQuery: Make a data table with search functionality Oct 26, 2023 am 10:03 AM

HTML, CSS and jQuery: Make a data table with search function In modern web development, data table is a frequently used element. In order to facilitate users to find and filter data, adding search functions to data tables has become an essential function. This article will introduce how to use HTML, CSS and jQuery to create a data table with search function, and provide specific code examples. 1. HTML structure First, we need to create a basic HTML structure to accommodate the data table

See all articles