Add a item (with default values) to the invoice

67 views
Skip to first unread message

Dora Ruggiero

unread,
Oct 13, 2020, 4:49:33 AM10/13/20
to KnockoutJS

Hi,
i want add a new line (with default values) to the invoice.
I succeeded but the line-total is not updated.

My Button
<div><a class="pointer" onClick="invoice.addItem();invoice.addStamp();">{{ trans('texts.add_stamp') }}</a></div>

My function

self.addStamp = function() { 
 var lastRow = self.invoice_items_without_tasks().length-1; $('textarea[name="invoice_items['+lastRow+'][notes]"').text('{{ env('STAMP_DESC') }}'); $('input[name="invoice_items['+lastRow+'][cost]"').val('{{ env('STAMP') }}'); $('input[name="invoice_items['+lastRow+'][cost]"').closest('tr').find('select[name="itemTaxes1"]').val('{{ env('STAMP_TAX') }}'); onItemChange(); refreshPDF(true); }

Help me.

SLG

unread,
Oct 19, 2020, 10:33:40 AM10/19/20
to KnockoutJS
I think you mixed view model with direct DOM manimupaltion.

You business logic should be in the view model. You viewmodel should rarely use any of the jquery $ selector. Knockout will handle the DOM elements based on your view model.

The code should be something like this.

function Line(){
   var self=this;
   self.name=ko.observable();
   self.price=ko.observable();
}

function Invoice(){
    var self=this;
    
  self.lines=ko.observableArray()

   self.total=ko.computed(function(){
         var total=0;
         
        for(var i=0;i<self.lines().length;i++)
        {
            //If the price is null value, treat as 0 value
            total+=(self.lines()[i].price()||0);
        }

      return total;
   })

     self.add=function(){
         var newLine=new Line();
         self.lines.push(newLine);

Dora Ruggiero

unread,
Nov 2, 2020, 6:58:33 AM11/2/20
to knock...@googlegroups.com
Thank you.
Resolved.

--
You received this message because you are subscribed to a topic in the Google Groups "KnockoutJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/knockoutjs/akd1piZoqf0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to knockoutjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/knockoutjs/f03423eb-877f-4ff3-a649-f4bbcdcf5284n%40googlegroups.com.


--
Dora Ruggiero
Web Developer Freelance

Reply all
Reply to author
Forward
0 new messages