Typo3 结束,以便每个复选框接收正确的值! 所以我们得到这个1+0+4+8+0+32+64,因为第一、第三、第四、第六和第七个复选框被选中
在方法countBits中,我们只需将所有整数值添加到一个数字Typo3 结束,以便每个复选框接收正确的值! 所以我们得到这个1+0+4+8+0+32+64,因为第一、第三、第四、第六和第七个复选框被选中,typo3,fluid,extbase,bitmask,typo3-extensions,Typo3,Fluid,Extbase,Bitmask,Typo3 Extensions,在方法countBits中,我们只需将所有整数值添加到一个数字 namespace Vendor\Example\Utility; use TYPO3\CMS\Core\Utility\GeneralUtility; class CheckboxUtility extends GeneralUtility { /** * Convert an integer to binary and then convert each bit back to an integer for
namespace Vendor\Example\Utility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class CheckboxUtility extends GeneralUtility
{
/**
* Convert an integer to binary and then convert each bit back to an integer for use with multiple checkboxes.
*
* @param int $value
* @return array
*/
public static function convertDataForMultipleCheckboxes(int $value): array
{
$bin = decbin($value); // convert dec to bin
$num = strlen($bin); // counts the bits
$res = array();
for ($i = 0; $i < $num; $i++) {
// loop through binary value
if ($bin[$i] !== 0) {
$bin_2 = str_pad($bin[$i], $num - $i, '0'); //pad string
$res[] = bindec($bin_2); // convert that bit to dec and push in array
}
}
return array_reverse($res); // reverse order and return
}
/**
* Adds the values of the checkboxes
*
* @param array $value
* @return int
*/
public static function countBits(array $value): int
{
foreach ($value as $key => $item) {
$res = $res + $item;
}
return $res;
}
}
名称空间供应商\Example\Utility;
使用TYPO3\CMS\Core\Utility\GeneralUtility;
类CheckboxUtility扩展了通用性
{
/**
*将整数转换为二进制,然后将每个位转换回整数,以便与多个复选框一起使用。
*
*@param int$value
*@return数组
*/
公共静态函数ConvertDataFormMultipleCheckBox(int$value):数组
{
$bin=decbin($value);//将dec转换为bin
$num=strlen($bin);//计算位
$res=array();
对于($i=0;$i<$num;$i++){
//循环遍历二进制值
如果($bin[$i]!==0){
$bin_2=str_pad($bin[$i],$num-$i,'0');//填充字符串
$res[]=bindec($bin_2);//将该位转换为dec并推入数组
}
}
return数组_reverse($res);//逆序返回
}
/**
*添加复选框的值
*
*@param数组$value
*@return int
*/
公共静态函数countBits(数组$value):int
{
foreach($key=>$item的值){
$res=$res+$item;
}
返回$res;
}
}
在模板中或部分
参数multiple=“1”在这里很重要。这将向属性天数数组添加一个额外维度。(这可以在网站的源代码中看到)。
根据二进制表示法为复选框指定正确的值是很重要的。
当我们从数据库中读取值时,结果作为数组提供给我们。因此,我们在适当的位置(从0开始)按照与复选框顺序相同的顺序读取附加维度。e、 g.第七个值/复选框:checked=“{week.days.6}==64”
。。。现在快乐编码 通过评估位掩码,可以解决多个复选框的问题 程序员通常希望将一些数据读入表单,然后将其输出为文本。这里有几个例子 有时程序员希望用多个复选框在同一表单中显示表单数据,以便用户可以更改数据。没有这样的例子,许多程序员发现很难一点一点地读取数据,然后再输出 以下是一个工作示例(在BE和FE中): (使用Typo3 9.5.20和10.4.9进行测试) 在TCA问题示例中:
'days' => [
'exclude' => false,
'label' => 'LLL:EXT:example/Resources/Private/Language/locallang_db.xlf:tx_example_domain_model_week.days',
'config' => [
'type' => 'check',
'items' => [
['monday', ''],
['thuesday', ''],
['wednesday', ''],
['thursday', ''],
['friday', ''],
['saturday', ''],
['sunday', ''],
],
'default' => 0,
]
],
模型:
属性的类型必须为整数。
但是,getter和setter是数组,因为我们有一个多复选框,这是用数组实现的。
记住这一点很重要,因为这将产生一个需要解决的问题
class Week extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* Days
*
* @var int
*/
protected $days = 0;
/**
* Returns the days
*
* @return array $days
*/
public function getDays()
{
return $this->days;
}
/**
* Sets the days
*
* @param array $days
* @return void
*/
public function setDays($days)
{
$this->days = $days;
}
}
在控制器中
在initializeCreateAction和initializeUpdateAction中,我们解决了整数和数组之间不同属性类型的问题。
否则,我们将收到一条错误消息,表示数组无法转换为整数。
这段代码意味着Extbase应该保留属性类型
在createAction和updateAction中,我们分支到CheckboxUtility中的countBits方法,以添加所选复选框的值。
在editAction和updateAction中,我们分支到CheckboxUtility中的ConvertDataForMultipleCheckBox方法,以便将值转换为输入和输出
/**
* initializeCreateAction
* @return void
*/
public function initializeCreateAction(): void
{
if ($this->arguments->hasArgument('newWeek')) {
$this->arguments->getArgument('newWeek')->getPropertyMappingConfiguration()->setTargetTypeForSubProperty('days', 'array');
}
}
/**
* action create
*
* @param Week $newWeek
* @return void
*/
public function createAction(Week $newWeek)
{
$days = (int)CheckboxUtility::countBits($newWeek->getDays());
$newWeek->setDays($days);
$this->weekRepository->add($newWeek);
$this->redirect('list');
}
/**
* action edit
*
* @param Week $week
* @return void
*/
public function editAction(Week $week)
{
$week->setDays(CheckboxUtility::convertDataForMultipleCheckboxes((int)$week->getDays()));
$this->view->assign('week', $week);
}
/**
* initializeUpdateAction
* @return void
*/
public function initializeUpdateAction(): void
{
if ($this->arguments->hasArgument('week')) {
$this->arguments->getArgument('week')->getPropertyMappingConfiguration()->setTargetTypeForSubProperty('days', 'array');
}
}
/**
* action update
*
* @param Week $week
* @return void
*/
public function updateAction(Week $week)
{
$days = (int)CheckboxUtility::countBits($week->getDays());
$week->setDays($days);
$this->weekRepository->update($week);
$this->redirect('list');
}
在Classes/Utility/CheckboxUtility.php中
阅读代码。在每一点上都描述了该程序
在方法ConvertDataFormMultipleCheckBox中,基本方向如下:
数据库中有一个整数值,例如109。
二进制表示法:1011011(64+32+0+8+4+0+1=109)
在表单中,这意味着选中了第一、第三、第四、第六和第七个复选框
我们从左到右读取二进制值,在1011011处分为七个循环。
例如,让我们读取第一个字符(从左边),我们用0覆盖右边的六个字符。这将导致二进制数1000000,十进制表示法=64。
例如,让我们读第四个字符(从左边开始),我们用0覆盖右边的三个字符。这将导致二进制数1000,十进制表示法=8
当我们读过这篇文章后,我们将得到结果64+32+0+8+4+0+1,因为我们是从左向右读的。
因此,我们在最后将结果转过来,以便每个复选框都接收到正确的值!
所以我们得到这个1+0+4+8+0+32+64,因为第一、第三、第四、第六和第七个复选框被选中
在方法countBits中,我们只需将所有整数值添加到一个数字
namespace Vendor\Example\Utility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class CheckboxUtility extends GeneralUtility
{
/**
* Convert an integer to binary and then convert each bit back to an integer for use with multiple checkboxes.
*
* @param int $value
* @return array
*/
public static function convertDataForMultipleCheckboxes(int $value): array
{
$bin = decbin($value); // convert dec to bin
$num = strlen($bin); // counts the bits
$res = array();
for ($i = 0; $i < $num; $i++) {
// loop through binary value
if ($bin[$i] !== 0) {
$bin_2 = str_pad($bin[$i], $num - $i, '0'); //pad string
$res[] = bindec($bin_2); // convert that bit to dec and push in array
}
}
return array_reverse($res); // reverse order and return
}
/**
* Adds the values of the checkboxes
*
* @param array $value
* @return int
*/
public static function countBits(array $value): int
{
foreach ($value as $key => $item) {
$res = $res + $item;
}
return $res;
}
}
名称空间供应商\Example\Utility;
使用TYPO3\CMS\Core\Utility\GeneralUtility;
类CheckboxUtility扩展了通用性
{
/**
*将整数转换为二进制,然后将每个位转换回整数,以便与多个复选框一起使用。
*
*@param int$value
*@return数组
*/
公共静态函数ConvertDataFormMultipleCheckBox(int$value):数组
{
$bin=decbin($value);//将dec转换为bin
$num=strlen($bin);//计算位
$res=array();
对于($i=0;$i<$num;$i++){
//循环遍历二进制值
如果($bin[$i]!==0){
$bin_2=str_pad($bin[$i],$num-$i,'0');//填充字符串
$res[]=bindec($bin_2);//将该位转换为dec并推入数组
}
}
return数组_reverse($res);//逆序返回
}
/**
*添加复选框的值
*
*@param数组$value
*@return int
*/
公众的
class Week extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* Days
*
* @var int
*/
protected $days = 0;
/**
* Returns the days
*
* @return array $days
*/
public function getDays()
{
return $this->days;
}
/**
* Sets the days
*
* @param array $days
* @return void
*/
public function setDays($days)
{
$this->days = $days;
}
}
/**
* initializeCreateAction
* @return void
*/
public function initializeCreateAction(): void
{
if ($this->arguments->hasArgument('newWeek')) {
$this->arguments->getArgument('newWeek')->getPropertyMappingConfiguration()->setTargetTypeForSubProperty('days', 'array');
}
}
/**
* action create
*
* @param Week $newWeek
* @return void
*/
public function createAction(Week $newWeek)
{
$days = (int)CheckboxUtility::countBits($newWeek->getDays());
$newWeek->setDays($days);
$this->weekRepository->add($newWeek);
$this->redirect('list');
}
/**
* action edit
*
* @param Week $week
* @return void
*/
public function editAction(Week $week)
{
$week->setDays(CheckboxUtility::convertDataForMultipleCheckboxes((int)$week->getDays()));
$this->view->assign('week', $week);
}
/**
* initializeUpdateAction
* @return void
*/
public function initializeUpdateAction(): void
{
if ($this->arguments->hasArgument('week')) {
$this->arguments->getArgument('week')->getPropertyMappingConfiguration()->setTargetTypeForSubProperty('days', 'array');
}
}
/**
* action update
*
* @param Week $week
* @return void
*/
public function updateAction(Week $week)
{
$days = (int)CheckboxUtility::countBits($week->getDays());
$week->setDays($days);
$this->weekRepository->update($week);
$this->redirect('list');
}
namespace Vendor\Example\Utility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class CheckboxUtility extends GeneralUtility
{
/**
* Convert an integer to binary and then convert each bit back to an integer for use with multiple checkboxes.
*
* @param int $value
* @return array
*/
public static function convertDataForMultipleCheckboxes(int $value): array
{
$bin = decbin($value); // convert dec to bin
$num = strlen($bin); // counts the bits
$res = array();
for ($i = 0; $i < $num; $i++) {
// loop through binary value
if ($bin[$i] !== 0) {
$bin_2 = str_pad($bin[$i], $num - $i, '0'); //pad string
$res[] = bindec($bin_2); // convert that bit to dec and push in array
}
}
return array_reverse($res); // reverse order and return
}
/**
* Adds the values of the checkboxes
*
* @param array $value
* @return int
*/
public static function countBits(array $value): int
{
foreach ($value as $key => $item) {
$res = $res + $item;
}
return $res;
}
}
<f:form.checkbox
id="day_1"
property="days"
value="1"
multiple="1"
checked="{week.days.0} == 1" />
<label for="day_1" class="form-control-label">
<f:translate key="tx_example_domain_model_week.day1" />
</label>
<f:form.checkbox
id="day_2"
property="days"
value="2"
multiple="1"
checked="{week.days.1} == 2" />
<label for="day_2" class="form-control-label">
<f:translate key="tx_example_domain_model_week.day2" />
</label>
<f:form.checkbox
id="day_3"
property="days"
value="4"
multiple="1"
checked="{week.days.2} == 4" />
<label for="day_3" class="form-control-label">
<f:translate key="tx_example_domain_model_week.day3" />
</label>
<f:form.checkbox
id="day_4"
property="days"
value="8"
multiple="1"
checked="{week.days.3} == 8" />
<label for="day_4" class="form-control-label">
<f:translate key="tx_example_domain_model_week.day4" />
</label>
<f:form.checkbox
id="day_5"
property="days"
value="16"
multiple="1"
checked="{week.days.4} == 16" />
<label for="day_5" class="form-control-label">
<f:translate key="tx_example_domain_model_week.day5" />
</label>
<f:form.checkbox
id="day_6"
property="days"
value="32"
multiple="1"
checked="{week.days.5} == 32" />
<label for="day_6" class="form-control-label">
<f:translate key="tx_example_domain_model_week.day6" />
</label>
<f:form.checkbox
id="day_7"
property="days"
value="64"
multiple="1"
checked="{week.days.6} == 64" />
<label for="day_7" class="form-control-label">
<f:translate key="tx_example_domain_model_week.day7" />
</label>