快速开发一个带分页功能的数据列表功能v0.6

鬼谷子叔叔 - 2025-09-11 10:27

介绍

介绍


准备

准备


开发

模型

model/member.inc.php

namespace tfproject\model;

use tfphp\model\tfmodel;
use tfphp\tfphp;

class member extends tfmodel {
    public function __construct(tfphp $tfphp){
        parent::__construct($tfphp);
        $this->setDAOOneToOne("member", ["members", "member_profiles"], [["mapping"=>["mId"=>"mId"]]]);
    }
}

视图

view/tfview/template/admin/member.html

搜索表单

<div class="row search-form crud-form-search">
    <div class="col8">
        <form>
            <div class="form-group">
                <span class="form-inline">关键字:&nbsp;</span>
                <div class="form-inline s-form-inline-auto"><input type="text" name="q" class="form-control" /></div>
                <div class="form-inline"><button class="btn btn-apply">查询</button> </div>
            </div>
        </form>
    </div>
    <div class="col2" style="text-align: right;">
        <button class="btn btn-apply" data-action="add">创建</button>
    </div>
</div>

数据列表

<div class="table-box crud-table">
    <table class="table">
        <thead>
        <tr>
            <th data-field="mId" data-sortable data-sort-field="mId" data-sort-value="desc">ID</th>
            <th data-field="mName" data-sortable data-sort-field="mName">用户名</th>
            <th data-field="mStat" data-sortable data-sort-field="mStat">状态</th>
            <th data-field="crtDT">注册时间</th>
            <th data-field="logDT">登录时间</th>
            <th data-field="__opers__"></th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <div class="pagination form-group pg-form-group">
        <div class="form-inline">
        </div>
        <div class="form-inline pg-form-inline-right">
        </div>
    </div>
</div>

脚本

$(function(){
    var tableDataUrlSearchQuery = "", $table = null, $searchForm = null, $actionFormDialog = null;
    var showActionForm = function(action){
        $(".member-box").hide();
        $("." + action + "-form").show();
    }, hideActionForm = function(){
        $(".action-form").hide();
        $(".member-box").show();
    }, formOnProcessSuccess = function(data){
        if(data.errcode !== 0){
            this.form.tftips({text: data.errmsg});
            this.form.find('[name="'+data.name+'"]').focus();
        }
        else{
            $table.refresh();
            if($actionFormDialog) $actionFormDialog.hide();
            else hideActionForm();
        }
    }, formOnProcessError = function(xhr, status, error){
        this.form.tftips({text: error});
    }, formOnPostValidateError = function(rule){
        this.form.tftips({text: rule.errmsg});
        this.form.find('[name="'+rule.name+'"]').focus();
    }, attachDataActions = function(){
        $("[data-action]").unbind().click(function(){
            dataAction = $(this).attr("data-action");
            switch(dataAction){

            }
        });
    }, attachDataRowActions = function(){
        $("[data-row-action]").unbind().click(function(e){
            e.stopImmediatePropagation();
            dataRowId = $(this).attr("data-row-id");
            dataRowAction = $(this).attr("data-row-action");
            $.get("<% URL('api/admin/member/') %>" + dataRowId, function(data){
                if(data){
                    switch (dataRowAction){

                    }
                }
            });
        });
    }, driveTable = function(){
        $table = $(".member-box .table-box").tftable({
            onMakeDataUrl: function(data){
                return "<% URL('api/admin/member') %>?pn=" + data.pn + "&sorts=" + data.sorts + tableDataUrlSearchQuery;
            },
            onProcessData: function(data){
                for(var i=0;i<data.data.length;i++){
                    data.data[i].__opers__ = "";
                }
                return data;
            },
            onEmptyTableData: function(data){
                console.log(data);
                $tbody = this.table.find("tbody");
                $tbody.find("tr").remove();
                $tbody.html("<tr><td colspan='6' style='text-align: center;'>未查询到符合条件的信息</td></tr>");
            },
            afterProcessSuccess: function(){
                attachDataRowActions();
            },
            textPageInfo: "共有记录{total}条,每页显示{percentpage}条,分为{totalpage}页",
            textPageButtonHome: "第一页",
            textPageButtonPrevious: "上页",
            textPageButtonNext: "下页",
            textPageButtonLast: "最后页"
        });
        $searchForm = $(".member-box .search-form").tfform({
            onSubmitForm: function(){
                tableDataUrlSearchQuery = "&" + $(this.form).serialize();
                $table.refresh();
                return false;
            }
        });
    };
    driveTable();
    attachDataActions();
});

控制器

页面

controller/admin/member.inc.php

namespace tfproject\controller\admin;

use tfphp\system\tfpage;

class member extends tfpage {
    protected function onLoad(){

    }
}

接口

controller/api/admin/member.inc.php

namespace tfproject\controller\api\admin;

use tfphp\model\tfcrudBuilder;
use tfphp\system\tfrestfulAPI;
use tfproject\model\member;

class member extends tfrestfulAPI {
    protected function onGET(){
        // parameters validate
        $resourceValue = $this->tfphp->getRequest()->getResourceValue();
        if($resourceValue){
            // build
            $tfcrudBuilder = new tfcrudBuilder($this->tfphp);
            $ret = $tfcrudBuilder->buildDetail((new member($this->tfphp))->getO2O("member"), [
                "mId"=>$resourceValue
            ]);
            $this->JSONData($ret);
        }
        else{
            // make sql
            $gets = $this->tfphp->getRequest()->get();
            $myQ = $gets->get("q");
            $myStat = $gets->get("stat");
            $pageSize = 10;
            $sql = "select * from tfart_members";
            $ws = "";
            $params = [];
            if($myQ){
                $ws .= " and mName like '%". $myQ. "%'";
            }
            if($myStat){
                $ws .= " and mStat = @int";
                $params[] = $myStat;
            }
            if($ws) $sql .= " where ". substr($ws, 5);
            // build
            $tfcrudBuilder = new tfcrudBuilder($this->tfphp);
            $ret = $tfcrudBuilder->buildList((new member($this->tfphp))->getO2O("member"), $sql, $params, [
                "ps"=>$pageSize
            ]);
            $this->JSONData($ret);
        }
    }
}