通过评估位掩码的多个复选框的可能解决方案
程序员经常希望将一些数据读入表单,然后将其输出为文本。这里有几个例子。
有时程序员希望通过多个复选框在同一个表单中显示表单数据,以便用户可以更改数据。没有这方面的例子,很多程序员发现很难逐位读取数据然后再次输出。
这是一个工作示例(在 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 中的 convertDataForMultipleCheckboxes 方法,以便转换要输入和输出的值。
/**
* 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 中
阅读代码。该过程在每一点都有描述。
convertDataForMultipleCheckboxes 方法的基本方向如下:
我们在数据库中有一个整数值,例如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;
}
}
在模板或部分
中
参数 multiple="1" 在这里很重要。这为属性天数数组添加了一个额外的维度。 (这可以在网站的源代码中看到)。
根据二进制符号为复选框赋予正确的值很重要。
当我们从数据库中读取值时,结果可以作为数组提供给我们。所以我们按照与复选框的顺序相同的顺序在适当的位置(从 0 开始)读取附加维度。例如第七个值/复选框:checked = "{week.days.6} == 64"
<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>
...现在快乐编码!