1
|
|
|
<?php |
2
|
|
|
namespace Ajax\bootstrap\html\html5; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* HTML Select |
6
|
|
|
* @author jc |
7
|
|
|
* @version 1.002 |
8
|
|
|
*/ |
9
|
|
|
use Ajax\bootstrap\html\base\HtmlDoubleElement; |
10
|
|
|
use Ajax\JsUtils; |
11
|
|
|
use Phalcon\Mvc\View; |
12
|
|
|
use Ajax\service\JArray; |
13
|
|
|
use Ajax\bootstrap\html\html5\HtmlOption; |
14
|
|
|
|
15
|
|
|
class HtmlSelect extends HtmlDoubleElement { |
16
|
|
|
private $default; |
17
|
|
|
private $options; |
18
|
|
|
|
19
|
|
|
public function __construct($identifier, $caption,$default=NULL) { |
20
|
|
|
parent::__construct($identifier, "select"); |
21
|
|
|
$this->setProperty("name", $identifier); |
22
|
|
|
$this->setProperty("class", "form-control"); |
23
|
|
|
$this->setProperty("value", ""); |
24
|
|
|
$this->default=$default; |
25
|
|
|
$this->options=array(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function addOption($element,$value="",$selected=false){ |
29
|
|
|
if($element instanceof HtmlOption){ |
30
|
|
|
$option=$element; |
31
|
|
|
}else{ |
32
|
|
|
$option=new HtmlOption($this->identifier."-".count($this->options), $element,$value); |
33
|
|
|
} |
34
|
|
|
if($selected || $option->getValue()==$this->getProperty("value")){ |
35
|
|
|
$option->select(); |
36
|
|
|
} |
37
|
|
|
$this->options[]=$option; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setValue($value) { |
41
|
|
|
foreach ( $this->options as $option ) { |
42
|
|
|
if (strcasecmp($option->getValue(),$value)===0) { |
43
|
|
|
$option->setProperty("selected", ""); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
$this->setProperty("value", $value); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* |
50
|
|
|
* (non-PHPdoc) |
51
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::compile() |
52
|
|
|
*/ |
53
|
|
|
public function compile(JsUtils $js=NULL, View $view=NULL) { |
54
|
|
|
$this->content=array(); |
55
|
|
|
if(isset($this->default)){ |
56
|
|
|
$default=new HtmlOption("", $this->default); |
57
|
|
|
$this->content[]=$default; |
58
|
|
|
} |
59
|
|
|
foreach ($this->options as $option){ |
60
|
|
|
$this->content[]=$option; |
61
|
|
|
} |
62
|
|
|
return parent::compile($js, $view); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function fromArray($array){ |
66
|
|
|
if(JArray::isAssociative($array)){ |
67
|
|
|
foreach ($array as $k=>$v){ |
68
|
|
|
$this->addOption($v, $k); |
69
|
|
|
} |
70
|
|
|
}else{ |
71
|
|
|
foreach ($array as $v){ |
72
|
|
|
$this->addOption($v, $v); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/* |
79
|
|
|
* (non-PHPdoc) |
80
|
|
|
* @see \Ajax\bootstrap\html\base\HtmlSingleElement::run() |
81
|
|
|
*/ |
82
|
|
|
public function run(JsUtils $js) { |
83
|
|
|
parent::run($js); |
84
|
|
|
$this->_bsComponent=$js->bootstrap()->generic("#".$this->identifier); |
85
|
|
|
$this->addEventsOnRun($js); |
86
|
|
|
return $this->_bsComponent; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function onChange($jsCode) { |
90
|
|
|
return $this->on("change", $jsCode); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function onKeypress($jsCode) { |
94
|
|
|
return $this->on("keypress", $jsCode); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* (non-PHPdoc) |
98
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
99
|
|
|
*/ |
100
|
|
|
public function fromDatabaseObject($object, $function) { |
101
|
|
|
$this->addOption($function($object)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setSize($size){ |
105
|
|
|
return $this->setProperty("size", $size); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|