首頁 > php框架 > ThinkPHP > ThinkPHP6搜尋器的使用

ThinkPHP6搜尋器的使用

wpj
發布: 2020-05-07 10:32:49
原創
350 人瀏覽過

    在ThinkPHP5.1.22新增模型搜尋器功能。搜尋器的作用是用於封裝欄位(或搜尋識別)的查詢條件運算式,一個搜尋器對應一個特殊的方法(該方法必須是public型別),方法命名規範為:searchFieldNameAttr。

搜尋器的情境包括:

  •     限制與規格表單的搜尋條件;

  • ##    預定義查詢條件簡化查詢;

搜尋器的比較:

  •     搜尋器通常會和查詢範圍進行比較,搜尋器無論定義了多少,只需要一次調用,查詢範圍如果需要組合查詢的時候就需要多次調用。

一、 在app\model 目錄下建立Admin.php模型

    Admin模型中定義的getByData()方法用於查詢資料並以JSON格式傳回,因為我們前端使用的是LayUI的資料表格。 searchUsernameAttr()、searchCreateTimeAttr()這兩個方法正是搜尋器 命名規格為:searchFieldNameAttr,FieldName為資料表欄位的駝峰轉換,搜尋器僅在呼叫withSearch方法的時候觸發。搜尋器方法的參數有三個,第一個是查詢對象,第二個是目前搜尋標識的值,第三個是目前所有的搜尋資料(可選)。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

<?php

 

namespace app\model;

 

use think\Exception;

use think\Model;

 

class Admin extends Model

{

 

    //查询数据(数据表格)

    public static function getByData($limit=&#39;10&#39;,$order=&#39;id desc&#39;,$field=&#39;*&#39;,$search=[],$searchKey=[])

    {

        try {

            //查询数据

            $result = self::withSearch($searchKey,$search)->order($order)->field($field)->paginate(request()->get(&#39;limit&#39;,$limit))->toArray();

        }catch (\Exception $e){

            throw new Exception(&#39;数据库内部错误!&#39;);

        }

        //返回格式为json的数据

        return json([

            &#39;code&#39;  =>  0,

            &#39;msg&#39;   =>  &#39;数据请求成功&#39;,

            &#39;count&#39; =>  $result[&#39;total&#39;],

            &#39;data&#39;  =>  $result[&#39;data&#39;]

        ]);

    }

 

    /**

     * @desc 查询用户名

     * @param $query    查询对象

     * @param $value    搜索的值

     * @param $data     搜索数据(可忽略)

     */

    public function searchUsernameAttr($query, $value, $data)

    {

        $query->where(&#39;username&#39;,&#39;like&#39;, &#39;%&#39; . $value . &#39;%&#39;);

    }

 

    /**

     * @desc 添加时间范围查询

     * @param $query

     * @param $value

     */

    public function searchCreateTimeAttr($query, $value)

    {

        $query->whereBetweenTime(&#39;create_time&#39;, $value[0], $value[1]);

    }

}

登入後複製

二、 在app\controller目錄下建立Admin.php控制器。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

<?php

 

namespace app\controller;

 

use app\BaseController;

 

use app\model\Admin as AdminModel;

 

class Admin extends BaseController

{

    //渲染页面

    public function index()

    {

        return view();

    }

 

    //数据表格接口

    public function data()

    {

        //存放搜索条件

        $search = [];

 

        //接收前台查询传递过来的参数

        $username = request()->param(&#39;username&#39;,&#39;&#39;,&#39;trim&#39;);

        $create_time  = request()->param(&#39;create_time&#39;,&#39;&#39;,&#39;trim&#39;);

 

        //如果参数不为空,把数据赋值给$search

        if (!empty($username)){

            $search[&#39;username&#39;] = $username;

        }

        if (!empty($create_time)){

            $search[&#39;create_time&#39;] = explode(&#39;~&#39;,$create_time);

        }

 

        //获取$search[] 中的key

        $searchKey = [];

 

        if (!empty($search)){

            $searchKey = array_keys($search);

        }

 

        return AdminModel::getByData(10,&#39;id desc&#39;,&#39;*&#39;,$search,$searchKey);

    }

}

登入後複製

三、渲染視圖

3.1、在專案目錄下執行以下命令:

1

composer require topthink/think-view

登入後複製

3.2、在app\view\admin資料夾下建立index.html文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

    <title>搜索器</title>

    <meta name="renderer" content="webkit">

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">

    <link rel="stylesheet" href="https://heerey525.github.io/layui-v2.4.3/layui-v2.4.4/css/layui.css"  media="all">

    <script src="https://heerey525.github.io/layui-v2.4.3/layui-v2.4.4/layui.js" charset="utf-8"></script>

    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>

</head>

 

<body>

<div class="layui-fluid">

    <div class="layui-row layui-col-space15">

        <div class="layui-col-md12">

            <div class="layui-card">

                <!-- 搜索框 -->

                <div class="layui-card-body" style="padding-bottom: 0;">

                    <div class="layui-form">

                        <div class="layui-inline layui-show-xs-block">

                            <input type="text" id="username" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">

                        </div>

                        <div class="layui-inline layui-show-xs-block">

                            <input type="text" id="test1" name="create_time" placeholder="请选择时间范围" autocomplete="off" class="layui-input">

                        </div>

                        <div class="layui-inline layui-show-xs-block">

                            <button class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i></button>

                        </div>

                    </div>

                </div>

                <div class="layui-card-body ">

                    <!--数据表格-->

                    <table id="dataTable" lay-filter="dataTable"></table>

                </div>

            </div>

        </div>

    </div>

</div>

</body>

<script>

    layui.use([&#39;layer&#39;,&#39;table&#39;,&#39;form&#39;,&#39;laydate&#39;],function () {

        var layer = layui.layer,form = layui.form,table = layui.table, laydate = layui.laydate;

 

        //日期时间范围 搜索

        laydate.render({

            elem: &#39;#test1&#39;

            , type: &#39;datetime&#39;

            , range: &#39;~&#39;

        });

 

        //用户表格初始化

        var dataTable = table.render({

            elem: &#39;#dataTable&#39;

            ,url: "/admin/data" //数据接口

            ,page: true //开启分页

            ,toolbar: "#toolbarTpl"

            ,cols: [[ //表头

                {checkbox: true,fixed: &#39;left&#39;}

                ,{field: &#39;id&#39;, title: &#39;ID&#39;, sort: true,width:70}

                ,{field: &#39;username&#39;, title: &#39;用户名&#39;}

                ,{field: &#39;create_time&#39;, title: &#39;添加时间&#39;,width:160}

            ]]

        });

 

        //搜索

        $("#search").click(function () {

            var username = $("#username").val();

            var create_time = $("#test1").val();

            dataTable.reload({

                where:{username:username,create_time:create_time},

                page:{curr:1}

            });

        });

    });

</script>

</html>

登入後複製

四、搜尋示範

ThinkPHP6搜尋器的使用#

以上是ThinkPHP6搜尋器的使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板