This seems basic but I can\'t seem to figure out how to manually set the current item to work with from the datasource?
To illustrate: I have a table and I notice th
Why it happens:
AM code: generate button click event
User code: handle button's click event
User code: navigate user to different page
AM code: destroy DOM of current page
AM code: build DOM for new page
-- dead code after this line
AM code: row click event handler
AM code: change datasource's current item
Row's click event handler never gets control, because row is destroyed by user's code.
What Morfinismo's solution does?
AM code: generate button click event
User code: handle button's click event
AM code: row click event handler
AM code: change datasource's current item
-- moved lines
User code: navigate user to different page
AM code: destroy DOM of current page
AM code: build DOM for new page
Here are more tech details: Event Loop
In App Maker this problem can be solved with
// button's onClick event handler
app.datasource.ListDatasource.selectKey(widget.datasource.item._key);
// button's onClick event handler
app.pages.ShowMeNext.properties.Key = widget.datasource.item._key;
app.showPage(app.pages.ShowMeNext);
// next page's onAttach event handler
app.datasources.RecordDatasource.filters._key._equals = app.currentPage.properties.Key;
app.datasources.RecordDatasource.load();
// button's onClick event handler
var params = {
key: widget.datasource.item._key
};
var page = app.pages.ShowMeNext;
app.showPage(page);
google.script.history.replace(null, params, page.name);
// next page's onAttach event handler
google.script.url.getLocation(function(location) {
app.datasources.RecordDatasource.filters._key._equals = location.parameters.key;
});
// button's onClick event handler
window.key = widget.datasource.item._key;
// next page's onAttach event handler
app.datasources.RecordDatasource.filters._key._equals = window.key;
ListDatasource - list/grid/table datasource
RecordDatasource - datasource dedicated for a specific record (single-record datasource)
Use a timeout function. This happens because it takes some time for appmaker to change the item in the datasource. You can use something like this on the onClick event handler of the button or link that will take you to the other page:
setTimeout(function(){
app.showPage(app.pages.pageToNavigate);
},200);
That should take care of the issue. I hope this helps!