This is a public Forum  publicRSS

Topic

    Bruno Aun
    Custom Widget - How to dynamically refer to YES/NO custom...Answered
    Topic posted August 7, 2017 by Bruno AunJourneyer 
    74 Views, 4 Comments
    Title:
    Custom Widget - How to dynamically refer to YES/NO custom Field in JS
    Content:

    Hi there. 

    My goal here is to replace the pseudocode below with the actual dynamic field IDs in the LOGIC.JS file
    I need to call different functions for each of the options ( _1 and _0 ) which are dynamically generated (see below)

     constructor: function() {
            this.Y.one("REPLACE WITH PARTIAL DYNAMIC FIELD NAME" + "_1']").on('click',this.function_required, this);
            this.Y.one("REPLACE WITH PARTIAL DYNAMIC FIELD NAME" + "_0']").on('click',this.function_notrequired, this);
        },
     
    VIEW.PHP
    <div id="rn_<?= $this->instanceID ?>" class="<?= $this->classList ?>">
        <rn:widget path="input/FormInput" name="Incident.CustomFields.Bruno.isSoleCustody" required="false"  />
     
    RENDERS LIKE SO ON THE BROWSER
    <input type="radio" name="Incident.CustomFields.Bruno.isSoleCustody" id="rn_SelectionInput_27_Incident.CustomFields.Bruno.isSoleCustody_1" value="1"> .....
     
    <input type="radio" name="Incident.CustomFields.Bruno.isSoleCustody" id="rn_SelectionInput_27_Incident.CustomFields.Bruno.isSoleCustody_0" checked="checked" value="0"> ......
     
    Any help would be greatly appreciated
    Thank you 
    Bruno

    Best Answer

    Bruno Aun

    Here's the solution

    constructor: function() {
       
            this.Y.one("[id*='" + this.data.attrs.field + "_1']").on('click',this._required, this);
            this.Y.one("[id*='" + this.data.attrs.field + "_0']").on('click',this._notrequired, this);
     
        },
     
    THANKS

    Answer

     

    • Pramod V

      Well you could do this in two ways:

      • Using Jquery - but if you use this method it wont support in IE browser some versions.
      • Using Javascript: Follow these steps:

      ================================================================================================

      • Create a custom widget from the scratch and logic.js constructor declare a listener event:
      constructor: function()
      {
      this.initialization();
      },
       
      initialization: function()
      {
      //document.getElementsByName('Incident.CustomFields.Bruno.isSoleCustody')[i].addEventListener("click",fieldHaveValue);
      document.getElementsByName('Incident.CustomFields.Bruno.isSoleCustody')[i].addEventListener("checked",fieldHaveValue);
      },
       
      fieldHaveValue: function()
      {
      var returnValue = "";
      var field = document.getElementsByName('Incident.CustomFields.Bruno.isSoleCustody')[i];
       
      if(field != null)
      {
      for(var i = 0;i < field.length;i++)
      {
      if(field[i].checked)
      {
      returnValue = field[i].value;
       
      if(returnValue == "1")
      {
      returnValue = "yes";
      }
      else if(returnValue == "0")
      {
      returnValue = "no";
      break;
      }
      }
      }
      }
      return returnValue;
      },
       
      ~VIP
    • Bruno Aun

      Thank you Pramod V

      However i'm getting an error. [i] is not defined

      document.getElementsByName('Incident.CustomFields.Bruno.isSoleCustody')[i].addEventListener("checked",fieldHaveValue);

       

    • Bruno Aun

      Here's the solution

      constructor: function() {
         
              this.Y.one("[id*='" + this.data.attrs.field + "_1']").on('click',this._required, this);
              this.Y.one("[id*='" + this.data.attrs.field + "_0']").on('click',this._notrequired, this);
       
          },
       
      THANKS
    • Danette Beal

      Bruno, 

      You are the best! Thank you for coming back and updating with the solution!

      yes

      Danette, Community Manager