Alpha Video Training
Results 1 to 6 of 6

Thread: additional javascript affects AJAX callback

  1. #1
    "Certified" Alphaholic Lance Gurd's Avatar
    Real Name
    Lance Gurd
    Join Date
    Jun 2005
    Location
    Southampton, UK
    Posts
    1,433

    Default additional javascript affects AJAX callback

    I have been experimenting with moving records between lists and then saving the data in the target list.

    I got the target list to save by using the SQL_INSERT function in an AJAX callback, which Selwyn recently altered at my request to UPSERT so that existing records are updated and new records are added. This worked brilliantly.

    I then got a little more ambitious and added some preprocessing by Javascript to the records in the target list before calling the callback. Firstly a simple set one of the fields in the list to a value from a textbox on the UX, all records to the same value. Using
    Code:
    var lObj={dialog.object}.getControl('buildup');var ldata=lObj._data.length;
    var qty={dialog.object}.getValue('qty');
    //alert(qty);	
    
    
    for (i=0; i<=ldata; i++){
    	
    	lObj.updateTableRow(i,{OUTPUT:qty});
    }
    {dialog.object}.saveListEdits('buildup',{rows: 'allRows'});
    everything works fine and I am part way to reaching my goal. I then needed to start adding some calculations to the records in the list, so I added the extra code in BLUE below
    Code:
    var lObj={dialog.object}.getControl('buildup');var ldata=lObj._data.length;
    var qty={dialog.object}.getValue('qty');
    //alert(qty);	
    
    
    for (i=0; i<=ldata; i++){
    	
    	lObj.updateTableRow(i,{OUTPUT:qty});
    	var constant=lObj.getData(i).CONSTANT;
    	var waste=lObj.getData(i).A_WASTE;
    	//alert(qty*constant*(1+(waste/100)));
    	lObj.updateTableRow(i,{TOTAL_QTY:qty*constant*(1+(waste/100))});
    	
    }
    {dialog.object}.saveListEdits('buildup',{rows: 'allRows'});
    Although the alert of the calculation works and the records in the list show the TOTAL_QTY as having the correct value for each record the subsequent callback does not fire and therefore the records do not get saved back to the mySQL table.

    I have been playing with this for a few hours now and have drawn a complete blank, can anybody spot what is wrong?

  2. #2
    "Certified" Alphaholic
    Real Name
    Frank Bicknell
    Join Date
    Feb 2010
    Location
    39.1915° N, 84.4348° W
    Posts
    2,467

    Default Re: additional javascript affects AJAX callback

    Just a suggestion. Perhaps this could be better done in a MySQL procedure. Just pass in the data needed for the calculation then let MySQL take care of it.
    Win 10 64 Development, Win 7 64 WAS 11-1, 2, Win 10 64 AA-1,2, MySql, dbForge Studio The Best MySQL GUI Tool IMHO. http://www.devart.com/dbforge/mysql/studio/

  3. #3
    "Certified" Alphaholic Lance Gurd's Avatar
    Real Name
    Lance Gurd
    Join Date
    Jun 2005
    Location
    Southampton, UK
    Posts
    1,433

    Default Re: additional javascript affects AJAX callback

    That's the sort of thing I am coming away from Frank. Used to do it in MySQL and also just all in a callback which resulted in too much server traffic hence trying out the client side. There are further calculations that need to be added of a similar nature to the ones in blue above which, when you have a lot of records can take an age in callbacks etc but Javascript is lightning fast.

  4. #4
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,693

    Default Re: additional javascript affects AJAX callback

    I feel you're doing a bit too much work in the JS code... and it's a bit too complex in general... and specifically for the List control.

    You're including a calculation in the update data. Isn't this producing a visual error in the List row?
    You've got the List control object, and therefore the data, but you're using another function to get specific row data... e.g. .getData()
    Although JS doesn't complain, you're using strings in calculations. I prefer to know specifically what I'm working with.
    You're updating the List twice per loop.
    I believe your loop is wrong... I think it should be i<ldata... not i<=ldata.

    Code:
    var lObj={dialog.object}.getControl('buildup');
    var ldata=lObj._data.length;
    var qty=Number({dialog.object}.getValue('qty'));
    
    for (i=0; i<ldata; i++){
    	
    	var updData = {};
    
    	var constant = Number(lObj._data[i].CONSTANT);
    	var waste = Number(lObj._data[i].A_WASTE);
    	var total = qty*constant*(1+(waste/100));
    
    	updData.OUTPUT = qty;	
    	updData.TOTAL_QTY = total;
    	
    	lObj.updateTableRow(i,updData);
    	
    }
    {dialog.object}.saveListEdits('buildup',{rows: 'allRows'});
    Edit: Corrected _data array
    Last edited by Davidk; 11-23-2017 at 05:58 PM.

  5. #5
    "Certified" Alphaholic Lance Gurd's Avatar
    Real Name
    Lance Gurd
    Join Date
    Jun 2005
    Location
    Southampton, UK
    Posts
    1,433

    Default Re: additional javascript affects AJAX callback

    Thanks David,

    I corrected the loop to i<ldata and my code worked!

    Thinking your code was more eloquent, and would work better for the further calculations, I replaced my code in the loop with yours and it failed. On testing with alert statements, I get nothing after the var constant = Number(lObj._data(i).CONSTANT); line. I then checked the var waste line with the var constant line commented out and this failed as well.

    I see your point about Number() and have added that to each of my var statements.

    Thanks for your help, as always.

  6. #6
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,693

    Default Re: additional javascript affects AJAX callback

    You should learn to use the Chrome debugger in order to walk through your Javascript. It'll save you a lot of trouble... and a lot of guessing.

Similar Threads

  1. The additional data to submit section on a ajax callback
    By Islip in forum Mobile & Browser Applications
    Replies: 2
    Last Post: 11-24-2014, 09:38 AM
  2. ajax callback before action javascript?
    By eskimoavenue in forum Mobile & Browser Applications
    Replies: 11
    Last Post: 05-03-2014, 04:36 PM
  3. Ajax callback "Additional Data to Submit"
    By coleresources in forum Mobile & Browser Applications
    Replies: 4
    Last Post: 02-12-2014, 08:58 PM
  4. Getting a JavaScript variable value from ajax callback?
    By nlights in forum Application Server Version 11 - Web/Browser Applications
    Replies: 8
    Last Post: 02-19-2013, 01:10 PM
  5. proper javascript in ajax callback
    By Jay Talbott in forum Application Server Version 10 - Web/Browser Applications
    Replies: 7
    Last Post: 11-18-2010, 03:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •