[SalesForce] ui:inputDate change event not firing

How do you add a change listener on an input date component that has been changed via the date picker?

Here's my current approach that does not work:

.app file

<aura:application >
    <ui:inputDate aura:id="dateField" label="Birthday" value="2014-01-30" displayDatePicker="true" change="{!c.handleDateChange}"/>
</aura:application>

Controller

({
    handleDateChange : function(component, event, helper) {
        console.log('date change');
    }
})

For contrast, a listener can be set on an input text field with the same approach (the below code works)

.app file

<aura:application >
    <ui:inputText label="Name" change="{!c.handleTextChange}"/>
</aura:application>

Controller

({
    handleTextChange : function(component, event, helper) {
        console.log('text change');
    }
})

Some things that I've noticed while testing:

  • Hitting 'enter' when the cursor is in date input field will cause the page to refresh
  • Hitting 'tab' when the cursor is in the date input field will cause the 'change' event to fire and the controller logs the 'date change' message to the console

Best Answer

You should be able to add the handlers after creation - The reason being is because you don't need to make your aura:id values unique.

If there are multiple components with the same local ID, find() returns an array of the components.

source

So with that in mind, in your iteration, give the input dates all the same id of say... aura:id="repeatedDate" and reference them in a loop on init:

var dateInputs = component.find("repeatedDate");

for (var i = 0; i < dateInputs.length; i++) {
  var dateInput = dateInputs[i];
  if (dateInput) {
    dateInput.addHandler("valueChange", component, "c.handleDateChange");
  }
}

Note - I'm not sure of the exact name for the valueChange event - you'll need to check the docs for this, or even consult the aura github project.


Note, there is another technique that SHOULD work, but doesn't - I'll put it in for reference, as it probably will at some point:

var cmpArr = component.find({ instancesOf : "ui:dateInput" });
for (var i = 0; i < cmpArr.length; i++) {
    var outputCmpArr = cmpArr[i];
    outputCmpArr.addHandler("valueChange", component, "c.handleDateChange");
}

(There are a couple of problems with this technique, one of which is supposed to be fixed by performing a find on the parent of the iterator list first and running a find off that - but I wasn't able to get that to work either)

Related Topic