招标
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

208 lines
5.4 KiB

<?php
/**
*
* ListJs.php
* User: ChenLong
* DateTime: 2020/3/18 16:02
*/
namespace sdModule\makeAdminBasics\htmlUnit;
use sdModule\common\Sc;
use sdModule\makeAdminBasics\Basics;
use sdModule\makeAdminBasics\ConfigHelper;
use sdModule\makeAdminBasics\Make;
class ListJs
{
/**
* @var Make
*/
private $make;
/**
* ListJs constructor.
* @param Make $make
*/
public function __construct(Make $make)
{
$this->make = $make;
}
/**
* @return string
* @throws \ReflectionException
*/
public function load()
{
JsFacade::registerLayUIModule('table');
JsFacade::normalJs(Basics::indentAndLineFeed(1, Basics::BEFORE) .
"let primary = \"{\$primary ?: 'id'}\";");
Sc::reflex()->getInstance(ListCreate::class)->register(Js::LIST_HEAD_TEMPLATE); // 新增
Sc::reflex()->getInstance(ListUpdate::class)->register(Js::LIST_LINE_TEMPLATE); // 修改
Sc::reflex()->getInstance(ListDelete::class)->register(); // 删除
Sc::reflex()->getInstance(ListQuickSearch::class)->register(); // 快捷搜索
return $this->tableRender();
}
/**
*
* @param string $head
* @param string $line
* @return string
*/
public function tableRender($head = Js::LIST_HEAD_TEMPLATE, $line = Js::LIST_LINE_TEMPLATE)
{
$tableDataUrl = ConfigHelper::get('list_url');
$showData = in_array('image', array_column($this->make->makeData, 'show_type'));
$size = $showData ? Basics::indentAndLineFeed(3, Basics::BEFORE) . ',size:"lg"' : '';
return <<<JSA
table.render({
elem: '#test'
,url: "{$tableDataUrl}"
,toolbar: '#{$head}'{$size}
,cellMinWidth: 80
,page:true
,autoSort: false
, title: '{\$page_name ?: lang("List data")}'
,limits:[10,20,30,40,50,100,200,1000]
,cols: [[
{type:'checkbox'}{$this->fieldHandle()}
,{width:150, title: '{:lang("operating")}',templet:'#{$line}'}
]],
done:function (res) {
custom.enlarge(layer, $, '.layer-photos-demo');
window.table = table;
}
});
{$this->templateHandle()}
{$this->search()}
JSA;
}
/**
* @return string
*/
private function fieldHandle()
{
$js = '';
foreach ($this->make->makeData as $field => $makeDatum) {
if ($makeDatum['show_type'] == 'text') {
if ($makeDatum['join'] && is_string($makeDatum['join'])) {
$table = explode(':', $makeDatum['join'])[0] ?: 'parent';
$field_arr = explode('=', $makeDatum['join']);
$field = $table . '_' . end($field_arr);
}
$js .= Basics::indentAndLineFeed(4, Basics::BEFORE) .
",{field:'{$field}', title: '{$makeDatum['label']}'}";
} elseif ($makeDatum['show_type'] == 'image') {
$js .= Basics::indentAndLineFeed(4, Basics::BEFORE) .
",{field:'{$field}', title: '{$makeDatum['label']}',templet:function (obj) {
return custom.tableImageShow(obj.{$field});
}}";
}
}
return $js;
}
/**
* @return string
*/
private function templateHandle()
{
$html = '';
$js = '';
foreach (Js::getInstance()->template as $template => $data) {
$html .= "
<!-- {$template} 模板-->
<script id='{$template}' type='text/html'>";
$templateJS = '';
foreach ($data as $e => $event) {
$html .= $event['template'];
$templateJS .= $templateJS ? "else " : Basics::indentAndLineFeed(3, Basics::BEFORE);
$templateJS .= "if(obj.event === '{$e}'){{$event['js']}
}";
}
$html .= Basics::indentAndLineFeed(0, Basics::BEFORE) . '</script>' . Basics::indentAndLineFeed(0, Basics::BEFORE);
$tool = $template == Js::LIST_HEAD_TEMPLATE ? "toolbar" : "tool";
$js .= Basics::indentAndLineFeed(2, Basics::BEFORE) .
"table.on('{$tool}(test)', function (obj) {{$templateJS} " . Basics::indentAndLineFeed(2, Basics::BEFORE) . "});";
}
JsFacade::templateHtml($html);
return $js;
}
/**
* @return string
*/
private function search()
{
return <<<JS
document.onkeyup = (e) => {
if (e.key === 'Enter') {
let search = $('#quick-search').val();
table.reload('test', {
where:{
quick_search:search
}
,page:{
curr:1
}
});
$('#quick-search').val(search).focus();
}
};
form.on('submit(search)', function (object) {
table.reload('test', {
where:{
search:object.field
},
page:{
curr:1
}
});
return false;
});
JS;
}
public function sort()
{
return <<<SJS
table.reload('idTest', {
initSort: obj
,where: {
sort: obj.field + ',' + obj.type
}
});
SJS;
}
}