Jakub
Posts: 0
Joined: Fri Dec 05, 2014 10:53 pm

Check checkbox in list

I have a list of items populated from the REST service.
User adds items into the "shopping cart" by checking clicking the list item.
Shopping cart is stored in local array.
There is a check box in the list item to indicate if the item is added or not.
User can switch "categories" which triggers new REST service call and refresh of the list item.
I want to be able to populate checkboxes of items in the shopping cart when user returns to the original category.
This is the code I have in Mapping of the REST service on the Checkbox Checked property

var uniQID=value;

try {

Code: Select all

  // Get cart 
  var cart = Apperyio.storage.cart.get() || []; 

     //Loop through all cart items 
     for (var i = 0; i < cart.length; i++) 
     { 
         //Search for the same item id 
         if ((cart[i].IdUniQ == uniQID) ) 
         { 
            return 1;                
         } else { 
            return 0; 
         } 
     } 

}
catch (error) {
alert("Something went wrong: ", error);

}

Only the first checked item gets retained when I return to original category. what am I missing?
Or is this a wrong approach?

Thanks

Image

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Check checkbox in list

Hi Jakub,

Please try to debug this code with logs to console:

pre

var uniQID = value;

console&#46;log("uniQID = " + uniQID);

try {

Code: Select all

 &#47;&#47; Get cart  
 var cart = Apperyio&#46;storage&#46;cart&#46;get() || []; 

 &#47;&#47;Loop through all cart items  
 for (var i = 0; i < cart&#46;length; i++) { 

     console&#46;log(cart[i]&#46;IdUniQ  + " = " +  uniQID); 

     &#47;&#47;Search for the same item id  
     if ((cart[i]&#46;IdUniQ == uniQID)) { 
         return true; 
     } else { 
         return false; 
     } 
 } 

} catch (error) {
alert("Something went wrong: ", error);

}

/pre

Regards.

Jakub
Posts: 0
Joined: Fri Dec 05, 2014 10:53 pm

Check checkbox in list

The Debug code shows that the JS is not cycling through the cart.
The cart.length is let's say 3, but variable "I" is always 0
??

Jakub
Posts: 0
Joined: Fri Dec 05, 2014 10:53 pm

Check checkbox in list

issue resolved. It was "user error"
I forgot that the return directive in the code aborts the loop

Here is a working code with the debug info

var uniQID = value;

var chkD=0;

console.log("uniQID = " + uniQID);

try {
// Get cart
var cart = Apperyio.storage.cart.get() || "Nothing";

Code: Select all

 if (cart!=="Nothing") { 

 //Loop through all cart items  
          console.log("CartCnt = " + cart.length); 

 for (i = 0; i < cart.length; i++) { 

console.log("i = " + i);

Code: Select all

     var currItem = cart[i]; 

     console.log(currItem.IdUniQ  + " = " +  uniQID); 

     //Search for the same item id  

     if ((currItem.IdUniQ === uniQID)) { 

         chkD = 1; 
         break; 

     } else { 

         chkD = 0; 

     } 
 } 
     return chkD; 

 } else { 

    return 0; 

 } 

} catch (error) {

Code: Select all

 alert("Something went wrong: ", error); 

return 0;

}

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Check checkbox in list

Hi Jakub -

Do you need any further help with Appery.io?

Jakub
Posts: 0
Joined: Fri Dec 05, 2014 10:53 pm

Check checkbox in list

yes.
After getting everything to work fine in Chrome on my PC I tested the app on iPhone and Android phone only to find out that on these devices the Click events do not fire when attached to the checkbox in the List. The check box gets checked but none of the underlying code (Run JavaScript, Mapping, set Storage variable, Set property) actually runs.
What is the proper procedure to handle check boxes in the list?
My task is simple.
List items and user adds them to a "shopping cart" (local array) by clicking on selected item(s). Checkbox indicates items in the shopping cart. User can select from several categories (list is refreshed from REST service) so checkboxes need to be re-checked for items already in the cart.

What is proper sequence of events? (as I said above, I have it working in the browser on the PC, but not on the phones)

Thanks

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Check checkbox in list

Hi,

Try to use "Virtual Click" or "Tap" instead of click.
Also you can place same event sequence for each checkbox for "value change" event.

EJLD
Posts: 0
Joined: Sun Jun 08, 2014 11:03 pm

Check checkbox in list

helpful.
value change works for me.
thks

Return to “Issues”