Page 1 of 1

Check checkbox in list

Posted: Tue Jan 20, 2015 10:20 pm
by Jakub

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


Check checkbox in list

Posted: Wed Jan 21, 2015 2:08 am
by Yurii Orishchuk

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.


Check checkbox in list

Posted: Wed Jan 21, 2015 6:35 pm
by Jakub

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
??


Check checkbox in list

Posted: Wed Jan 21, 2015 7:43 pm
by Jakub

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;

}


Check checkbox in list

Posted: Thu Jan 22, 2015 12:42 pm
by Illya Stepanov

Hi Jakub -

Do you need any further help with Appery.io?


Check checkbox in list

Posted: Thu Jan 22, 2015 7:30 pm
by Jakub

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


Check checkbox in list

Posted: Tue Jan 27, 2015 9:14 pm
by Alena Prykhodko

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.


Check checkbox in list

Posted: Sun Apr 26, 2015 9:51 pm
by EJLD

helpful.
value change works for me.
thks