2012-02-03 41 views
2

我有一个发票模型和一个LineItem模型。我可以通过将LineItem价格乘以数量来计算线路总数。现在我必须加税。Rails 3.如何使用jQuery将订单项添加到订单项?

数量和价格从lineitem选择菜单的数据属性中获得。现在,我如何将税额加到计算中?

这是我......

<tr class="item line_item_row"> 
    <td style="border:0;"><%= link_to_remove_fields "X", f %></td> 
    <td class="item_name_data first_column"> 
    <!-- the line_item_row class name is used in the jQuery function remove_fields() --> 

    <%= f.select(:item_id, options_for_select(@items.map{|c| [c.name, c.id, {'data-defaultquantity'=>1,'data-price'=>c.price, 'data-description'=>c.description}]}, f.object.item_id), {:prompt => ''}, {:class=>'product', :style => 'width:105px;'}) %> 

    </td> 
    <td class="description"> 
    <%= f.text_area :description, :rows => 1, :value => f.object.description, :class => 'description' %> 
    </td> 

    <td class="price_data"> 
    <%= f.text_field :price, :size => 6, :value => (number_with_precision(f.object.price,:precision => 2)||0), :class => 'price' %> 
    </td> 

    <td class="quantity_data"> 
    <%= f.text_field :quantity, :size => 4, :class => 'qty' %> 
    </td> 

    <td class="tax_data"> 
    <%= f.collection_select 'tax_ids', Tax.all, :id, :name, {:name => 'line_item[tax_ids][]', :prompt => ''} %> 
    </td> 

    <td class="lineitemtotal linetotal_data" style="text-align:right;"> 
    </td> 
</tr> 

这是我的javascript

function remove_fields(link){ 
    $(link).prev("input[type=hidden]").val("1"); 
    $(link).closest(".line_item_row").hide(); 
} 

function add_fields(link, association, content) { 
    var new_id = new Date().getTime(); 
    var regexp = new RegExp("new_" + association, "g") 
    // $(link).parent().before(content.replace(regexp, new_id)); 
    // $('#invoice > tbody:last').append(content.replace(regexp, new_id)); 
    $('.add_a_line_row').before(content.replace(regexp, new_id)); 
} 

// get invoce grand total 
function getTotal(lines) { 
    var total = 0; 
    $.each(lines, function(){ 
    total += parseFloat($(this).html()); 
    }); 
    $('#total-price').html('$' + total + ' MXN'); 
} 

function getLineItemTotals(lines){ 
    $.each(lines, function(){ 
    row_total = 0; 
    var price = parseFloat($(this).find('.price').val()); // get price 
    var qty = parseFloat($(this).find('.qty').val()); // get quantity 
    /* Check if quantity or price are empty */ 
    if(!qty) qty = '0'; 
    if(!price) price = '0'; 
    row_total = price * qty; // row_total = parseFloat($(this).find('.price').val()) * parseFloat($(this).find('.qty').val()); 
    // $($(this).find('.lineitemtotal')).html(roundNumber(row_total,2)); 
    $($(this).find('.lineitemtotal')).html(row_total.toFixed(2)); 
    }) 
} 

function roundNumber(num,dec){ 
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); 
    return result; 
} 

// new items will be populated with default data 
// http://stackoverflow.com/questions/8524126/function-stops-working-on-new-items 
$(document).on("blur", ".product", function(){ 
    var optionElem = $(this).find(":selected")[0]; // <option value="3" data-defaultQuantity="1">product name</option> 
    $(this).closest('.item').find('.qty').val(optionElem.dataset.defaultquantity); // replace quantity 
    $(this).closest('.item').find('.price').val(optionElem.dataset.price); // replace price 
    $(this).closest('.item').find('.description').val(optionElem.dataset.description); // replace price 

    var line = $('.item'); 
    getLineItemTotals(line); 
    var line_totals = $('.lineitemtotal'); 
    getTotal(line_totals); 
    $('#total-price').effect('highlight',{},3000); 
}); 

$(document).on("blur", ".price", function(){ 
    // price change 
    var price = $('.price'); 
    price.blur(function(){ 
    var line = $('.item'); 
    getLineItemTotals(line); 
    var line_totals = $('.lineitemtotal'); 
    getTotal(line_totals); 
    $('#total-price').effect('highlight',{},3000); 
    }); 
}); 

$(document).on("blur", ".qty", function(){ 
    // price change 
    var qty = $('.qty'); 
    qty.blur(function(){ 
    var line = $('.item'); 
    getLineItemTotals(line); 
    var line_totals = $('.lineitemtotal'); 
    getTotal(line_totals); 
    $('#total-price').effect('highlight',{},3000); 
    }); 
}); 

$(document).ready(function() { 

    var line = $('.item'); 
    // so the line totals are calculated on page load 
    getLineItemTotals(line); 

    var line_totals = $('.lineitemtotal'); 
    getTotal(line_totals); // So the total is calculated on page load. 

}); 
+0

你可以发布源自查看源码的convereted html吗 – DG3 2012-02-03 15:52:04

+0

你最终找到了答案吗?我面临着类似的问题,并且会很好奇你是如何解决它的。 – Tintin81 2012-09-21 08:57:51

回答

0

要获得一个选择框所选项目的价值,它是:

$("#your_select_box option:selected").text(); OR $("#your_select_box option:selected").val();,具体取决于您是否想要选项的value=""或选项中的文本。

所以你要看一看的ID Rails正在分配您的选择框(或只是通过$(".tax_data select option:selected")引用它,并在您getLineItemTotals功能,增加了取景器和您的税值添加到row_total。不要忘记你的parseFloat