ThinkPHP5开发网站后台,列表页构建方法 - listBuilder
/** * Created by PhpStorm. * * @auther:wusn<message@jswusn.com> * Date: 2018/11/3 * Time: 22:36 */ namespace appcommonlibldBuilder; class listBuilder extends ldBuilder { public $base_template = ''; protected $list_key = []; /*标题*/ protected $list_data = []; //列表数据 protected $vars = []; //模板变量 protected $check_name = ['key'=>'id','value'=>'id']; //自定义全选key & value public function _initialize() { $this->element_path = APP_PATH . '/common/lib/ldBuilder/view/list'; $this->base_template = APP_PATH . '/admin/view/common/list_base.html'; } /** * 表头 * @param $name 字段 * @param $title 标题 * @param string $type 类型 * @param string $param 参数 * @auther:wusn<message@jswusn.com> * @return $this */ public function setTableColumn($name,$title,$type='',$param=''){ $width = ''; $th_class = ''; $td_class = ''; if(is_array($title)){ !empty($title[1]) ? $width = $title[1] : ''; !empty($title[2]) ? $width = $title[2] : ''; !empty($title[3]) ? $width = $title[3] : ''; $title = $title[0]; } $this->list_key[] = ['name' => $name , 'title' => $title , 'type' => $type , 'param' => $param , 'width' => $width , 'th_class' => $th_class , 'td_class' => $td_class]; return $this; } /** * 设置列表上方详情 * @param $title 标题 * @param string $info 详情 * @param string $color 样式 * @auther:wusn<message@jswusn.com> * @return $this */ public function setTopInfo($title,$info='',$color='primary'){ $vars = [ 'title' => $title, 'info' => $info, 'color' => $color, ]; $this->vars['top_info'] = parent::fetch($this->element_path . '/panel.html' , $vars); return $this; } /** * 设置th class * @param $name 名称 * @param $class 样式名 * @auther:wusn<message@jswusn.com> * @return $this */ public function setTableThClass($name,$class){ foreach ($this->list_key as $value){ if($value['name'] == $name){ $value['th_class'] = $class; } } return $this; } /** * 设置 td class * @param $name * @param $class * @auther:wusn<message@jswusn.com> * @return $this */ public function setTableTdClass($name,$class){ foreach ($this->list_key as $value){ if($value['name'] == $name){ $value['td_class'] = $class; } } return $this; } /** * 添加右侧按钮 * @param string $type 按钮类型 * @param array $option 参数 * @auther:wusn<message@jswusn.com> * @return $this */ public function setButton($type,$option=[]){ $this->vars['buttons'][] = ['type' => $type , 'option' => $option]; return $this; } /** * 添加表头按钮 * @param string $type 按钮类型 * @param array $option 参数 * @auther:wusn<message@jswusn.com> * @return $this */ public function setTopButton($type='button',$option=[]){ $element = $this->element_path . '/button.html'; //组件模板 switch ($type){ case 'back': $_option = ['btn_color' => 'btn-warning' , 'icon' => 'fa fa-reply' , 'title' => '返回' , 'url' => url('index')]; break; case 'add': $_option = ['btn_color' => 'btn-success' , 'icon' => 'fa fa-plus-square' , 'title' => '添加' , 'url' => url('add')]; break; case 'delete': $_option = ['btn_color' => 'btn-danger' , 'icon' => 'fa fa-times-circle' , 'class' => 'confirm-all' , 'title' => '删除' , 'url' => url('delete')]; break; case 'refresh': $_option = ['btn_color' => 'btn-primary' , 'icon' => 'fa fa-refresh' , 'class' => 'refresh' , 'title' => '更新排序' , 'url' => url('refresh')]; break; case 'select': $element = $this->element_path . '/select.html'; //组件模板 $_option = ['btn_color' => 'btn-primary' , 'icon' => 'fa fa-sitemap' , 'title' => '下拉按钮' , 'url_arr' => []]; break; case 'info': $_option = ['btn_color' => 'btn-info' , 'icon' => 'fa fa-file-text-o' , 'title' => '详细' , 'url_arr' => 'javascript:;']; break; default: $_option = ['btn_color' => 'btn-default' , 'icon' => 'fa fa-times-circle' , 'title' => '默认按钮' , 'url_arr' => 'javascript:;']; } $option = array_merge($_option,$option); $this->vars['top_buttons'][] = parent::fetch($element,$option); return $this; } /** * 搜索框 * @param $key 搜索字段 * @param string $value 搜索值 * @auther:wusn<message@jswusn.com> * @return $this */ public function setSearch($key,$value=''){ $options['search']['key'] = $key; $options['search'][$value] = $value; $this->vars['search'] = parent::fetch($this->element_path . '/ssearch.html' , $options); //组件模板 return $this; } /** * 设置全选的健与值 * @param $name 健 & 值 * @auther:wusn<message@jswusn.com> * @return $this */ public function setCheckName($name){ $this->check_name = ['key' => $name , 'value' => $name]; return $this; } /** * 正则替换url里面的参数 * @param $array * @param $url * @param string $regexp * @auther:wusn<message@jswusn.com> * @return mixed */ public function replace_url($array,$url,$regexp='/__row.(w+)__/i'){ //匹配所有占位参数 preg_match_all($regexp,$url,$param,PREG_SET_ORDER); foreach ($param as $value){ empty($array[$value[1]]) ? $param_url = '' : $param_url = $array[$value[1]]; $url = str_replace($value[0],$param_url,$url); //替换参数 } return $url; } /** * 右侧操作按钮 * @param array $row 行数据 * @auther:wusn<message@jswusn.com> * @return mixed|string */ private function actionBtn($row=[]){ $btns = ''; $buttons = $row['action_buttons']; $element = $this->element_path . '/button.html'; // 组件模板 if(!empty($buttons)){ foreach ($buttons as &$vo){ if(!empty($vo['option']['url'])){ $vo['option']['url'] = $this->replace_url($row,$vo['option']['url']); } switch ($vo['type']){ case 'add': $option = ['btn_color' => 'btn-success' , 'icon' => 'fa fa-plus-square' , 'title' => '添加' , 'url' => url('add',array('id'=>$row['id']))]; break; case 'edit': $option = ['btn_color' => 'btn-warning' , 'icon' => 'fa fa-pencil-square' , 'title' => '编辑' , 'url' => url('edit',array('id'=>$row['id']))]; break; case 'delete': $option = ['btn_color' => 'btn-danger' , 'icon' => 'fa fa-times-circle' , 'title' => '删除' , 'class' => 'confirm-one' , 'url' => url('delete',array('id'=>$row['id']))]; break; case 'info': $option = ['btn_color' => 'btn-info' , 'icon' => 'fa fa-file-text-o' , 'title' => '详细' , 'url' => url('info',array('id'=>$row['id']))]; break; default: $option = ['btn_color' => 'btn-default' , 'icon' => 'fa fa-times-circle' , 'title' => '默认按钮' , 'url' => url('',array('id'=>$row['id']))]; } $vo['option'] = array_merge($option , $vo['option']); $btns .= parent::fetch($element , $vo['option']); } } return $btns; } /** * 列表数据 * @param $list_data * @auther:wusn<message@jswusn.com> * @return $this */ public function setTableData($list_data){ $this->list_data = $list_data; return $this; } /** * 设置单元格值 * @param $key_item 表格列标题 * @param $data_item 表格数据 * @auther:wusn<message@jswusn.com> * @return false|mixed|string */ protected function setItemValue($key_item,$data_item){ if (strpos($key_item['name'],'.')){ $vars = explode('.',$key_item['name']); $first = $vars[0]; unset($vars[0]); if(is_array($data_item[$first])){ $value = $data_item[$first]; foreach ($vars as $key=>$val){ //遍历多维数组,取出多维数组的值 $value = $value[$val]; } }else{ $value = ''; } }else{ $value = &$data_item[$key_item['name']]; //当前单元格的值 } $param = $key_item['param']; //传入的参数 switch ($key_item['type']){ case 'callback': case 'function': //函数 if (is_object($param)){ //匿名函数 $value = call_user_func_array($param,[&$value,&$data_item]); }else{ //解析模板函数 $var_str = $value . '|' .$param; $var_str = $this->parseFunction($var_str); $value = $var_str; } break; case 'time': //生成时间格式 $param = empty($param) ? 'Y-m-d' : $param; $value = empty($value) ? date($param) : date($param,$value); break; case 'status': //状态 switch ($value){ case -1: $value = '<span class="badge badge-danger">'.$param[-1].'</span>'; break; case 0: $value = '<span class="badge badge-default">'.$param[0].'</span>'; break; case 1: $value = '<span class="badge badge-success">'.$param[1].'</span>'; break; case 2: $value = '<span class="badge badge-warning">'.$param[2].'</span>'; break; default: $value = '<span class="badge badge-default">'.$param[1].'</span>'; } return $value; break; case 'switch': //开关状态 $url = url('status',['id'=>$data_item['id']]); if(is_array($param)){ $url = $this->replace_url($data_item,$param['url']); } if($value == 1){ $value = '<input type="checkbox" class="js-switch" data-href="'.$url.'" checked="checked" />'; }else{ $value = '<input type="checkbox" class="js-switch" data-href="'.$url.'" />'; } break; case 'input': $value = '<input type="text" name="'.$key_item['name'].'['.$data_item['id'].']" class="form-control sort" value="'.$data_item[$key_item['name']].'" />'; break; case 'image': $value = $data_item[$key_item['name']]; if(!empty($value)){ if(is_array($value)){ $value = $value[0]; } $value = '<span class="list_images">< img src="'.$value.'" class="hide_images" alt="" /></span>'; } break; case 'url': $value = $data_item[$key_item['name']]; $url = $this->replace_url($data_item,$param); $value = '<a href="'.$url.'" class="url" >'.$value.'</a>'; break; default: } if ($key_item['name'] == 'action_buttons'){ //生成按钮 $data_item['action_buttons'] = $value; $value = $this->actionBtn($data_item); } return $value; } /** * 解析并执行函数 * @param $varStr * @auther:wusn<message@jswusn.com> * @return mixed|void */ private function parseFunction($varStr){ $varArray = explode('|',$varStr); //取得变量名称 $name = array_shift($varArray); //对变量使用函数 $length = count($varArray); for ($i = 0; $i < $length; $i++){ $args = explode('=',$varArray[$i] , 2); //模板函数过滤 $fun = trim($args[0]); switch ($fun){ case 'default': //特殊模板函数 if(strpos($name,'(') === false){ $name = ( isset($name) && ($name !== '') ) ? $name : $args[1]; }else{ $name = $name ? : $args[1]; } break; default: //通用模板函数 if(empty($name)){ return; } if (isset($args[1])){ if (strstr($args[1],'###')){ $args[1] = str_replace('###',$name,$args[1]); $args_arr = explode(',',$args[1]); //将参数字符串转为数组 }else{ $args_arr = explode(',',$args[1]); //将参数字符串转为数组 array_unshift($args_arr,$name); //将数值插入第一个元素 } $name = call_user_func_array($fun,$args_arr); //回调执行函数 }else{ if(!empty($args[0])){ $name = call_user_func_array($fun,[$name]); //回调执行函数 } } } } return $name; } /** * 设置分页列表 * @param $pages * @auther:wusn<message@jswusn.com> * @return $this */ public function setPages($pages){ $this->vars['pages'] = $pages; return $this; } /** * 生成最终html页面 * @auther:wusn<message@jswusn.com> * @return array */ public function outHtml(){ $str = ''; $check_key = 'ids'; $check_value = 'id'; $data = $this->list_data; $check_name = $this->check_name; //check key & value 自定义 if(!empty($check_name)){ $this->vars['check_name'] = $this->check_name; $check_key = $check_name['key']; $check_value = $check_name['value']; } if(!empty($data)){ foreach ($data as $data_item){ !empty($this->vars['buttons']) && $data_item['action_buttons'] = $this->vars['buttons']; $str = '<tr> <td><input type="checkbox" class="ckeck-item" name="'.$check_key.'[]" value="'.$data_item[$check_value].'" ></td>'; foreach ($this->list_key as $key_item){ $str .= '<td class="'.$key_item['td_class'].'">' . $this->setItemValue($key_item,$data_item) . '</td>'; } $str .= '</tr>'; } } $this->vars['table'] = $str; return $this->vars; } /** * 解析模板 * @param string $template * @param array $vars * @param array $replace * @param array $config * @auther:wusn<message@jswusn.com> * @return mixed */ public function fetch($template = '', $vars = [], $replace = [], $config = []) { $this->vars['list_key'] = $this->list_key; $this->vars['list_data'] = $this->list_data; if(is_array($vars) && key($vars) !== 0){ //判断是否为数组 $vars = array_merge($this->outHtml(),$vars); } return parent::fetch($template, $vars, $replace, $config); } }
以上代码由(Ldcms)项目团队构思,苏州网站建设整理!