浅谈pbootcms自定义表单如何实现搜索功能

92建站   PbootCms   2022-10-25   收藏本文

本文介绍了看看pbootcms自定义表单如何实现搜索功能,pbootcms是个完美的内容管理平台,但在留言和会员系统方面还有些弱,特别是表单留言方面,居然没有搜索功能,要是留言太多,都不知道怎么查找,所以加个搜索功能还是比较有必要的。一起来看看现实方法吧。

旺铺招租1 旺铺招租2 旺铺招租3

下面来说说看看pbootcms自定义表单如何实现搜索功能。

直接上代码:

一、找到apps\admin\model\content\formmodel.html,增加

    // 字段查找
    public function findFormField($table, $page, $type, $keyword)
    {
        return parent::table($table)
            ->like($type, $keyword)
            ->order('id ASC')
            ->decode(false)
            ->page($page)
            ->select();
    }
     // 全部字段查找
    public function findFormFieldAll($table, $page, $typeall, $keyword)
    {
        return parent::table($table)
            ->like($typeall, $keyword)
            ->order('id ASC')
            ->decode(false)
            ->page($page)
            ->select();
    }

二、找到apps\admin\controller\content\formcontroller.html,修改//自定义表单下的内容为:

// 自定义表单列表
    public function index()
    {
        if ((! ! $fcode = get('fcode', 'var')) && $form = $this->model->getFormByCode($fcode)) {
            $this->assign('form', $form);
            if (get('action') == 'showdata') {
                $this->assign('showdata', true);
                $this->assign('fields', $this->model->getFormFieldByCode($fcode)); // 获取字段
                $table = $this->model->getFormTableByCode($fcode);
                //查找
                if (get('keyword')) {
                    if ($type = get('type')) {
                        $result = $this->model->findFormField($table, true, get('type'), get('keyword'));
                    } else {
                        $fields = $this->model->getFormFieldByCode($fcode);
                        foreach ($fields as $key => $value) {
                           $typeall = $value->name.','.$typeall;
                        }
                        $typeall = substr($typeall, 0, strlen($typeall)-1); 
                        $result = $this->model->findFormFieldAll($table, true, $typeall, get('keyword'));
                    }
                    $this->assign('formdata', $result);
                } elseif (get('export')) {
                    $this->assign('formdata', $this->model->getFormData($table, false));
                    header('Content-Type:application/vnd.ms-excel');
                    header('Cache-Control: max-age=0');
                    header("Content-Disposition:filename=" . $form->form_name . "-" . date("YmdHis") . ".xls");
                    $this->display('content/exform.html');
                } else {
                    $this->assign('formdata', $this->model->getFormData($table, true));
                }
            }
            if (get('action') == 'showfield') {
                $this->assign('showfield', true);
                $this->assign('fields', $this->model->getFormFieldByCode($fcode));
            }
        } else {
            $this->assign('list', true);
            if (! ! ($field = get('field', 'var')) && ! ! ($keyword = get('keyword', 'vars'))) {
                $result = $this->model->findForm($field, $keyword);
            } else {
                $result = $this->model->getList();
            }
            
            $this->assign('forms', $result);
        }
        $this->display('content/form.html');
    }

三、修改 \apps\admin\view\default\content\form.html 文件 大概 81行,{if([$showdata])} {if}中间内容替换为

             <div class="layui-tab layui-tab-brief" lay-filter="tab">
      <ul class="layui-tab-title">
        <li class="layui-this" lay-id="t1">{$form->form_name} — 数据</li>
      </ul>
      <div class="layui-tab-content">
             <div class="layui-tab-item layui-show">
           <!--搜索-->
                   <form action="{url./admin/Form/index/fcode/'.[$form->fcode].'}" method="get" class="layui-form">
                    <div class="layui-form-item nospace">
                        <div class="layui-input-inline">
                            {$pathinfo}
                            {php}$type = get('type');{/php}
                            <select name="type">
                                <option value="">全部</option>
                                {foreach $fields(key2,value2,num2)}
                                <option value="[value2->name]">[value2->description]</option>
                                {/foreach}
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" name="keyword" value="{$get.keyword}" placeholder="请输入搜索关键字" class="layui-input">
                        </div>
                        <div class="layui-input-inline">
                            <button class="layui-btn" lay-submit onclick="submit();">搜索</button>
                            <a class="layui-btn layui-btn-primary" href="{url./admin/Form/index/fcode/'.[$form->fcode].'/action/showdata}">清除搜索</a>
                        </div>
                    </div>
                </form>
                <!--搜索栏结束-->
<form action="{url./admin/Form/del/fcode/'.[$form->fcode].'}/action/deldata" method="post" id="contentForm" name="contentForm" class="layui-form" onkeydown="if(event.keyCode==13) return false;">
                    <input type="hidden" name="formcheck" value="{$formcheck}">
                    <table class="layui-table">
                        <thead>
                            <tr>
                                <th width="10"><input type="checkbox" class="checkbox" lay-ignore id="checkall" title="全选"></th>
                                <th width="30">序号</th>
                                {foreach $fields(key2,value2,num2)}                               
                                <th>[value2->description]</th>                                
                                {/foreach}
                                <th width="150">时间</th>                               
                                <th width="150">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {foreach $formdata(key,value,num)}
                            <tr>
                                <td><input type="checkbox" class="checkbox checkitem" lay-ignore name="list[]" value="[value->id]" id="checka"></td>
                                <td>[num]</td>
                                 {foreach $fields(key2,value2,num2)}
                              {php} $field=$value2->name {/php}
                                  <td>[value->$field]</td>
                            {/foreach}
                                <td>[value->create_time]</td>                                
                                <td>                                
                                   <a href="{url./admin/Form/del/id/'.$value->id.'/fcode/'.[$form->fcode].'/action/deldata}" onclick="return confirm('您确定要删除么?')" class="layui-btn layui-btn-xs layui-btn-danger" style="margin-bottom:0px" title="删除">删除</a>
                                </td>
                            </tr>
                            {/foreach}
                        </tbody>
                    </table>
                    <div class="layui-btn-group">
                       {if(session('ucode')==10001 && [$formdata])}
                     <a href="{url./admin/Form/clear/fcode/'.[$form->fcode].'}"  onclick='return confirm("您确定要清空么?")' class="layui-btn layui-btn-sm">清空记录</a>
                {/if}
                
                {if([$formdata])}
                    <a href="{url./admin/Form/index/fcode/'.[$form->fcode].'/action/showdata/export/1}" class="layui-btn layui-btn-sm">导出记录</a>
                {/if}
                        
                    </div>
                    <div class="page">{$pagebar}</div>
                </form>
              
             </div>
           </div>
      </div>

这样就可以了,有此功能需要的朋友可以试试看,记得提前备份哦~

旺铺招租4
旺铺招租5 旺铺招租5
展开