문제

I'm attempting to dynamically load a select box's options based on some existing criteria in my database. The useful information is that there are 3 classes, a Pet, which has a breed and type, a Breed, which has a name and type, and Type which is something like 'Dog' or 'Cat'.

When editing a pet I have two drop-downs populated from Doctrine/Symfony2.

Type [Select One]
Breed [Select One]

Currently the Breed select lists all breeds of dogs and cats, I'd like to select the 'Type' and have that force the Breed select box to only list Breeds based on that type.

I've done this with raw php/javascript before using Symfony figured there's some best practice for handling it in Symfony.

Any advice would be appreciated. I can provide any additional information needed, but wanted to keep it as simple as possible.

도움이 되었습니까?

해결책

I didnt found any other way than a .change jquery on the first select, then an ajax call with the selected option to repopulate the target select.

$('#Your_source_select').change(function()
{
    $.ajax({
    type: 'POST',
    url: Routing.generate('AJAXDevisOnduleurOptions', { onduleurId: $('#Your_source_select').val() }),
    success: function(msg){
        $('#Your_target_select').children().remove().end().append(msg);
    }
});
});

Please note that Your_source/target_select are entity fields. But it seems that you got that part.

You might want to use FOSJSRoutingBundle => https://github.com/FriendsOfSymfony/FOSJsRoutingBundle It allows you to build route in your javascript using twig syntax. So it allows you access to its variables.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top