0

Lightswitch HTML Client

After a query has been executed, I'm curious how to perform changes on the client immediately after. For example, i have a search box (textbox) that takes the input and passes it along to a parameterized query. On the client, i would like to set the focus back to the search box after the query has finished. I have working java script code to set the foucs, i just don't know when and where to use it. I need something like the server methods _Executed and _Executing but on the client. Is this possible?

HiTech
  • 913
  • 1
  • 16
  • 34

1 Answers1

1

I don't think you'll know when the query has completed, since it's done asynchronously, and by default only loads a portion of the results. Have you tried putting the javascript to set the focus in the postRender of the control associated with the search box? You can find it in the designer for the screen by selecting the control, and selecting the drop down next to Write Code.

Don't forget to use the setTimeout method to ensure the element is properly rendered before your javascript is applied. For a proper explanation refer to Why is setTimeout(fn, 0) sometimes useful?.

I used the following and it shifted focus to the button:

myapp.BrowseMyEntity.MyButton_postRender = function (element, contentItem) {
    setTimeout(function () {
        element.focus();
    }, 0);
};

If you really want to set focus after you've deliberately called a query and it has completed, use the promise mechanism ".then(...)", for example:

screen.details.dataWorkspace.ApplicationData.MyQuery().execute().then(function (result) {
    // Do whatever you want here
    }
});
Community
  • 1
  • 1
BobbyJ
  • 371
  • 2
  • 9
  • I already know how to set the focus during the post render. I need to be able to do this after the query has executed. I guess another approach would be to set the focus from the server... Except i have no clue how to do that either. – HiTech Feb 10 '16 at 22:17
  • If you're on the HTML client, you won't be able to set focus from the server. Can you explain why you need to set the focus only after the query is done? Does it disturb the focus or cause the search box to perform an action? – BobbyJ Feb 10 '16 at 22:21
  • Check the answer, I added some more information on performing an action when a query has completed execution. – BobbyJ Feb 10 '16 at 22:33