This commit is contained in:
Alessandro Ferro 2022-03-15 22:35:48 +01:00
parent b76a67562e
commit 2040d745c9
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,18 @@
<html>
<head>
<title>Dropdown example</title>
</head>
<body>
<?php
include "../phpxpress/dropdown.php";
$dropdown = new Dropdown;
$dropdown->setTitle("Title");
$dropdown->setDataSource(array("AA" => "#" , "BB" => "#"));
$dropdown->setSize("large");
$dropdown->draw();
?>
</body>
</html>

View File

@ -0,0 +1,64 @@
<script type="text/javascript" src="../bootstrap-5.1.3-dist/js/bootstrap.bundle.js"></script>
<?php
include "include.php";
class Dropdown{
private $title;
private $datasource;
private $color = 'secondary';
private $size;
// Associative array name => link
public function setDataSource(Array $datasource){
$this->datasource = $datasource;
}
public function setTitle($title){
$this->title = $title;
}
public function setColor($color){
$this->color = Code::bootstrapColors($color);
}
public function setSize($size){
if($size == "default")
$this->size = NULL;
else if($size == "large")
$this->size = "btn-lg";
else if($size == "small")
$this->size = "btn-sm";
else
throw new InvalidArgumentException('Parameter size must be either default, large or small.');
}
public function draw(){
$btnClass = 'btn btn-' . $this->color . ' dropdown-toggle';
if(isset($this->size)){
$btnClass .= ' ' . $this->size;
}
echo '<div class="dropdown">';
// Title
echo '<button class="' . $btnClass .'" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">';
echo $this->title;
echo '</button>';
echo '<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">';
foreach($this->datasource as $name => $link){
echo '<li><a class="dropdown-item" href="' . $link . '">' . $name . '</a></li>';
}
echo '</ul></div>';
}
}
?>