'template' 和 'editable' 之间的 Sonata Admin 冲突
Sonata Admin conflict between 'template' and 'editable'
我目前正在做一个小的个人项目来学习如何操作 Symfony 和 Sonata,我发现自己遇到了一个小问题。我已将我的一个变量限制为 "configureListFields" 中的 "template",但我无法将其提交到 "editable"。如果不是我向您展示的 "editable" 错误,我可以单独做一个,但不能同时做两个。
列表:
$listMapper->add('status', 'string', array(
'template' => 'WebBundle:Default:list_client.html.twig',
'label'=> 'Status'))
表格:
$formMapper->add('Status', 'choice', array(
'choices' => array(
'Client' => 'Client',
'Ex-Client' => 'Ex-Client',
'Prospect' => 'Prospect')))
模板:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
<p class="ClientStatus {% if object.Status == 'Ex-Client' %}
label label-danger
{% elseif object.Status == 'Client' %}
label label-success
{% else %}
label label-info
{% endif %}" >
{{ object.Status }}
</p>
</div>
{% endblock %}
使用该配置查看:
第二个配置:
$listMapper->add('status', 'choice', array(
'choices'=>array(
"Client"=>"Client",
"New Client"=>"New Client",
"Ex-Client"=>"Ex Client"
),
'label'=> 'Status',
'editable'=>true))
查看:
第三配置:
->add('status', 'choice', array(
'choices'=>array(
"Client"=>"Client",
"New Client"=>"New Client",
"Ex-Client"=>"Ex Client"
),
'template' => 'WebBundle:Default:list_client.html.twig',
'label'=> 'Status',
'editable'=>true))
视图:
所以 "template" 和 "editable" 之间似乎在如何处理这个问题上存在冲突?
非常感谢。
查看随奏鸣曲源提供的 list_choice 模板。
# SonataAdminBundle/Resources/views/CRUD/list_choice.html.twig
{% set is_editable =
field_description.options.editable is defined and
field_description.options.editable and
admin.hasAccess('edit', object)
%}
{% set x_editable_type = field_description.type|sonata_xeditable_type %}
{% if is_editable and x_editable_type %}
{% block field_span_attributes %}
{% spaceless %}
{{ parent() }}
data-source="{{ field_description|sonata_xeditable_choices|json_encode }}"
{% endspaceless %}
{% endblock %}
{% endif %}
他们检查该字段是否可编辑并将 data-source
属性添加到父 base_list_field 模板部分的 field_span_attributes
块:
<span {% block field_span_attributes %}class="x-editable"
data-type="{{ xEditableType }}"
data-value="{{ data_value }}"
data-title="{{ field_description.label|trans({}, field_description.translationDomain) }}"
data-pk="{{ admin.id(object) }}"
data-url="{{ url }}" {% endblock %}>
{{ block('field') }}
</span>
因此,也请尝试在您的自定义模板中添加数据源。
这个答案对我有用:
如果您检查 HTML 代码,在您选择的元素上,您可以检测是否缺少任何标签,例如下面的代码是生成没有 [=34 的页面的代码=] 配置选项:
<span class="x-editable label-info editable editable-click editable-open" data-type="select"
data-value="Rechazado"
data-title="Estado"
data-pk="4"
data-url="/eventos/web/app_dev.php/admin/core/set-object-field-value?context=list&field=estado&objectId=4&code=admin.evento"
data-source="[{"value":"Aprobado","text":"1"},{"value":"Pendiente","text":"Pendiente"},{"value":"Rechazado","text":"Rechazado"}]" data-original-title="" title="" aria-describedby="popover786605">
</span>
现在检查设置 'template' 选项时生成的代码
<span class="x-editable label label-danger editable editable-click"
data-type="select"
data-value="Rechazado"
data-title="Estado"
data-pk="4"
data-url="/eventos/web/app_dev.php/admin/core/set-object-field-value?context=list&field=estado&objectId=4&code=admin.evento">
</span>
缺少数据源字段,这就是它崩溃的原因。
解决方案很简单(不是最好的,但它是某种东西)在您的模板上添加带有您选择的数据的数据源。
我目前正在做一个小的个人项目来学习如何操作 Symfony 和 Sonata,我发现自己遇到了一个小问题。我已将我的一个变量限制为 "configureListFields" 中的 "template",但我无法将其提交到 "editable"。如果不是我向您展示的 "editable" 错误,我可以单独做一个,但不能同时做两个。
列表:
$listMapper->add('status', 'string', array(
'template' => 'WebBundle:Default:list_client.html.twig',
'label'=> 'Status'))
表格:
$formMapper->add('Status', 'choice', array(
'choices' => array(
'Client' => 'Client',
'Ex-Client' => 'Ex-Client',
'Prospect' => 'Prospect')))
模板:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
<p class="ClientStatus {% if object.Status == 'Ex-Client' %}
label label-danger
{% elseif object.Status == 'Client' %}
label label-success
{% else %}
label label-info
{% endif %}" >
{{ object.Status }}
</p>
</div>
{% endblock %}
使用该配置查看:
第二个配置:
$listMapper->add('status', 'choice', array(
'choices'=>array(
"Client"=>"Client",
"New Client"=>"New Client",
"Ex-Client"=>"Ex Client"
),
'label'=> 'Status',
'editable'=>true))
查看:
第三配置:
->add('status', 'choice', array(
'choices'=>array(
"Client"=>"Client",
"New Client"=>"New Client",
"Ex-Client"=>"Ex Client"
),
'template' => 'WebBundle:Default:list_client.html.twig',
'label'=> 'Status',
'editable'=>true))
视图:
所以 "template" 和 "editable" 之间似乎在如何处理这个问题上存在冲突? 非常感谢。
查看随奏鸣曲源提供的 list_choice 模板。
# SonataAdminBundle/Resources/views/CRUD/list_choice.html.twig
{% set is_editable =
field_description.options.editable is defined and
field_description.options.editable and
admin.hasAccess('edit', object)
%}
{% set x_editable_type = field_description.type|sonata_xeditable_type %}
{% if is_editable and x_editable_type %}
{% block field_span_attributes %}
{% spaceless %}
{{ parent() }}
data-source="{{ field_description|sonata_xeditable_choices|json_encode }}"
{% endspaceless %}
{% endblock %}
{% endif %}
他们检查该字段是否可编辑并将 data-source
属性添加到父 base_list_field 模板部分的 field_span_attributes
块:
<span {% block field_span_attributes %}class="x-editable"
data-type="{{ xEditableType }}"
data-value="{{ data_value }}"
data-title="{{ field_description.label|trans({}, field_description.translationDomain) }}"
data-pk="{{ admin.id(object) }}"
data-url="{{ url }}" {% endblock %}>
{{ block('field') }}
</span>
因此,也请尝试在您的自定义模板中添加数据源。
这个答案对我有用:
如果您检查 HTML 代码,在您选择的元素上,您可以检测是否缺少任何标签,例如下面的代码是生成没有 [=34 的页面的代码=] 配置选项:
<span class="x-editable label-info editable editable-click editable-open" data-type="select"
data-value="Rechazado"
data-title="Estado"
data-pk="4"
data-url="/eventos/web/app_dev.php/admin/core/set-object-field-value?context=list&field=estado&objectId=4&code=admin.evento"
data-source="[{"value":"Aprobado","text":"1"},{"value":"Pendiente","text":"Pendiente"},{"value":"Rechazado","text":"Rechazado"}]" data-original-title="" title="" aria-describedby="popover786605">
</span>
现在检查设置 'template' 选项时生成的代码
<span class="x-editable label label-danger editable editable-click"
data-type="select"
data-value="Rechazado"
data-title="Estado"
data-pk="4"
data-url="/eventos/web/app_dev.php/admin/core/set-object-field-value?context=list&field=estado&objectId=4&code=admin.evento">
</span>
缺少数据源字段,这就是它崩溃的原因。
解决方案很简单(不是最好的,但它是某种东西)在您的模板上添加带有您选择的数据的数据源。