简体   繁体   中英

auto calculate field value in sonata Admin Bundle

I use symfony + sonataAdminBundle for my application and I have an entity "Produit" with 2 inputs "PrixAchat" and "PrixVenteTTC" and another field called "marge" that I like Calculated his value in with a formula with the other input, this is why I have create an disabled input for this field :

ProduitAdmin

 protected function configureFormFields(FormMapper $formMapper)
  {
    $formMapper
        ->add('prixAchat')
        ->add('prixVenteTTC')

        ->add('marge',null,array(
        'label' => 'Marge',
        'read_only' => true,
        'disabled'  => true,
        'attr' =>array(
        'template' => 'CRUD/marge.html.twig',
        )))
   }

Template: Marge.html.twig

{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}



{% block javascripts %}
{{ parent() }}
 <script type="text/javascript">

  $(function(){


     var prixVente = document.getElementById('form [id$="_prixVenteTTC"]').value;
    var prixAchat = document.getElementById('form [id$="_prixAchat"]').value; 

    document.getElementById('form [id$="_marge"]').value = ((prixVente/prixAchat)*100)-100;

    }); 



 </script>
 {% endblock %}

But the value is not calculed .. Somewone have an idea and can help me please ?

That code only runs once. You should add a change listener to the input fields that triggers recalculating the price

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