完成通过注释生成table选择器组件;完善table选择器必选效果。

This commit is contained in:
2022-07-09 15:53:00 +08:00
parent 7a8c75de45
commit 69ac76a108
7 changed files with 101 additions and 21 deletions

View File

@@ -225,7 +225,7 @@ class BuildCurdService
* 表单类型 * 表单类型
* @var array * @var array
*/ */
protected $formTypeArray = ['text', 'image', 'images', 'file', 'files', 'select', 'switch', 'date', 'editor', 'textarea', 'checkbox', 'radio', 'relation']; protected $formTypeArray = ['text', 'image', 'images', 'file', 'files', 'select', 'switch', 'date', 'editor', 'textarea', 'checkbox', 'radio', 'relation', 'table'];
/** /**
* 初始化 * 初始化
@@ -285,19 +285,8 @@ class BuildCurdService
throw new TableException($e->getMessage()); throw new TableException($e->getMessage());
} }
// 初始化默认控制器名
$nodeArray = explode('_', $this->table); $this->controllerFilename = $this->getTableControllerName($this->table);
if (count($nodeArray) == 1) {
$this->controllerFilename = ucfirst($nodeArray[0]);
} else {
foreach ($nodeArray as $k => $v) {
if ($k == 0) {
$this->controllerFilename = "{$v}{$this->DS}";
} else {
$this->controllerFilename .= ucfirst($v);
}
}
}
// 初始化默认模型名 // 初始化默认模型名
$this->modelFilename = Str::studly($this->table); $this->modelFilename = Str::studly($this->table);
@@ -310,6 +299,26 @@ class BuildCurdService
return $this; return $this;
} }
public function getTableControllerName($table)
{
$controllerFilename = '';
// 初始化默认控制器名
$nodeArray = explode('_', $table);
if (count($nodeArray) == 1) {
$controllerFilename = ucfirst($nodeArray[0]);
} else {
foreach ($nodeArray as $k => $v) {
if ($k == 0) {
$controllerFilename = "{$v}{$this->DS}";
} else {
$controllerFilename .= ucfirst($v);
}
}
}
return $controllerFilename;
}
/** /**
* 设置关联表 * 设置关联表
* @param $relationTable * @param $relationTable
@@ -570,6 +579,8 @@ class BuildCurdService
return $this->fileList; return $this->fileList;
} }
/** /**
* 构建基础视图、JS、URL * 构建基础视图、JS、URL
* @return $this * @return $this
@@ -654,9 +665,9 @@ class BuildCurdService
preg_match('/\([\s\S]*?\)/i', $string, $defineMatch); preg_match('/\([\s\S]*?\)/i', $string, $defineMatch);
if (!empty($formTypeMatch) && isset($defineMatch[0])) { if (!empty($formTypeMatch) && isset($defineMatch[0])) {
$colum['comment'] = str_replace($defineMatch[0], '', $colum['comment']); $colum['comment'] = str_replace($defineMatch[0], '', $colum['comment']);
if (isset($colum['formType']) && in_array($colum['formType'], ['images', 'files', 'select', 'switch', 'radio', 'checkbox', 'date', 'relation'])) { if (isset($colum['formType']) && in_array($colum['formType'], ['images', 'files', 'select', 'switch', 'radio', 'checkbox', 'date', 'relation', 'table'])) {
$define = str_replace(')', '', str_replace('(', '', $defineMatch[0])); $define = str_replace(')', '', str_replace('(', '', $defineMatch[0]));
if (in_array($colum['formType'], ['select', 'switch', 'radio', 'checkbox', 'relation'])) { if (in_array($colum['formType'], ['select', 'switch', 'radio', 'checkbox', 'relation', 'table'])) {
$formatDefine = []; $formatDefine = [];
$explodeArray = explode(',', $define); $explodeArray = explode(',', $define);
foreach ($explodeArray as $vo) { foreach ($explodeArray as $vo) {
@@ -784,6 +795,45 @@ class BuildCurdService
); );
return $optionCode; return $optionCode;
} }
/**
* 构建表格选择器视图
* @param $field
* @param string $select
* @return mixed
*/
protected function buildTableView($field, $options, $value)
{
$default_define = [
'type' => 'checkbox',
'value_filed' => 'id',
'comment' => $options['comment'],
'field' => $field,
'required' => $options['required'],
'value' => $value,
];
$define = array_merge($default_define, $options['define']);
$table_controller_name = $this->getTableControllerName($define['table']);
$nodeArray = explode($this->DS, $table_controller_name);
$formatArray = [];
foreach ($nodeArray as $vo) {
$formatArray[] = Str::snake($vo);
}
$controller_url = implode('.', $formatArray);
$define['controller_url'] = $controller_url;
$table_main_code = $this->replaceTemplate(
$this->getTemplate("view{$this->DS}module{$this->DS}tableMain"),
$define
);
return $table_main_code;
}
/** /**
* 构建单选框视图 * 构建单选框视图
@@ -1194,7 +1244,7 @@ class BuildCurdService
if (!in_array($define, ['year', 'month', 'date', 'time', 'datetime'])) { if (!in_array($define, ['year', 'month', 'date', 'time', 'datetime'])) {
$define = 'datetime'; $define = 'datetime';
} }
} elseif ($val['formType'] == 'radio') { } elseif ($val['formType'] == 'radio' || $val['formType'] == 'switch') {
$templateFile = "view{$this->DS}module{$this->DS}radio"; $templateFile = "view{$this->DS}module{$this->DS}radio";
if (isset($val['define']) && !empty($val['define'])) { if (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildRadioView($field, '{in name="k" value="' . $val['default'] . '"}checked=""{/in}'); $define = $this->buildRadioView($field, '{in name="k" value="' . $val['default'] . '"}checked=""{/in}');
@@ -1221,6 +1271,9 @@ class BuildCurdService
} elseif (isset($val['define']) && !empty($val['define'])) { } elseif (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildOptionView($field); $define = $this->buildOptionView($field);
} }
} elseif ($val['formType'] == 'table') {
$templateFile = "view{$this->DS}module{$this->DS}table";
$define = $this->buildTableView($field, $val, $val['default']);
} }
@@ -1280,7 +1333,7 @@ class BuildCurdService
if (!in_array($define, ['year', 'month', 'date', 'time', 'datetime'])) { if (!in_array($define, ['year', 'month', 'date', 'time', 'datetime'])) {
$define = 'datetime'; $define = 'datetime';
} }
} elseif ($val['formType'] == 'radio') { } elseif ($val['formType'] == 'radio' || $val['formType'] == 'switch') {
$templateFile = "view{$this->DS}module{$this->DS}radio"; $templateFile = "view{$this->DS}module{$this->DS}radio";
if (isset($val['define']) && !empty($val['define'])) { if (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildRadioView($field, '{in name="k" value="$row.' . $field . '"}checked=""{/in}'); $define = $this->buildRadioView($field, '{in name="k" value="$row.' . $field . '"}checked=""{/in}');
@@ -1300,6 +1353,9 @@ class BuildCurdService
} elseif (in_array($field, ['remark']) || $val['formType'] == 'textarea') { } elseif (in_array($field, ['remark']) || $val['formType'] == 'textarea') {
$templateFile = "view{$this->DS}module{$this->DS}textarea"; $templateFile = "view{$this->DS}module{$this->DS}textarea";
$value = '{$row.' . $field . '|raw|default=\'\'}'; $value = '{$row.' . $field . '|raw|default=\'\'}';
} elseif ($val['formType'] == 'table') {
$templateFile = "view{$this->DS}module{$this->DS}table";
$define = $this->buildTableView($field, $val, $value);
} }
$editFormList .= $this->replaceTemplate( $editFormList .= $this->replaceTemplate(

View File

@@ -0,0 +1,6 @@
<div class="layui-form-item">
<label class="layui-form-label">{{comment}}</label>
<div class="layui-input-block">
{{define}}
</div>
</div>

View File

@@ -0,0 +1,3 @@
<div data-toggle="table-data" data-index="{:__url('{{controller_url}}/index')}" data-select-type="{{type}}" data-select-value="{{value}}" data-value-field="{{valueField}}" data-name="{{field}}" data-field-name="{{fieldName}}" data-required="{{required}}">
</div>

View File

@@ -12,3 +12,6 @@
.table-data-container .layui-input .layui-btn i { .table-data-container .layui-input .layui-btn i {
pointer-events: all; pointer-events: all;
} }
.table-data-container .layui-form-danger + .layui-input {
border-color: #ff5722 !important;
}

View File

@@ -1,5 +1,5 @@
<div class="table-data-container"> <div class="table-data-container" ref="tableData">
<input type="hidden" :name="setting.name" :value="value"> <input type="hidden" :name="setting.name" :value="value" :lay-verify="setting.required == 1?'required':''">
<div class="layui-input" @click="openSelectPage"> <div class="layui-input" @click="openSelectPage">
<div class="placeholder" v-if="listSelected.length == 0">{{setting.placeholder}}</div> <div class="placeholder" v-if="listSelected.length == 0">{{setting.placeholder}}</div>
<div class="layui-btn layui-btn-xs layui-btn-normal" v-for="(itemSelect,indexSelect) in listSelected"> <div class="layui-btn layui-btn-xs layui-btn-normal" v-for="(itemSelect,indexSelect) in listSelected">

View File

@@ -76,7 +76,9 @@ define(['jquery', 'vue'], function ($, Vue) {
}, },
created() { created() {
this.value = options.selectValue; this.value = options.selectValue;
if(typeof this.value != 'string') {
this.value = this.value.toString();
}
var valueLength = this.value.split(',').length; var valueLength = this.value.split(',').length;
loading.show(); loading.show();
$.get(options.index,{ $.get(options.index,{
@@ -90,6 +92,11 @@ define(['jquery', 'vue'], function ($, Vue) {
}) })
}, },
mounted() {
if(options.required == 1){
$(this.$refs['tableData']).closest('.layui-form-item').children('.layui-form-label').addClass('required');
}
},
template: tableDataTemplate, template: tableDataTemplate,
methods: { methods: {

View File

@@ -16,4 +16,9 @@
} }
} }
} }
.layui-form-danger+.layui-input {
border-color: #ff5722 !important;
}
} }