Coral UI 3 : How to populate option in second selection based on selection change event of first drop-down | Community
Skip to main content
New Participant
February 26, 2018
Solved

Coral UI 3 : How to populate option in second selection based on selection change event of first drop-down

  • February 26, 2018
  • 8 replies
  • 6399 views

I have two drop-down in dialog, based on first dialog select change event I have to populate the value in second drop-down.

Framework : Coral UI 3

AEM version : 6.3 SP1

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by hpathan

We achieved this functionality by Following listener.

1. Add granite:class property to selector radio group and value will be "cq-dialog-dropdown-selector".

2. Add granite:data node under selector node.

3. Add cq-dialog-dropdown-target property to granite:data node and assign some unique css class name that you can give it to target element

4. Add granite:class property to target element and assign that unique class name.

(function (document, $, Coral) {

    "use strict";

    var prevRadioGroupVal;

    var SELECTOR_DROP_DOWN_CLASS = ".cq-dialog-dropdown-selector";

    $( document ).on("foundation-contentloaded", function(e) {

$(SELECTOR_DROP_DOWN_CLASS, e.target).each(function (i, element) {

            var target = $(element).data("cq-dialog-dropdown-target");

            Coral.commons.ready(element, function (component) {

                $(component).on("change",function () {

                   fillData($(component),target);

                });

            });

fillData($(element), target);

        });

    });

    function fillData(component, target) {

        var value = component != null ? component.value : $(target).val();

        if(!value) {

            var tempCheckBoxArray = component.find("[checked]");

            if(tempCheckBoxArray.length > 1)

            {

                for (var i = 0; i < tempCheckBoxArray.length; i++) {

                    if( $(tempCheckBoxArray[i]).val() != prevRadioGroupVal)

                    {

                        value = $(tempCheckBoxArray[i]).val();

                    }

                }

            }

            else

           {

                value = component.find("[checked]").val();

            }

        }

     var prevSelectedVal = $(target).find('coral-select-item').filter(':selected').val();

        $(target).find("coral-select-item").remove();

        for(var i=1;i<=value;i++) {

            if(i==prevSelectedVal)

            {

                $(target).append("<coral-select-item value="+i+" selected>"+i+"</coral-select-item>");

            }

            else

            {

                $(target).append("<coral-select-item value="+i+">"+i+"</coral-select-item>");

            }

        }

        if(value==1)

        {

            $(target).parent().addClass("hide");

        }

        else

        {

            $(target).parent().removeClass("hide");

        }

        prevRadioGroupVal = value;

    }

})(document, Granite.$, Coral);

8 replies

hpathanAuthor
New Participant
March 13, 2018

This is just a example implementation, it will differ as per the requirement.

hpathanAuthorAccepted solution
New Participant
March 13, 2018

We achieved this functionality by Following listener.

1. Add granite:class property to selector radio group and value will be "cq-dialog-dropdown-selector".

2. Add granite:data node under selector node.

3. Add cq-dialog-dropdown-target property to granite:data node and assign some unique css class name that you can give it to target element

4. Add granite:class property to target element and assign that unique class name.

(function (document, $, Coral) {

    "use strict";

    var prevRadioGroupVal;

    var SELECTOR_DROP_DOWN_CLASS = ".cq-dialog-dropdown-selector";

    $( document ).on("foundation-contentloaded", function(e) {

$(SELECTOR_DROP_DOWN_CLASS, e.target).each(function (i, element) {

            var target = $(element).data("cq-dialog-dropdown-target");

            Coral.commons.ready(element, function (component) {

                $(component).on("change",function () {

                   fillData($(component),target);

                });

            });

fillData($(element), target);

        });

    });

    function fillData(component, target) {

        var value = component != null ? component.value : $(target).val();

        if(!value) {

            var tempCheckBoxArray = component.find("[checked]");

            if(tempCheckBoxArray.length > 1)

            {

                for (var i = 0; i < tempCheckBoxArray.length; i++) {

                    if( $(tempCheckBoxArray[i]).val() != prevRadioGroupVal)

                    {

                        value = $(tempCheckBoxArray[i]).val();

                    }

                }

            }

            else

           {

                value = component.find("[checked]").val();

            }

        }

     var prevSelectedVal = $(target).find('coral-select-item').filter(':selected').val();

        $(target).find("coral-select-item").remove();

        for(var i=1;i<=value;i++) {

            if(i==prevSelectedVal)

            {

                $(target).append("<coral-select-item value="+i+" selected>"+i+"</coral-select-item>");

            }

            else

            {

                $(target).append("<coral-select-item value="+i+">"+i+"</coral-select-item>");

            }

        }

        if(value==1)

        {

            $(target).parent().addClass("hide");

        }

        else

        {

            $(target).parent().removeClass("hide");

        }

        prevRadioGroupVal = value;

    }

})(document, Granite.$, Coral);

hpathanAuthor
New Participant
February 26, 2018

Yes, Value is populated in second field. but it is not retaining the value.

smacdonald2008
New Participant
February 26, 2018

Is the 2nd field populated with the value?

hpathanAuthor
New Participant
February 26, 2018

I wrote below listener; data population is working fine but drop-down is not retaining.

(function (document, $, Coral) {

    "use strict";

    var SELECTOR_DROP_DOWN_CLASS = ".cq-dialog-dropdown-selector";

    $( document ).on("foundation-contentloaded", function(e) {

$(SELECTOR_DROP_DOWN_CLASS, e.target).each(function (i, element) {

var target = $(element).data("cq-dialog-dropdown-target");

                Coral.commons.ready(element, function (component) {

                    component.on("change", function () {

                       fillData(component,target);

                    });

                });

        });

        fillData($(".cq-dialog-dropdown-selector", e.target));

    });

    function fillData(component, target) {

        var value = component.value;

        if(!value) {

var options = $(component).find('coral-select-item');

            if(options && options.length > 0) {

value = $(options[0]).val();

            }

        }

$(target).each(function (i, element) {

            console.log(element);

            $(element).find("coral-select-item").remove();

for(var i=1;i<=value;i++)

            {

$(element).append("<coral-select-item value="+i+">"+i+"</coral-select-item>");

            }

        });

      }

})(document, Granite.$, Coral);

smacdonald2008
New Participant
February 26, 2018

That is the correct way to proceed in this use case.

Hemant_arora
New Participant
February 26, 2018

Using the above link, write a listener and use that in your component