招标
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.
 
 
 
 
 

133 lines
2.7 KiB

<?php
/**
*
* FormText.php
* User: ChenLong
* DateTime: 2020/3/11 14:40
*/
namespace sdModule\makeAdminBasics\htmlUnit;
use sdModule\makeAdminBasics\Basics;
/**
* 下拉
* Class FormSelect
* @package sdModule\makeAdminBasics\htmlUnit
*/
class FormSelect implements FormUnitInterface
{
/**
* @var string
*/
private $label;
/**
* @var string
*/
private $name;
/**
* 默认皮肤
* @var string
*/
private $skin = 'lay-skin="primary"';
/**
* @var
*/
private $default;
/**
*
* FormText constructor.
* @param string $label
* @param string $name
*/
public function __construct(string $label, string $name)
{
$this->label = $label;
$this->name = $name;
}
/**
* 设置皮肤
* @param string $skin
* @return $this
*/
public function setSkin(string $skin = '')
{
$this->skin = $skin;
return $this;
}
/**
* 获取html
* @param array $data
* @return mixed|string
*/
public function getHtml($data = [])
{
return <<<HTML
<div class="layui-form-item">{$this->getLabel()}
<div class="layui-input-block">{$this->getForm($data)}</div>
</div>
HTML;
}
/**
* 获取label
* @return string
*/
private function getLabel()
{
return Basics::indentAndLineFeed(5, Basics::BEFORE) .
"<label class=\"layui-form-label\">{$this->label}</label>";
}
/**
* 获取表单html文本
* @param $data
* @return string
*/
private function getForm($data)
{
$html = Basics::indentAndLineFeed(6, Basics::BEFORE) ."<select name=\"{$this->name}\" lay-search>";
$dataOriginTitle = is_string($data) ?
explode(':', strtr($data, [':' => ':']))[0]
: $this->name . '_data';
$html .= Basics::indentAndLineFeed(7, Basics::BEFORE) .
implode(Basics::indentAndLineFeed(7, Basics::BEFORE), [
"<option ></option>",
"{foreach \${$dataOriginTitle} as \$value => \$title}",
$this->default
? "<option value=\"{\$value}\" {if \$value == '$this->default'}selected{/if} >{\$title}</option>"
: "<option value=\"{\$value}\">{\$title}</option>",
"{/foreach}"
]);
return $html . Basics::indentAndLineFeed(6, Basics::BEFORE) .
'</select>' . Basics::indentAndLineFeed(5, Basics::BEFORE);
}
/**
* 设置默认值
* @param $value
* @return mixed
*/
public function setDefault($value)
{
$value and $this->default = $value;
return $this;
}
}