1.前台页面
layui
搜索ID:
2.后端页面。format_table可以常态使用
0,
'msg' => '',
'count' => $count,
'data' => $data,
);
//在框架当中不要如下使用
echo json_encode($response);
//框架中要如下使用。
//return ($response);//在框架里,因为对ajax请求,默认会做json处理返回前端的
}
//格式化数据给lay tree控件
function format_tree($items,$name='name',$primaryKey='id',$relation='parent_id',$diff=0){
//$items 二位数组;查库结果格式就可以
//$name 想在树控件上显示的字段名称
//$primaryKey 主键用来避免重复
//$relation 表示关联关系的字段名称比如 parent_id
//$diff 用来确定谁为父集
$arr = [];
foreach($items AS $k1 => $v1){
$tmp = $v1;
$tmp['name'] = $v1[$name];
if($v1[$relation] == $diff){
if(!isset($arr[$v1[$primaryKey]])){
$arr[$v1[$primaryKey]] = [];
}
$arr[$v1[$primaryKey]] = array_merge($tmp,$arr[$v1[$primaryKey]]);//保留键值的合并
}else{
if(!isset($arr[$v1[$relation]])){
$arr[$v1[$relation]] = [];
}
if(!isset($arr[$v1[$relation]]['children'])){
$arr[$v1[$relation]]['children'] = [];
}
$arr[$v1[$relation]]['children'][] = $tmp;
}
}
//键值从0开始json串开头[
return json_encode(array_values($arr) ,JSON_UNESCAPED_UNICODE);
}
// +----------------------------------------------------------------------
// | 1.测试程序使用的函数
// +----------------------------------------------------------------------
//判断是否为get
function is_get()
{
return isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD'])=='GET';
}
//判断是否为post
function is_post()
{
return isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD'])=='POST';
}
//判断是否为ajax
function is_ajax()
{
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH'])=='XMLHTTPREQUEST';
}
//table数据
function table(){
/**
* 1.对请求方式判断的好处是:强化函数的功能。当非ajax请求时渲染界面;ajax请求时专为table查询数据
* 2.下面data赋值,就是参照数据库查询结果的形式。所以库查询时,不用单独做格式处理了
*/
if(is_ajax()){
$page = 1;
$limit = 10;
$count = 30;
$data = array(
array(
"id"=> 10000,"username"=>"user-0","sex"=> "女","city"=> "城市-0","sign"=>"签名-0","experience"=>255,"logins"=>24,"wealth" =>82830700,"classify"=>"作家","score"=>57,
),
array(
"id"=>10001,"username"=>"user-1","sex"=>"男","city"=>"城市-1","sign"=>"签名-1","experience"=>884,"logins"=>58,"wealth"=>64928690,"classify"=>"词人","score"=>27
),
);
return format_table($data,$count);
}
//这里可以渲染模板扥等等操作
#######
}
function tree(){
$data = array(
array('id'=>1,'name'=>'河南','parent_id'=>0),
array('id'=>2,'name'=>'郑州','parent_id'=>1),
array('id'=>3,'name'=>'河北','parent_id'=>0),
array('id'=>4,'name'=>'石家庄','parent_id'=>3),
);
echo format_tree($data,'name','id','parent_id',0) ;
}
//处理请求的
$action = $_GET['action'];
isset($action) && call_user_func_array($action,$_GET);
?>