如何在Sonata Admin Bundle中使用jQuery UI datepicker创建一个doctrine_orm_datetime_range过滤器?
我试过以下,但它不工作:
protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper ->add('datumUitgevoerd','doctrine_orm_datetime',array('widget' => 'single_text'),null,array('required' => false,'attr' => array('class' => 'datepicker'))) ; }
解决方法
UPDATED新加坡02-02
如果您正在使用3. * Symfony,则必须使用以下twig配置:
#app / config / config.yml
枝条:
#…
form_themes:
– “SonataCoreBundle:Form:datepicker.html.twig”
更新2015-05-04
不再需要使用自定义日期选择器。 Sonata包含本机datetime选择器,可与Twitter Boostrap配合使用。
要激活datetime选择器表单字段,您必须在app / config.yml中的twig表单资源中添加SonataCoreBundle:Form:datepicker.html.twig:
twig: # ... form: resources: - 'SonataCoreBundle:Form:datepicker.html.twig'
您可以使用形式的选择器:
protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('createdAt','sonata_type_date_picker'); }
在datetime过滤器中:
protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper ->add('createdAt',array('field_type'=>'sonata_type_datetime_picker',)); }
或作为日期时间范围过滤器:
protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper ->add('createdAt','doctrine_orm_datetime_range',array('field_type'=>'sonata_type_datetime_range_picker',)); }
旧答案
要在doctrine_orm_datetime中使用datePicker,请使用以下代码:
protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper->add('datumUitgevoerd',array(),array('widget' => 'single_text','required' => false,'attr' => array('class' => 'datepicker'))); }
或者在doctrine_orm_datetime_range中使用datePicker,代码应该如下所示:
protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper->add('datumUitgevoerd','attr' => array('class' => 'datepicker'))); }
并且您应该重载主模板以添加您的自定义javascript文件来初始化DatePicker。
#File app/config.yml sonata_admin: title: Admin title_logo: /logo_admin.png templates: layout: AcmeDemoBundle::standard_layout.html.twig #...another Sonata and Symfony settings...
{# File src/Acme/Bundle/DemoBundle/Resources/views/standard_layout.html.twig #} {% extends 'SonataAdminBundle::standard_layout.html.twig' %} {% block javascripts %} {{ parent() }} <script src="{{ asset('bundles/acmedemo/js/jquery_admin.js') }}" type="text/javascript"></script> {% endblock %}
//File web\bundles\acmedemo\js\jquery_admin.js jQuery(document).ready(function(){ jQuery.datepicker.setDefaults( jQuery.datepicker.regional[ "" ] ); jQuery(".datepicker").datepicker( jQuery.datepicker.regional[ "en" ]); });