Tuesday, October 5, 2021

PowerApps: Dropdown (Pre select the text OnLoad)

 Hello Friends,

Welcome back with another post on PowerApps. Today, we will see how to show a text as selected in Dropdown. Normally, we bind a dropdown with Display Text and it's relative value (in most cases, an ID). If we save the display text to our data source then there is no issue in binding back on display/edit screen. But, if we are saving the ID of selected item of dropdown to data source then we face issues in binding back on screen. Let's see, how we can do this-

  1. Create a blank PowerApp.
  2. Add a Button-
    1. Name: btnLoadData
    2. Display Text: Load Data
  3. Add a Dropdown-
    1. Name: ddlCountries
  4. Now we need a collection which can be linked with dropdown.
  5. For that, we will use OnSelect property of "btnLoadData".
  6. Add below function on OnSelect property of button-
  7. ClearCollect(
        collCountries,
        {
            ID: 1,
            Name: "India"
        },
        {
            ID: 2,
            Name: "Japan"
        },
        {
            ID: 3,
            Name: "Russia"
        },
        {
            ID: 4,
            Name: "Brazil"
        },
        {
            ID: 5,
            Name: "South Korea"
        },
        {
            ID: 6,
            Name: "Israel"
        }
    )
    

  8. Assign this collection to dropdown Items property.
  9. First part has been completed. where we had make a setup.
  10. Now the actual part comes where we have to show any random text as selected. Let's say we have to show "South Korea" as selected.
  11. Let's add another button-
    1. Name: btnSelectItem
    2. Display Text: Show Selected 
  12. Now update the OnSelect property of this button as below-
  13. UpdateContext({varSelectedID:"South Korea"});
    
  14. It will assign the text to a variable.
  15. Now, we will bind this variable with dropdown Default property.
  16. As you can see, the moment we linked the variable to dropdown, the selected value changed.
  17. As we have to show the default value as selected which was saved in data source, therefore it will work only when the screen loads (because we are using Default property). Once you will change the value of dropdown manually, the purpose of Default gets over.
  18. Now, the main issue: Here we have used text to bind the default property. What if, we were having the ID of item? Then how do we bind this text.
  19. Ha Ha Ha. It's simple. As we have used a collection to bind with dropdown, we will use the same to find the text from ID.
  20. Replace the above code line written on btnSelectItem >> OnSelect property with belwo one-
  21. UpdateContext({varSelectedID:First(Filter(collCountries,ID=3)).Name});
    
  22. Here we are setting Russia as default selected.
  23. You will be surprised that the selected value is still Japan. The reason is the default value in not getting reset.
  24. For this, we will add one more single line function on btnLoadData >> OnSelect.
  25. UpdateContext({varSelectedID:""});
    
  26. It will reset the default selected value of dropdown. Now when we click on "Show Selected" button, Russia will appear as default selected.
  27. Now, play the PowerApp-
  28. Click on Load Data button.
  29. Click on Show Selected button.

  30. This way we can bind the default selected text using value also in dropdown.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.