phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlSelect.php
Go to the documentation of this file.
1 <?php
2 namespace Ajax\common\html\html5;
3 
5 use Ajax\JsUtils;
14  private $default;
15  private $options;
16 
17  public function __construct($identifier, $caption="",$default=NULL) {
18  parent::__construct($identifier, "select");
19  $this->setProperty("name", $identifier);
20  $this->setProperty("class", "form-control");
21  $this->setProperty("value", "");
22  $this->default=$default;
23  $this->options=array();
24  }
25 
26  public function addOption($element,$value="",$selected=false){
27  if($element instanceof HtmlOption){
28  $option=$element;
29  }else{
30  $option=new HtmlOption($this->identifier."-".count($this->options), $element,$value);
31  }
32  if($selected || $option->getValue()==$this->getProperty("value")){
33  $option->select();
34  }
35  $this->options[]=$option;
36  }
37 
38  public function setValue($value) {
39  foreach ( $this->options as $option ) {
40  if (strcasecmp($option->getValue(),$value)===0) {
41  $option->setProperty("selected", "");
42  }
43  }
44  $this->setProperty("value", $value);
45  }
46 
47  /*
48  * (non-PHPdoc)
49  * @see \Ajax\bootstrap\html\base\BaseHtml::compile()
50  */
51  public function compile(JsUtils $js=NULL, &$view=NULL) {
52  $this->content=array();
53  if(isset($this->default)){
54  $default=new HtmlOption("", $this->default);
55  $this->content[]=$default;
56  }
57  foreach ($this->options as $option){
58  $this->content[]=$option;
59  }
60  return parent::compile($js, $view);
61  }
62 
63  public function fromArray($array){
64  if(JArray::isAssociative($array)){
65  foreach ($array as $k=>$v){
66  $this->addOption($v, $k);
67  }
68  }else{
69  foreach ($array as $v){
70  $this->addOption($v, $v);
71  }
72  }
73  return $this;
74  }
75 
76  /*
77  * (non-PHPdoc)
78  * @see \Ajax\bootstrap\html\base\HtmlSingleElement::run()
79  */
80  public function run(JsUtils $js) {
81  parent::run($js);
82  $this->_bsComponent=$js->bootstrap()->generic("#".$this->identifier);
83  $this->addEventsOnRun($js);
84  return $this->_bsComponent;
85  }
86 
87  public function onChange($jsCode) {
88  return $this->on("change", $jsCode);
89  }
90 
91  public function onKeypress($jsCode) {
92  return $this->on("keypress", $jsCode);
93  }
94 
95  /* (non-PHPdoc)
96  * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
97  */
98  public function fromDatabaseObject($object, $function) {
99  $this->addOption($function($object));
100  }
101 
102  public function setSize($size){
103  return $this->setProperty("size", $size);
104  }
105 }
addOption($element, $value="", $selected=false)
Definition: HtmlSelect.php:26
on($event, $jsCode, $stopPropagation=false, $preventDefault=false)
__construct($identifier, $caption="", $default=NULL)
Definition: HtmlSelect.php:17
addEventsOnRun(JsUtils $js=NULL)
bootstrap(Bootstrap $bootstrap=NULL)
getter or setter of the Twitter Bootstrap variable
Definition: JsUtils.php:141
static isAssociative($array)
Definition: JArray.php:6
compile(JsUtils $js=NULL, &$view=NULL)
Definition: HtmlSelect.php:51
JQuery PHP library.
Definition: JsUtils.php:23
fromDatabaseObject($object, $function)
Definition: HtmlSelect.php:98