Home PHP Framework ThinkPHP Implementation of department management functions

Implementation of department management functions

Jun 09, 2020 pm 05:02 PM
thinkphp

Implementation of department management functions

[1] Department list display

Analysis:

①控制器DeptController.class.php
②方法showList(不要使用list方法,因为list是关键词)
③模板文件:showList.html
Copy after login

Follow the steps to write

①Create the method showList to display the template

class DeptController extends Controller{
        public function showList(){
            $this->display();
        }
}
Copy after login

②Copy the template file showList.html to View/Dept (because the controller is Dept and the method is showList, so there must be a one-to-one correspondence)

③Modify the static resource path

④Revise the showList method to obtain department data. And passed to the template

public function showList(){
            $model = M('dept');//模型实例化
            $data = $model->order('sort asc')->select();//条件查询,升序排列
            $this->assign('data',$data);//变量分配传递到模板
            $this->display();
        }
Copy after login

⑤ Template traverses to read data

<table border="1" cellspacing="0">
    <tr>
        <td>id</td><td>部门</td><td>所属部门</td><td>排序</td><td>备注</td><td>编辑数据</td><td>删除数据</td>
    </tr>
    <volist name=&#39;data&#39; id=&#39;vol&#39;>
        <tr>
            <td class="num">{$vol.id}</td>
            <td class="name">{$vol.name}</td>
            <td class="procress">
                <if condition="$vol.pid == 0">
                    顶级部门                <else/>
                    二级部门                </if>
            </td>
            <td class="node">{$vol.sort}</td>
            <td class="time">{$vol.remark}</td>
            <td><a href="javascript:;">编辑</a></td>
            <td><a href="javascript:;">删除</a></td>
        </tr>
    </volist></table>
Copy after login

Note: 1. Spaces must be added to the if judgment of the template

<if condition="$vol.pid == 0">Top-level department</if><br/>

2.<else />Add/

【二】Department Editor

Analysis:

Controller: DeptController.class.php

Method: edit (display template】process submission)

Template: edit.html

Write the code step by step

(1) Write the edit method to realize template display

public function edit(){
//展示模板
 $this->display();
}
Copy after login

(2) Modify the edit button and bring the id when jumping to the page

<td><a href="__CONTROLLER__/edit/id/{$vol.id}">编辑</a></td>
Copy after login

(3) Copy the template file edit.html to the specified location, Admin/View/Dept/edit.html; Modify the static resource path

(4) Modify the edit method to display the original data

id:<input type="text" name="id" readonly="readonly" value="{$data.id}"><br/>部门:<input type="text" name="name" value="{$data.name}"><br/>所属部门:<select>
    <option value="0">顶级部门</option>
    <volist name="info" id="vol">
        <option value="{$vol.id}">{$vol.name}</option>
    </volist></select><br/>排序:<input type="text" name="sort" value="{$data.sort}"><br/>备注:<input type="text" name="id" value="{$data.remark}"><br/>
Copy after login

(5) Process the form submission page

Hidden field: Because system restrictions cannot perform batch modification, the primary key must be specified when modifying. Therefore, a hidden field must be added to pass the id

<form action="" method="post">
    id:<input type="hidden" name="id" value="{$data.id}"><br/>
    <!-- 或者$Think.get.id -->
    部门:<input type="text" name="name" value="{$data.name}"><br/>
    所属部门:    <select name="pid">
        <option value="0">顶级部门</option>
        <volist name="info" id="vol">
            <option value="{$vol.id}">{$vol.name}</option>
        </volist>
    </select><br/>
    排序:<input type="text" name="sort" value="{$data.sort}"><br/>
    备注:<input type="text" name="remark" value="{$data.remark}"><br/>
    <button>提交</button></form>
Copy after login

jquery submission:

<script type="text/javascript">
    $(document).ready(function(){
        $(&#39;button&#39;).on(&#39;click&#39;,function(){
            $(&#39;form&#39;).submit();//提交表单        })
    })</script>
Copy after login

(6) Saving of data, modifying the code after editing the edit method

public function edit(){//展示模板或者post请求            if (IS_POST){                $post = I(&#39;post.&#39;);                // dump($post);die;                $model = M(&#39;dept&#39;);                //保存操作                $result = $model->save($post);                if ($result !== false) {                    $this->success(&#39;修改成功&#39;,U(&#39;showList&#39;),3);                }else{                    $this->error(&#39;修改失败&#39;);                }            }else{                //接收id                $id=I(&#39;get.id&#39;);                //实例化模型                $model = M(&#39;dept&#39;);                //查询指定记录                $data = $model->find($id);                // 查询全部的部门信息(不包含当前级),同于下拉。因为包含自己所在级别会在递归时陷入死循环?                $info = $model->where(&#39;id != &#39;.$id)->select();                //变量分配                $this->assign(&#39;data&#39;,$data);                $this->assign(&#39;info&#39;,$info);                //展示模板                $this->display();            }        }
Copy after login

[3] Department Delete

Analysis

Controller:DeptController.class.php

Method: del

Template: Delete no template required File, because deletion is a data processing process. Same as logging out

Note: There are single deletions and batch deletions. Editing can only be done individually, not in batches.

(1) Modify the template and add a check box

>
        
            <td><a href="__CONTROLLER__/edit/id/{$vol.id}">编辑</a></td>
            
id部门 所属部门排序备注编辑数据删除
{$vol.id} {$vol.name} 顶级部门 二级部门 {$vol.sort} {$vol.remark}
Copy after login

(2) Click the delete button to achieve deletion

①Click delete to get the value of the check box (jquery accomplish). Then go to the php deletion method

<script type="text/javascript">
    $(document).ready(function(){
        $(&#39;.del&#39;).on(&#39;click&#39;,function(){
            var id = &#39;&#39;;
            $.each($(&#39;input:checkbox:checked&#39;),function(){
                id += $(this).val()+&#39;,&#39;;
            });            // 去掉最后的,通过截取字符串获取
            id = id.substring(0,id.length-1);            //带着参数跳转到del方法
            window.location.href = &#39;__CONTROLLER__/del/id/&#39;+id;//删除方法和展示方法的控制器是同级,所以用模板常量__CONTROLLER__        })
    })</script>
Copy after login

②Write the del method to achieve deletion

//True deletion---batch and single deletion

 public function del(){
            //接收参数
            $id  = I(&#39;get.id&#39;);            //模型实例化
            $model = M(&#39;dept&#39;);            //删除
            $result = $model->delete($id);            //判断结果,删除成功或失败都会跳转到列表页,所以不用加入跳转链接
            if ($result) {                $this->success(&#39;删除成功&#39;);
            }else{                $this->error(&#39;删除失败&#39;);
            }
        }
Copy after login

The above is the ThinkPHP department All management functions.

Related references:thinkphp tutorial

The above is the detailed content of Implementation of department management functions. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles