This is a very common requirement where when the entity list if complete, you want to interact with the entity list HTML elements and use custom scripts to apply some custom styles depending on some business logic. You may be wondering why this can be so difficult. After all we can write $(document).ready on Web page onload or entity list onload to do the same.
Yes you can actually. But if you register functions using $(document).ready of webpage or entity list, the function will fire even before the entity list component has loaded successfully. So if your function is trying to work with entity list HTML elements, it won’t be available by the time your function executes. And then how it is usually worked out? We end up executing our functions using setTimeout and hope things work out. And again it is kind of hit and miss. Not a 100% success rate.
Then how can we make sure that your function works every time once the entity list have loaded completely?
Instead of attaching event handler at all operations we can achieve it with simple event which is “onloaded” event of entity list in Power Apps Portals. Every entity list control is loaded under div control with identifier as “EntityListControl”.
Now I have the identifier for accessing the entity list. Now open your entity lists and go to Options tab. Under Custom JavaScript section. Paste the below code
$(‘#EntityListControl’).on(“loaded”,function() {
// write your code here.
});
What I am doing here, applying my custom logic to “loaded” event of entity list. This works in all operations and no need to handle them with setTimeout
In case sometimes if you are facing difficulties in accessing the entity list control identifier you can even use the class identifier and apply the same code.
$(‘.entitylist’).on(“loaded”, function() {
//custom logic here
});
Hope this helps!
Debajit Dutta
(Business Applications MVP)
For training and consulting, please reach out to info@xrmforyou.com
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.