symfony - 如何以 symfony 形式显示一组 js 对象?
问题描述
Symfony 5.3 + EasyAdmin 3.3.2 + Doctrine
问 SO 问题的新方法:简短而简单(我以前被禁止提问,我不知道我做错了什么)
我所拥有的: 我有一个实体“设备”,它有一个字段“属性列表”。
属性列表的示例内容:
[ {"key":"color", "value":"blue", "type":"text"}, {"key":"amount", "value":"4", "type":"number"}, {"key":"consumable", "value":"true", "type":"boolean"} ]
我想要什么: 该数组中的每个 js 对象都应该呈现在一个表单行中(在本例中为编辑表单)。“color”是标签文本,“blue”是输入值,“text”是表单小部件的输入 HTML 元素的类型。
我需要能够在(编辑)表单中编辑、删除和添加新属性。我认为更容易维护代码并使其尽可能减少树枝调整的数量,并改用 symfony/easyadmin 的板载工具/服务。
当前方法: 这就是我最近通过数据映射器尝试它的原因。从它可以做什么的描述听起来正是我需要的。可悲的是,使用 setData() 函数它只会更改输入值。标签适用于所有属性“值”,字段类型始终为文本。
DeviceCrudController.php
$attributeListForm = CollectionField::new('attributeList'," ")->setEntryType(JsonAttributeType::class);
JsonAttributeType.php
class JsonAttributeType extends AbstractType implements DataMapperInterface
{
public function mapDataToForms($viewData, iterable $forms)
{
$forms = iterator_to_array($forms);
$forms['value']->setData($viewData["key"]); // if I could set the type of the form and the label text here, it would help me a lot
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('value', TextType::class)->setDataMapper($this);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'empty_data' => null,
));
}
}
解决方案
推荐阅读
- javascript - JS - 其他 SO 帖子说“this”应该保留在内联函数中。我误解了什么?
- html - 两行下拉菜单
- java - TestContainers - 无法连接到 MySQLContainer Docker - 集成测试
- javascript - 在 XMLHttprequest 中获取图像缩略图
- bash - bash:替换大括号内的字符串
- reactjs - 将值从组件传递到上下文,并在 componentDidMount() 方法中使用该值
- javascript - 更新 d3 数据反应钩子
- api - 来自 API 的 Flutter ListView 数据未显示在我的网络应用程序上
- c - ymm0 寄存器(不是内存)中单个浮点数的位表示是什么?
- mysql - 如何处理在mysql中返回多于1行的子查询