我正在尝试在 SonataAdminBundle 中实现 onchange 下拉菜单。我的实体就像
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
<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....
But when i tried to click on create.its showing an error
我无法找出问题所在。
我们将不胜感激你的帮助