2013-04-10 79 views
0

我是樹枝新手。有人可以告訴我如何傳遞選擇框的選定值,以便在加載ajax表單時選擇它?樹枝設置選定值

我在做什麼錯?

warehouse_id = 32在這個例子中,和form_widget自動創建我的選擇列表。

{{ form_widget(form.warehouse, {'selected': warehouse_id }) }} 

非常感謝!

+1

通常情況下,您可以通過將實體傳遞給窗體來讓它自動生根。 – 2013-04-10 22:45:35

+0

我們可以看看你如何建立表單嗎? – 2013-04-10 22:59:24

回答

1

這是我發現的最簡單的方法:{{form_widget(form.field,{值:1 })}}

+1

我使用symfony 2.3,我觀察到的id是用雙引號括起來的。 我從[這裏](http://stackoverflow.com/questions/18270513/symfony2-twig-set-the-default-value-in-the-dropdownlist)以及它的工作。 – 2014-04-16 07:17:05

+0

^^^^^^^^^^^^^^^^^^^^^^^看看@GhanshyamKDobariya的評論 – ziiweb 2015-02-27 00:14:27

+0

如果你正在創建一個沒有附加到學說實體的表單,或者你'想要指定樹枝下拉的選定值,這是正確的答案。在symfony文檔中不清楚設置值會選擇指定的選項,但它確實可以。 – Halfstop 2015-04-06 15:12:18

0

我認爲你不需要這樣做。創建表單時,您可以傳遞實體(包含數據)。您的下拉菜單將包含所選數據。 看到這個代碼,例如:

$em = $this->getDoctrine()->getManager(); 
$entity = $em->getRepository('YourBundle:YourEntity')->find($id); 
if (!$entity) { 
    throw $this->createNotFoundException('Unable to find entity.'); 
} 

$editForm = $this->createForm(new YourFormType(), $entity); 
//in entity goes with all data and your selectbox will be selected with correct data 
return $this->render('YourBundle:YourController:edit.html.twig', array(
    'entity'  => $entity, 
    'edit_form' => $editForm->createView(), 
)); 

或嘗試做到這一點!通過改變「價值」選擇'

{{ form_widget(form.warehouse, {'value': warehouse_id }) }} 

請告訴我,如果你解決你的疑問

+0

由於某些原因,當我通過ajax創建頁面時不起作用。我最終做的只是將值傳遞到ajax頁面,然後這樣做,它的工作... {{form_widget(form.warehouse,{value:warehouse_id})}} – LargeTuna 2013-04-15 16:07:33