dongxie2613 2015-05-20 18:09
浏览 82

如何根据另一个列表中的选定值从下拉列表中选择默认值

In my Symfony project when I select an Employee from an 'Employee' dropdown list, I need to get a default value from another dropdown list, 'Department' that Employee is in.

Both dropdown lists, Employee and Department are already populated from the database.

I have a form in Symfony where I can manually select an Employee and a Department.

How is it possible to get an employee's default department on selecting an employee's name from a dropdown list?

In the FormType.php file I have:

class CommMemberType extends AbstractType

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
    ->add('employee','entity', array(
            'label' => 'Employee Name',
            'empty_value' => ' ',
            'class' => 'CompanyBundle:Employee',
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('e')
                    ->orderBy('e.fname', 'ASC');
            },
        )
    )

    ->add('dept', 'entity', array(
        'label' => 'Department',
        'empty_value' => '---Select Department---',
        'required' => true,
        'class' => 'CompanyBundle:Lookup\Dept',
        'property' => 'meaning',
        'query_builder' => function(EntityRepository $er) {
            return $er->createQueryBuilder('dept')
                ->orderBy('dept.meaning', 'ASC');
        },
    ));

I also have a jQuery code and I tested just the console.log part:

$(document).ready(function() {

$('body .employee').on('change', function(){
    var id =  $(this).val();
    console.log(id);

    $.post('{{ emp_jquery }}', { 'employee': id },function(data){
        $('.dept').html(data);
    },"text");
})
});

Could you please give me a hand? Thank you so much!

  • 写回答

1条回答 默认 最新

  • dsfs21312 2015-05-21 06:37
    关注

    You could pass the Dept entity as a parameter in the form constructor and then use 'data':

    class CommMemberType extends AbstractType 
    {
        private $dept;
    
        function __construct(Dept $dept = null)
        {
            $this->dept = $dept
        }
    
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
        $builder
            ->add('employee','entity', array(
                'label' => 'Employee Name',
                'empty_value' => ' ',
                'class' => 'CompanyBundle:Employee',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('e')
                        ->orderBy('e.fname', 'ASC');
                    },
                ) 
            )
    
            ->add('dept', 'entity', array(
                'label' => 'Department',
                'empty_value' => '---Select Department---',
                'required' => true,
                'class' => 'CompanyBundle:Lookup\Dept',
                'property' => 'meaning',
                'data' => $this->dept ? $this->dept->getMeaning() : null, // e.g.
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('dept')
                        ->orderBy('dept.meaning', 'ASC');
                },
            ));
    
       ....
    

    The, in the controller, you should pass the Department object to the form as a parameter:

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line