简体   繁体   中英

Sonata admin dropdown with ajax data results

I am working on a sonata admin panel using symfony 3.3. I have two entities Category and Chalets . They are associated many to many so that One category have many Chalets and Chalet can be associated to many Categories.

I want the functionality in my admin panel to show the Chalets based on Category choosen from dropdown and a button to replicate the same to add another category with Chalets and so on.

The picture below will give you the deep insights. Picture of my current functionality

Right now all the Chalets added in the database are visible in the dropdown I want to show only the selected category Chalets to be shown. Alson, the button(Add more) will replicate the Category and Chalet to repeat the process. Thanks!

What you need is sonata_type_collection field.

It will look a bit different out of the box, but capabilities is exactly what you want.

So, how it works:

  • sonata_type_collection field will include a table inside of your edit form. Every row in this table is another Category.
  • there is possibility to add "Add" and "Delete" buttons, so you could add and delete categories directly from this edit form.
  • second column in this embed table could be "Charlets" field, and it can be editable inline (select2 elements). So you can choose charlets for every category.
  • when you add Category - it's a "create" form, it has only Category dropdown. But when you edit a row inline - it's an "edit" form, it will has also "Charlets" multi select. And options for this multi select can be generated based on chosen Category.

So, all this will work without any custom templates/Ajax calls.

P S. If you still want to customize UI - easier to do it with JavaScript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM