If else statement in Barcode scan result

I already make a Barcode scanner, the result after it scan the barcode is in text (example - "page 2").
My problem is how do i want create event on the result.
That result will determine which page the application will display.

Example :

Result Name of Page
"page 1" Home
"page 2" Activity 1
"page 3" Activity 2

So what kind of script do i have to put on the barcode scanner?

*Result is the text result after barcode is scanned
1 person has
this question
+1
Reply
  • Hi Ben,

    If i understand you correctly you can use following JS code:



    //Here is your result.
    var result = "page 2";

    var pageMapping = {
    "page 1": "Home",
    "page 2": "Activity 1",
    "page 3": "Activity 2"
    };

    var goalPage = pageMapping[result];

    console.log("result = " + result);
    console.log("goalPage = " + goalPage);

    Apperyio.navigateTo(goalPage);



    Regards.
    • Thanks for your reply. Really appreciate that.
      Here some idea of what i actually want but don't know how to implement it in apperyio. Hope you can help:

      *After barcode scanner scanned the barcode, its result stored in "Data".
      {
      if (Data=="page1")
      navigateTo(page1);
      else if(Data=="page2")
      navigateTo(page2);
      else
      display ("There is no such data");
      }

      And where do i have to put the JS, is it in the barcodescanner element?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Hi Ben,

    You should use this code where you have data from your barcodescanner. Or rather, you must pass result from scanner to "result" variable.



    //Here you should specify your dynamic Data instead of "page 2".
    //var result = "page 2";

    //This is settings compliance between data stored in "result" variable and page that you need to navigate to.
    var pageMapping = {
    "page 1": "Home",
    "page 2": "Activity 1",
    "page 3": "Activity 2"
    };
    var goalPage = pageMapping[result];

    console.log("result = " + result);
    console.log("goalPage = " + goalPage);

    if(goalPage)
    Apperyio.navigateTo(goalPage)
    else
    alert("There is no such data");



    Also you can see some intermediate debug data. Read more about debugging here: http://devcenter.appery.io/documentat...

    Regards.
    • Sorry if i am asking too much sir.. I already understand the rest of the code u mentioned, and I thankful for that. But.. i am a bit confused with the 1st code line.. the result you initialized as "page 2". How to make result be initialized directly with value from barcode scanner..

      1) How do I pass the result from scanner to result variable?
      2) And you stated there I need to specify dynamic data instead "page 2". How can I specify that dynamic data? I already created new storage variable(local storage) and i named it DATA. is that dynamic data that you mentioned? If yes, instead of you initialized value result as "page 2", should I do like this:

      var result = "DATA" or var result = DATA

      Thank you sir. Much appreciate.
    • I did this, i already created variable in local storage named DATA. result of barcode scanner will mapped on DATA . Then I put this JS code on that DATA :

      var pageMapping = {
      "page 1": "Home",
      "page 2": "Activity 1",
      "page 3": "Activity 2"
      };
      var goalPage = pageMapping[DATA];
      console.log("result = " + DATA);
      console.log("goalPage = " + goalPage);
      if(goalPage)
      Apperyio.navigateTo(goalPage);
      else
      alert("There is no such data");

      Still it didnt navigate.. (Below is the image how i map the data of barcode scanner)
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Hi Ben,

    Okay, you have stored result into the "DATA" storage.

    In this case code should be:



    //Where "DATA" is your storage name.
    var result = Apperyio.storage.DATA.get();

    //This is settings compliance between data stored in "result" variable and page that you need to navigate to.
    var pageMapping = {
    "page 1": "Home",
    "page 2": "Activity 1",
    "page 3": "Activity 2"
    };
    var goalPage = pageMapping[result];
    console.log("result = " + result);
    console.log("goalPage = " + goalPage);
    if(goalPage)
    Apperyio.navigateTo(goalPage)
    else
    alert("There is no such data");



    Regards.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Okay, my mistake wrongly put JS. Thank you sir. Problem solved.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated