flsouto / htcklist
此软件包最新版本(1.0.0)没有提供许可信息。
生成多选框列表。
1.0.0
2017-03-20 00:23 UTC
Requires
- flsouto/htchoice: ^1.0
This package is not auto-updated.
Last update: 2024-09-18 20:13:13 UTC
README
此库可用于生成多选字段。建议您查看其父类的文档,以便了解所有继承的功能。
安装
通过composer
composer require flsouto/htcklist
用法
在以下示例中,我们生成一个列表,允许用户选择一周中的多天。
<?php use FlSouto\HtCklist; require("vendor/autoload.php"); $select = new HtCklist("select_days"); $select->options([1=>'Mon',2=>'Tue',3=>'Wed',4=>'Thu',5=>'Fri',6=>'Sat']); echo $select;
输出
<div class="widget 58cf20cba6cf5" style="display:block"> <label> <input type="checkbox" name="select_days[]" value="1" />Mon</label> <br /> <label> <input type="checkbox" name="select_days[]" value="2" />Tue</label> <br /> <label> <input type="checkbox" name="select_days[]" value="3" />Wed</label> <br /> <label> <input type="checkbox" name="select_days[]" value="4" />Thu</label> <br /> <label> <input type="checkbox" name="select_days[]" value="5" />Fri</label> <br /> <label> <input type="checkbox" name="select_days[]" value="6" />Sat</label> <br /> <input type="hidden" name="select_days_submit" value="1" /> <div style="color:yellow;background:red" class="error"> </div> </div>
注意: options
方法还接受除关联数组以外的其他格式。请查看HtChoice 类的文档以获取更多信息。
更改分隔符
默认情况下,用于分隔选项的分隔符是 <br/>
元素,即换行符。但您可以使用 separator
方法来更改它。在下面的示例中,我们将分隔符更改为两个空格,以便选项水平显示。
<?php use FlSouto\HtCklist; require("vendor/autoload.php"); $select = new HtCklist("select_days"); $select->options([1=>'Mon',2=>'Tue',3=>'Wed',4=>'Thu',5=>'Fri',6=>'Sat']) ->separator(" "); echo $select;
输出
<div class="widget 58cf20cba8e2c" style="display:block"> <label> <input type="checkbox" name="select_days[]" value="1" />Mon</label> <label> <input type="checkbox" name="select_days[]" value="2" />Tue</label> <label> <input type="checkbox" name="select_days[]" value="3" />Wed</label> <label> <input type="checkbox" name="select_days[]" value="4" />Thu</label> <label> <input type="checkbox" name="select_days[]" value="5" />Fri</label> <label> <input type="checkbox" name="select_days[]" value="6" />Sat</label> <input type="hidden" name="select_days_submit" value="1" /> <div style="color:yellow;background:red" class="error"> </div> </div>
选择选项
如果您已阅读 HtField
和 HtWidget
父类的文档,您 already know that you are supposed to use the context
method to populate the value of a field/widget. 在此情况下,您必须传递一个包含要在字段显示时被选中选项值的数组。
<?php use FlSouto\HtCklist; require("vendor/autoload.php"); $select = new HtCklist("select_days"); $select->options([1=>'Mon',2=>'Tue',3=>'Wed',4=>'Thu',5=>'Fri',6=>'Sat']); $select->context(['select_days'=>[2,5]]); // check Tue and Fri echo $select;
输出
<div class="widget 58cf20cba984c" style="display:block"> <label> <input type="checkbox" name="select_days[]" value="1" />Mon</label> <br /> <label> <input type="checkbox" name="select_days[]" value="2" checked="checked" />Tue</label> <br /> <label> <input type="checkbox" name="select_days[]" value="3" />Wed</label> <br /> <label> <input type="checkbox" name="select_days[]" value="4" />Thu</label> <br /> <label> <input type="checkbox" name="select_days[]" value="5" checked="checked" />Fri</label> <br /> <label> <input type="checkbox" name="select_days[]" value="6" />Sat</label> <br /> <input type="hidden" name="select_days_submit" value="1" /> <div style="color:yellow;background:red" class="error"> </div> </div>