1

I have question about calculation multiply form fields on event change. Calaculation for new form fields form modila works fine. I have on each form field event if img-X is clicked the form field must be erased, and all fields must be calculated again for new result after that field is deleted. The problem is in my <tr></tr> tags insade table, i have text with class pdv, and that field must be calculated with sum of two values kolicina * cijena, I mean calculation of three fields give result for output. Better explanation I will provide with code below, and will explain in details.

html

<tbody>
    <tr class="opa">
        <td>234</td>
        <td>Penkala za ured</td>
        <td>
            <input type="text" class="form-control" placeholder=" Napomena"/>
        </td>
        <td>Komad</td>
        <td>
            <input type="text" class="form-control kolicina" placeholder=" Količina" />
        </td>
        <td>
            <input type="text" class="form-control cijena" placeholder=" Cjena"/>
        </td>
        <td>
            <input type="text" class="form-control popust" placeholder=" Popust"/>
        </td>
        <td class="pdv">13%</td>
        <td>10.00</td>
        <td>
            <img class="img-responsive remove_element" src="../images/forms/icon_delete.png"/>
        </td>
    </tr>
    <tr class="opa">
        <td>1</td>
        <td>Čokolada</td>
        <td>
            <input type="text" class="form-control" placeholder=" Napomena"/>
        </td>
        <td>Komad</td>
        <td>
            <input type="text" class="form-control kolicina" placeholder=" Količina"/>
        </td>
        <td>
            <input type="text" class="form-control cijena" placeholder=" Cjena"/>
        </td>
        <td>
            <input type="text" class="form-control popust" placeholder=" Popust"/>
        </td>
        <td class="pdv">25%</td>
        <td>5.30</td>
        <td>
            <img class="img-responsive remove_element" src="../images/forms/icon_delete.png"/>
        </td>
    </tr>
    <tr class="opa">
        <td>52</td>
        <td>Pranje auta</td>
        <td>
            <input type="text" class="form-control" placeholder=" Napomena"/>
        </td>
        <td>Komad</td>
        <td>
            <input type="text" class="form-control kolicina" placeholder=" Količina"/>
        </td>
        <td>
            <input type="text" class="form-control cijena" placeholder=" Cjena"/>
        </td>
        <td>
            <input type="text" class="form-control popust" placeholder=" Popust"/>
        </td>
        <td class="pdv">25%</td>
        <td>6.40</td>
        <td>
            <img class="img-responsive remove_element" src="../forms/icon_delete.png"/>
        </td>
    </tr>
</tbody>

This is my table with results, you can see class kolicina - cijena - popust - pdv, that is all for calculation of form fields. The question is how to take current text from class pdv and calculate with kolicina - cijena - popust, then after calculation append to some element in html.

In jQuery I have acomplished calculation of fields kolicina - cijena - popust, and append them to HTML elemnet. The problem is when pdv text value is 25 or 13 or 5 calculation must go into separated html element.

html

<span><a class="sum">0 </a></span>
<span><a class="popust_count">0 </a></span>
<span><a class="popust_all">0 </a></span>
<span><a class="sum">0 </a></span>
<span>PDV 25%: <a class ="pdv_25">0 </a> | PDV 13%: <a class="pdv_13">0 </a> | PDV 5%: <a class="pdv_5">0 </a></span>

And my jQuery code looks like: This code works.

$(".prod_go input").keyup(multInputs);

                    $(".remove_element").click(function() {
                        $(this).parent().parent().remove();
                        multInputs();
                    });

                       function multInputs() {
                           var mult = 0;
                           var sa_popust_uk = 0;

                           var pdv_25 = 0;
                           var pdv_13 = 0;
                           var pdv_5 = 0;
                           // for each row:
                           $("tr.opa").each(function () {
                               // get the values from this row:

                               var $kolicina = $('.kolicina', this).val();
                               var $cijena =   $('.cijena', this).val();
                               var $popust =   $('.popust', this).val();

                               var $total = ($kolicina * 1) * ($cijena * 1);
                               var $total_popus = ($kolicina * 1) * ($cijena * 1) * (($popust * 1)/100);

                                /*Porez*/



                               var value1 =Number($(this).closest('tr.opa').find('td[class=pdv]').text().replace(/[%]/g, ""));

                               if (value1 === 25){

                                    var kaos = ($kolicina * 1) * ($cijena * 1) * (0.25 * 1);
                                    pdv_25 +=kaos;
                               }
                               if (value1 === 13){

                                    var kaos = ($kolicina * 1) * ($cijena * 1) * (0.13 * 1);
                                    pdv_13 +=kaos;
                               }
                               if (value1 === 5){

                                    var kaos = ($kolicina * 1) * ($cijena * 1) * (0.05 * 1);
                                    pdv_5 +=kaos;
                               }


                               // set total for the row
                               //$('.sum').text($total);
                               mult += $total;
                               sa_popust_uk += $total_popus;

                           });
                            $(".popust_all").text(mult-sa_popust_uk.toFixed(2)+' kn');
                            $('.sum').text(mult.toFixed(2)+' kn');
                            $('.popust_count').text(sa_popust_uk.toFixed(2)+' kn');

                           /*Append PDV to page*/
                            $('.pdv_25').text(pdv_25.toFixed(2)+' kn');
                            $('.pdv_13').text(pdv_13.toFixed(2)+' kn');
                            $('.pdv_5').text(pdv_5.toFixed(2)+' kn');

                           $('.sum_od_all').text((mult+pdv_25+pdv_13+pdv_5).toFixed(2)+' kn');

                           console.log('PDV 25:'+ pdv_25+' PDV 13:'+ pdv_13+' PDV 5:'+ pdv_5);

                           //$(".sum_od_all").text(mult);
                       }


            });

If you can have a look and give me some advice on this form calculation.

1
  • I have replaced that now correctly. @Roko C. Buljan Commented May 9, 2015 at 13:45

1 Answer 1

1

I haven't fully understood(first of all what you need to put inside the span values), so this is what I get: JSFIDDLE DEMO

$(".prod_go").on('input','input',multInputs);

function multInputs() {
    var mult = 0;
    var sa_popust_uk = 0;

    $("tr.opa").each(function () {
        var $kolicina = $('.kolicina', this).val(),
            $cijena = $('.cijena', this).val(),
            $popust = $('.popust', this).val(),
            $total = ($kolicina * 1) * ($cijena * 1),
            $total_popus = ($kolicina * 1) * ($cijena * 1) * (($popust * 1) / 100);

        //Forge the class name of the span: <td class="pdv">25%</td> becomes pdv_25
        var $pdv_span = 'pdv_'+($('.pdv', this).text().replace('%','')).trim();
        console.log($pdv_span);

        mult += $total;
        sa_popust_uk += $total_popus;
        //Append value to the relative span value
        $('.'+$pdv_span).text(mult);
    });
    $(".popust_all").text(mult - sa_popust_uk.toFixed(2) + ' kn');
    $('.sum').text(mult.toFixed(2) + ' kn');
    $('.popust_count').text(sa_popust_uk.toFixed(2) + ' kn');

}

Where prod_go is the table class, also I have change $(".prod_go input").keyup(multInputs); to $(".prod_go").on('input','input',multInputs); since it is more reliable as event.

If you need to distinguish between the multi, then: JSFIDDLE

    $(".prod_go").on('input', 'input', multInputs);

function multInputs() {
    var mult = 0,
        mult5 = 0,
        mult13 = 0,
        mult25 = 0,
        sa_popust_uk = 0;

    $("tr.opa").each(function () {
        var $kolicina = $('.kolicina', this).val(),
            $cijena = $('.cijena', this).val(),
            $popust = $('.popust', this).val(),
            $total = ($kolicina * 1) * ($cijena * 1),
            $total_popus = ($kolicina * 1) * ($cijena * 1) * (($popust * 1) / 100);

        //Forge the class name of the span: <td class="pdv">25%</td> becomes pdv_25
        var $pdv_span = ($('.pdv', this).text().replace('%', '')).trim();
        console.log($pdv_span);
        switch ($pdv_span) {
            case '5':
                mult5 += $total;
                $('.pdv_' + $pdv_span).text(mult5);
                break;
            case '13':
                mult13 += $total;
                $('.pdv_' + $pdv_span).text(mult13);
                break;
            case '25':
                mult25 += $total;
                $('.pdv_' + $pdv_span).text(mult25);
                break;
            default:
                break;
        }
        sa_popust_uk += $total_popus;
    });
    mult = mult5 + mult13 + mult25;
    $(".popust_all").text(mult - sa_popust_uk.toFixed(2) + ' kn');
    $('.sum').text(mult.toFixed(2) + ' kn');
    $('.popust_count').text(sa_popust_uk.toFixed(2) + ' kn');

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thak you for your time, I have solved this and i will update my code. Than you @Razorphyn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.