I have have a function in server module which deals with employees time basically takes two datetime objects as parameters and returns total_working_time as int. When I call this function manually and print its output on form load it works find. However its not working with date picker ‘change’ event.
I have two date picker fields and I want to feed the selected-dates to my function and show their deifference in a text field. Here is the little code I am using
def date_picker_end_time_change(self, **event_args):
"""This method is called when the selected date changes"""
self.text_box_total_time.text = anvil.server.call('calc_working_time',self.date_picker_start_time.date,self.date_picker_end_time.date)
def date_picker_start_time_change(self, **event_args):
"""This method is called when the selected date changes"""
self.text_box_total_time.text = anvil.server.call('calc_working_time',self.date_picker_start_time.date,self.date_picker_end_time.date)
Nothing. The return value of my function never shows up in text field. Actually the same issue is with dropdown box too. The change event doesn’t make a call to server. Here is the code.
def drop_down_wo_no_change(self, **event_args):
"""This method is called when an item is selected"""
self.text_box_product.text = anvil.server.call('get_product_in_wo',self.drop_down_wo_no.selected_value)
Here is the server function its calling (The function makes a call to mongodb and returns a particular item in collection).
Step 1 would be to verify that the change-event handler is actually being called. Try inserting a distinctive print function call, one line above the assignment statement. If the handler is being called, you should see the effect of that print in the IDE’s Output window.
@p.colbert Brilliant. It just didn’t occur to me whether event is being called or not. Anyway “Change” event was not being called. And I found the culprit. I am using form_checker package as dependency for validation purposes. In case of drop down I mentioned above the validation code is like this
self.validator.require(self.drop_down_wo_no,['change'],
lambda wo_no:wo_no.selected_value is not None)
The moment I removed the above code ‘Change’ event started to work. Although its weird, validation library is not supposed to interfere with a component’s basic functionality. Thanks a lot.
When something doesn’t work the way we expect, then usually one of our assumptions is broken. Some of those assumptions can be so “obvious” and subtle, it can be a challenge to recognize which assumptions we’re actually making. But once we do, we can check them, from first to last.