经常用bootstrap开发后台,搞了一个标签库
array('attr' => array('id', 'class', 'name', 'value', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),
'password' => array('attr' => array('id', 'class', 'name', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),
'number' => array('attr' => array('id', 'class', 'name', 'value', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),
'email' => array('attr' => array('id', 'class', 'name', 'value', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),
'url' => array('attr' => array('id', 'class', 'name', 'value', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),
'file' => array('attr' => array('id', 'class', 'name', 'label', 'help', 'inline', 'horizontal'), 'close' => 0),
'checkbox' => array('attr' => array('id', 'class', 'name', 'label', 'inline', 'disabled', 'checked', 'default'), 'close' => 0),
'radio' => array('attr' => array('id', 'class', 'name', 'label', 'inline', 'disabled', 'checked', 'default'), 'close' => 0),
/**
* key : value值字段名称,默认id
* text : 显示值字段名称,默认name
*/
'textarea' => array('attr' => array('id', 'class', 'name', 'label', 'value', 'label', 'rows', 'key', 'text', 'horizontal'), 'close' => 0),
'select' => array('attr' => array('id', 'class', 'name', 'label', 'data', 'value', 'key', 'horizontal'), 'close' => 0)
);
private $ids = array();//自动生产id保存,防止重复
/**
* @param $id string input的id
* @param $name string input的name值
* @return string
*/
protected function setid($id, $name)
{
return empty($id) ? 'input' . ucfirst($name) . mt_rand(1, 9999) : $id;
}
/**
* 根据表单的状态返回图标和状态
* @param $status 表单当前的状态
* @return array()
*/
protected function inputstatus($attr)
{
switch ($attr['status']) {
case 'success':
$icon = '';
$class = 'has-success';
break;
case 'error':
$icon = '';
$class = 'has-error';
break;
case 'warning':
$icon = '';
$class = 'has-warning';
break;
default:
$icon = '';
$class = '';
break;
}
$icon = empty($icon) ? $icon : $icon . '(' . $attr['status'] . ')';
return array('statusclass' => $class, 'icon' => $icon);
}
protected function wrapdiv($attr)
{
$class = array();
if (!empty($attr['inline'])) array_push($class, 'form-inline');
if (!empty($attr['statusclass'])) array_push($class, $attr['statusclass']);
if (!empty($attr['has_feedback'])) array_push($class, $attr['has_feedback']);
if (!empty($attr['class'])) array_push($class, $attr['class']);
if (empty($attr['typeclass'])) {
//输入类型
array_push($class, 'form-group');
} else {
//选择类型 checkbox radio
array_push($class, $attr['typeclass']);
}
return '';
}
protected function wraplabel($attr)
{
$id = $attr['id'];
$width = '';
if (array_key_exists('horizontal', $attr) && !empty($attr['horizontal'])) {
$width = 'col-xs-' . $attr['horizontal'][0];
}
$typeclass = empty($attr['typeclass']) ? 'control-label' : $attr['typeclass'];
return '';
}
protected function input($attr)
{
$attrs = array();
if (!empty($attr['placeholder'])) array_push($attrs, $attr['placeholder']);//输入框描述
if (!empty($attr['describedby'])) array_push($attrs, $attr['describedby']);//输入框与图标关联,
if (empty($attr['typeclass'])) {
//没有这个类型为输入框类型 text/password/email....
array_push($attrs, 'class=form-control');//calss 属性
if (!empty($attr['value'])) array_push($attrs, 'value=' . $attr['value']);//默认值
} else {
//选择类型 checkbox/radio
if (!empty($attr['default'])) {
$default = $this->_trim($attr['default']);
$default = explode('.', $default);
$default = $default[0] . [' . $default[1] . '];
$checked = '';
} else {
$checked = empty($attr['checked']) ? '' : 'checked';
}
if (!empty($attr['disabled'])) array_push($attrs, 'disabled');//是否禁用
array_push($attrs, $checked);//默认值判断
}
$input = ;
return $input . $attr['icon'] . $attr['help'];
}
/**
* 输入框默认表单赋值
* @param $attr
*/
protected function set_input_attr($attr)
{
$attr['id'] = $this->setid($attr['id'], $attr['name']);
$status = $this->inputstatus($attr);
$attr['statusclass'] = $status['statusclass'];//wrapdiv上面显示
$attr['icon'] = $status['icon'];//wraplabel上面显示
$attr['has_feedback'] = empty($status['icon']) ? '' : 'has-feedback';//wrapdiv上面显示
$attr['describedby'] = empty($status['icon']) ? '' : 'aria-describedby=' . $attr['id'] . 'status';//表单上关联图标
$attr['help'] = empty($attr['help']) ? '' : '' . $attr['help'] . '';
$attr['placeholder'] = empty($attr['placeholder']) ? '' : 'placeholder=' . $attr['placeholder'] . '';
if (array_key_exists('horizontal', $attr) && !empty($attr['horizontal'])) {
$attr['horizontal'] = explode(',', $attr['horizontal']);
}
return $attr;
}
public function _text($attr)
{
$attr['type'] = 'text';
return $this->inputclass($attr);
}
public function _password($attr)
{
$attr['type'] = 'password';
return $this->inputclass($attr);
}
public function _email($attr)
{
$attr['type'] = 'email';
return $this->inputclass($attr);
}
public function _number($attr)
{
$attr['type'] = 'number';
return $this->inputclass($attr);
}
public function _url($attr)
{
$attr['type'] = 'url';
return $this->inputclass($attr);
}
public function _file($attr)
{
$attr['type'] = 'file';
return $this->inputclass($attr);
}
public function _checkbox($attr)
{
$attr['typeclass'] = empty($attr['inline']) ? 'checkbox' : 'checkbox-inline';
$attr['type'] = 'checkbox';
return $this->checkclass($attr);
}
public function _radio($attr)
{
$attr['typeclass'] = empty($attr['inline']) ? 'radio' : 'radio-inline';
$attr['type'] = 'radio';
return $this->checkclass($attr);
}
public function _textarea($attr)
{
$id = $this->setid($attr['id'], $attr['name']);
$textarea = '' . $attr['value'] . '';
return $this->_return($attr, $textarea);
}
//选择类型表单
protected function checkclass($attr)
{
$attr = $this->set_input_attr($attr);
$input = $this->input($attr);
$div = $this->wrapdiv($attr);
$label = $this->wraplabel($attr);
return $div . $label . $input . $attr['label'] . '
';
}
//输出输入类型表单
protected function inputclass($attr)
{
$input = $this->input($attr);
return $this->_return($attr, $input);
}
//去除左右花括号
private function _trim($field)
{
$field = ltrim($field, '{');
return rtrim($field, '}');
}
public function _select($attr)
{
$data = $this->_trim($attr['data']);
$key = empty($attr['key']) ? 'id' : $attr['key'];
$text = empty($attr['text']) ? 'name' : $attr['text'];
$value = $attr['value'];
$id = $this->setid($attr['id'], $attr['name']);
if (strpos($value, '{') !== false) {
$value = $this->_trim($value);
}
$select = '';
$select .= '';
$select .= ' >{$v.' . $text . '}';
$select .= '';
$select .= '';
return $this->_return($attr, $select);
}
public function _return($attr, $form)
{
//判断是不是水平排列的表单
$horizontal = false;
if (array_key_exists('horizontal', $attr) && !empty($attr['horizontal'])) {
$horizontal = true;;
}
$attr = $this->set_input_attr($attr);
$div = $this->wrapdiv($attr);
$label = $this->wraplabel($attr);
if ($horizontal) {
return $div . $label . $attr['label'] . '' . $form . '
';
} else {
return $div . $label . $attr['label'] . '' . $form . '';
}
}
}调用
0,'name'=>'北京'),
array('id'=>1,'name'=>'上海'),
array('id'=>2,'name'=>'天津'),
array('id'=>3,'name'=>'深圳')
);
$member = array(
'gender'=>2,
'email'=>'314231604@qq.com',
'sleep'=>1,
'eat'=>1,
'username'=>'小二郎',
'url'=>'http://www.baidu.com'
) ;
?>
爱好
性别
运行效果
bootstrap标签库.rar ( 3.94 kb 下载:52 次 )
ad:真正免费,域名+虚机+企业邮箱=0元