Dropdown Ajax onchange SonataAdminBundle Symfony2 问题 [英] Dropdown Ajax onchange SonataAdminBundle Symfony2 Issue

查看:28
本文介绍了Dropdown Ajax onchange SonataAdminBundle Symfony2 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SonataAdminBundle 中实现 onchange 下拉菜单.我的实体就像

I am trying to implement onchange dropdown in SonataAdminBundle. My Entity is like

 class BuilderHomePage
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

 /**
 * @var \Hello\FrontendBundle\Entity\MtContentType
 *
 * @ORM\ManyToOne(targetEntity="Hello\FrontendBundle\Entity\MtContentType")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="content_type_id", referencedColumnName="id")
 * })
 */
private $section;


/**
 * @var string
 *
 * @ORM\Column(name="title", type="string",length=100, nullable=false)
 */
private $title;

我的管理类

     public function getTemplate($name)
     {
       switch ($name) {
        case 'edit':
            if ($this->getSubject()->getId()) {
                return 'HelloAdminBundle:Builder:base_edit.html.twig';
            } else {
                return 'HelloAdminBundle:Builder:base_edit.html.twig';
            }
            break;
        default:
            return parent::getTemplate($name);
            break;
    }

}

protected function configureRoutes(RouteCollection $collection) {
$collection
    ->add('getArticleFromSection', 'getArticleFromSection')
    ;
}
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper

        ->add('section')
        ->add('title','choice',array('required' => false ))

    ;
}

我的生成器:base_edit.html.twig

My Builder:base_edit.html.twig

     <script type="text/javascript">
     $(document).ready(function()
       {
         $("#{{ admin.uniqId }}_section").change(function()
        {
        var id=$(this).val();
        var dataString = 'id='+ id;
        $.ajax
        ({
            type: "POST",
            url: "{{ admin.generateObjectUrl('getArticleFromSection', object) }}",
            data: dataString,
            cache: false,
            success: function(html)
            {
                $("#{{ admin.uniqId }}_title").html(html);
            } 
        });
       });

     });

</script>

Ajax 请求控制器

    $article        = $this->get('hello_frontend.article');
    $totalArticle        = $article->getArticleByContentType($id);

     $html = "";
    foreach($totalArticle as $res){
    $html .="<option value=".$res->getId().">".$res->getTitle()."</option>";
    }

直到现在一切正常....

Till now everything works fine....

但是当我尝试单击 create.its 时显示错误

But when i tried to click on create.its showing an error

我无法弄清楚问题所在.您的帮助将不胜感激

I am not able to figure out the problem. your help will be appreciated

推荐答案

Noscope,正是他想做的.他想动态填充第二个选择,所以他无法在管理类中填充它.

Noscope, it is exactly what he wants to do. He wants to populate the second select dynamically, so there is no way he could populate it in the admin class.

我遇到了同样的问题,我找不到如何解决它.我现在找到的唯一方法是检查 $this->container->get('request')->request->get('YourFormName[your_field]', null, true) 在我的行动中.

I have the same problem and I can't find how to solve it. The only way I've found for now is to check $this->container->get('request')->request->get('YourFormName[your_field]', null, true) in my action.

这篇关于Dropdown Ajax onchange SonataAdminBundle Symfony2 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆