首页
登录 | 注册

discuz学习记录:前面的数据用EasyUI DataGrid显示,实现了服务器端分页


    考虑到安全因素,继续使用入口文件来控制后面的模板文件及控制文件。

    重点:

    1、服务器端,class文件根据分页参数返回json数据格式。

    2、EasyUI DataGrid的前端分页设置。


    运行结果:http://localhost/bgtj.php

discuz学习记录:前面的数据用EasyUI DataGrid显示,实现了服务器端分页


涉及文件:

/bgtj.PHP
/source/module/cat/bgtj_publish.php
/source/class/table/table_bgtj.php
/template/default/cat/bgtj_publish.htm

/template/default/common/header_cat.htm  (这个里面增加了EasyUI DateGrid 的支持文件)


下载


/bgtj.PHP:

<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: bgtj.php  cuibq $
 */


define('APPTYPEID', 101);
define('CURSCRIPT', 'bgtj');

require './source/class/class_core.php';

$discuz = C::app();

$discuz->reject_robot();
$modarray = array('bgtj', 'bgtj_publish');


$mod = getgpc('mod');
$mod = (empty($mod) || !in_array($mod, $modarray)) ? 'error' : $mod;


$discuz->init();

define('CURMODULE', $mod);

require DISCUZ_ROOT.'./source/module/cat/bgtj_publish.php';
//include_once template("cat/bgtj_publish");
?>

/source/module/cat/bgtj_publish.php:


<?php
// mod文件只能被入口文件引用,不能直接访问
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

require_once libfile('function/discuzcode');

// -------------------------------------------
// 分页
require_once libfile('function/home');

$perpage = empty($_GET['rows'])?0:intval($_GET['rows']);//页码  
$perpage = mob_perpage($perpage);  
  
$page = empty($_GET['page'])?0:intval($_GET['page']);//页码  
if($page<1) $page = 1;  
$start = ($page-1)*$perpage;//开始记录  

//ckstart($start, $perpage);//分页判断  

//$count = C::t('bgtj')->count();//所有记录数量
//$mpurl = 'bgtj.php';
// ============================================

$fmt = $_GET['fmt'];
$fmt = dhtmlspecialchars($fmt);

//$querydata = C::t('bgtj')->fetch_all_by_sql($start, $perpage);

//$queryarr = array();
//foreach ($querydata as $rec) {
	//$queryarr[] = $rec;
//}

//$multipage = multi($count, $perpage, $page, $mpurl);


//echo 'aaa:'.$fmt;
//exit;

//$get_json = C::t('bgtj')->fetch_json_by_sql($start, $perpage);
// fmg请求 json 格式
if ($fmt == 'json'){ 
	// 取得json格式数据
	$get_json = C::t('bgtj')->fetch_json_by_sql($start, $perpage);
	echo $get_json;
	exit;
}
else {
	//显示发布表单
	include_once template("cat/bgtj_publish");
}
?>



/source/class/table/table_bgtj.php:

<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *      工业产品表
 *      $Id: table_gycp.php
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_bgtj extends discuz_table
{
	public function __construct() {

		$this->_table = 'bgtj';
		$this->_pk    = 'id';

		parent::__construct();
	}

	public function fetch_all_by_displayorder() {
		return DB::fetch_all('SELECT * FROM %t ORDER BY id ASC', array($this->_table), $this->_pk);
	}

	public function fetch_all_by_sql($start, $limit) {
		return DB::fetch_all('SELECT * FROM %t ORDER BY id ASC'.DB::limit($start, $limit), array($this->_table), $this->_pk);
	}
	
	//返回json格式数据
	public function fetch_json_by_sql($start, $limit) {
		$query = DB::query('SELECT * FROM %t ORDER BY id ASC'.DB::limit($start, $limit), array($this->_table), $this->_pk);
		
		$result = array();
		
		// 记录数量
		$row = DB::fetch_first('SELECT count(*) as cnt FROM %t ', array($this->_table), $this->_pk); //所有记录数量
		$result["total"] = intval($row['cnt']);

		while($data = DB::fetch($query)){
			$list[] = array(  
				'ID' => $data['ID'],  
				'XH' => $data['XH'],
				'WT' => urlencode($data['WT']),
				'DA' => urlencode($data['DA'])
			);  
		}  
		$result["rows"] = $list;
  
		return urldecode(json_encode($result));  
	}

}

?>



/template/default/cat/bgtj_publish.htm:

<!--//说明: 显示公共头部模板-->
<!--{template common/header_cat}-->



<br>
<table class="easyui-datagrid" title="公务员考试常识题" style="width:1000px;height:450px"
		data-options="
			singleSelect:true,
			collapsible:true,
			url:'/upload/bgtj.php?fmt=json',
			pagination:true,
			method:'get',
			" 
		fitcolumns="true" 
		striped="true">
	<thead>
		<tr>
			<th data-options="field:'ID',width:10">ID</th>
			<th data-options="field:'XH',width:10">序号</th>
			<th data-options="field:'WT',width:80,align:'left'">问题</th>
			<th data-options="field:'DA',width:40,align:'left'">答案</th>
		</tr>
	</thead>
</table>

<!--//说明: 显示公共尾部模板-->
<!--{template common/footer}-->

/template/default/common/header_cat.htm:在header.htm基础上,增加了 <!--cbq--> 包含的部分

<!--{subtemplate common/header_common}-->
	<meta name="application-name" content="$_G['setting']['bbname']" />
	<meta name="msapplication-tooltip" content="$_G['setting']['bbname']" />
	<!--{if $_G['setting']['portalstatus']}--><meta name="msapplication-task" content="name=$_G['setting']['navs'][1]['navname'];action-uri={echo !empty($_G['setting']['domain']['app']['portal']) ? 'http://'.$_G['setting']['domain']['app']['portal'] : $_G[siteurl].'portal.php'};icon-uri={$_G[siteurl]}{IMGDIR}/portal.ico" /><!--{/if}-->
	<meta name="msapplication-task" content="name=$_G['setting']['navs'][2]['navname'];action-uri={echo !empty($_G['setting']['domain']['app']['forum']) ? 'http://'.$_G['setting']['domain']['app']['forum'] : $_G[siteurl].'forum.php'};icon-uri={$_G[siteurl]}{IMGDIR}/bbs.ico" />
	<!--{if $_G['setting']['groupstatus']}--><meta name="msapplication-task" content="name=$_G['setting']['navs'][3]['navname'];action-uri={echo !empty($_G['setting']['domain']['app']['group']) ? 'http://'.$_G['setting']['domain']['app']['group'] : $_G[siteurl].'group.php'};icon-uri={$_G[siteurl]}{IMGDIR}/group.ico" /><!--{/if}-->
	<!--{if helper_access::check_module('feed')}--><meta name="msapplication-task" content="name=$_G['setting']['navs'][4]['navname'];action-uri={echo !empty($_G['setting']['domain']['app']['home']) ? 'http://'.$_G['setting']['domain']['app']['home'] : $_G[siteurl].'home.php'};icon-uri={$_G[siteurl]}{IMGDIR}/home.ico" /><!--{/if}-->
	<!--{if $_G['basescript'] == 'forum' && $_G['setting']['archiver']}-->
		<link rel="archives" title="$_G['setting']['bbname']" href="{$_G[siteurl]}archiver/" />
	<!--{/if}-->
	<!--{if !empty($rsshead)}-->$rsshead<!--{/if}-->
	<!--{if widthauto()}-->
		<link rel="stylesheet" id="css_widthauto" type="text/css" href='{$_G['setting']['csspath']}{STYLEID}_widthauto.css?{VERHASH}' />
		<script type="text/javascript">HTMLNODE.className += ' widthauto'</script>
	<!--{/if}-->
	<!--{if $_G['basescript'] == 'forum' || $_G['basescript'] == 'group'}-->
		<script type="text/javascript" src="{$_G[setting][jspath]}forum.js?{VERHASH}"></script>
	<!--{elseif $_G['basescript'] == 'home' || $_G['basescript'] == 'userapp'}-->
		<script type="text/javascript" src="{$_G[setting][jspath]}home.js?{VERHASH}"></script>
	<!--{elseif $_G['basescript'] == 'portal'}-->
		<script type="text/javascript" src="{$_G[setting][jspath]}portal.js?{VERHASH}"></script>
	<!--{/if}-->
	<!--{if $_G['basescript'] != 'portal' && $_GET['diy'] == 'yes' && check_diy_perm($topic)}-->
		<script type="text/javascript" src="{$_G[setting][jspath]}portal.js?{VERHASH}"></script>
	<!--{/if}-->
	<!--{if $_GET['diy'] == 'yes' && check_diy_perm($topic)}-->
		<link rel="stylesheet" type="text/css" id="diy_common" href="{$_G['setting']['csspath']}{STYLEID}_css_diy.css?{VERHASH}" />
	<!--{/if}-->

    <!--cbq-->
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="./easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="./easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="./easyui/themes/color.css">
    <link rel="stylesheet" type="text/css" href="./easyui/demo/demo.css">
    <script type="text/javascript" src="./easyui/jquery.min.js"></script>
    <script type="text/javascript" src="./easyui/jquery.easyui.min.js"></script>
	<!--/cbq-->

</head>













    



2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号