Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liferay.AutoFields and DatePicker in Liferay

I have the requirement to duplicate a set of input fields on click of a button; as many times as required.

This feature is same like we do have in LIferay:

Goto "Control panel -> User", click on any user.

On right side of the page, under Identification; click on "Addresses, Phone Numbers".

Clicking on PLUS symbol (Add button) duplicates the set of input fields .

Here is the code that I have done for my requirement.

The code for input field:

<input class="date-pick" readonly="readonly" id="<portlet:namespace/>fromDate1"   type="text" onchange="showDate()"

     name="<portlet:namespace/>fromDate1" value="" />" >

The javascript to use the value of date:

function showDate()
  {
   alert(document.getElementById("<portlet:namespace/>fromDate1"));
  }

The jQuery function to bind the datepicker with above text box:

jQuery(function(){
  jQuery('.date-pick').datepicker({autoFocusNextInput: true});
});

The Liferay.Autofields function for creating duplicate row of form fields:

jQuery(
  function () {
        new Liferay.AutoFields(
                    {
                          container: '#<portlet:namespace />webFields',
                          baseRows: '#<portlet:namespace />webFields > .lfr-form-row',
                          fieldIndexes: '<portlet:namespace />formFieldsIndexes',
                          onAdd: function(newField) {
                                      alert('This field got added.');
                                      jQuery('.date-pick').datepicker({autoFocusNextInput: true});
                                },

                          onRemove: function() {
                                      alert('The last field was removed.');
                                }
                    }
        );
  }

);

For the original set of input fields, the datepicker works properly; but for the set of input fields generated after clicking on PLUS symbol (Add button), datepicker doesn’t works.

Also as the name of input fields gets changed dynamically, so I am facing issue in using the values of input fields (see javascript function showDate()).

Have anyone worked on this or have any idea; then please help

like image 477
Tina Agrawal Avatar asked Jan 25 '26 18:01

Tina Agrawal


1 Answers

Got it Fixed. Thanks again to this thread -

Why does jQuery UI's datepicker break with a dynamic DOM?

like image 199
Tina Agrawal Avatar answered Jan 28 '26 19:01

Tina Agrawal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!