Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <HTML xmlns="http://www.w3.org/1999/xhtml">
- <HEAD>
- <TITLE></TITLE>
- <META charset=utf-8></META>
- </HEAD>
- <style>
- label, select {font-size: 11px;}
- </style>
- <BODY style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; BORDER-TOP: 0px; BORDER-RIGHT: 0px BACKGROUND-COLOR: #ffffff; MARGIN: 0px; FONT-FAMILY: Segoe UI; FONT-SIZE: 11px;">
- <!--
- This section contains the two "select" DOM elements in the web
- resource that will be used to enforce the cascading option set
- functionality.
- This template uses "new_division" and "new_state" as example fields,
- if you find and replace all the values with your relevant CRM
- optionset attribute names everything will work.
- Also the labels in this section, "First" and "Second" should be
- updated to show the desired labels.
- You will need to construct a json string to store the cascading
- logic. It is required in the $('#cascade_new_division').change()
- function.
- -->
- <div id="f1">
- <table>
- <tr>
- <td>
- <label for="cascade_new_division">Division</label>
- </td>
- <td>
- <select id="cascade_new_division">
- <option value="null"></option>
- </select>
- </td>
- </tr>
- <tr>
- <td>
- <label for="cascade_new_state">State</label>
- </td>
- <td>
- <select id="cascade_new_state">
- <option value="null"></option>
- </select>
- </td>
- </tr>
- </table>
- </div>
- <!-- ******************** -->
- <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
- <script>
- /*
- This function will fire when the web resource loads and it will
- initialize the values and set the actual crm fields to be not
- visible.
- */
- $(document).ready(function () {
- //Load cascading options set with the CRM optionsetvalues
- var options = parent.Xrm.Page.getAttribute("new_division").getOptions();
- for(i = 0; i < options.length; i++)
- {
- if(options[i].value != "null"){
- $("#cascade_new_division").append($('<option>', {
- value: options[i].value,
- text: options[i].text
- }));
- }
- }
- //Set the cascading values to match the selected values
- var first_selection = parent.Xrm.Page.getAttribute("new_division").getValue();
- var second_selection = parent.Xrm.Page.getAttribute("new_state").getValue();
- if(first_selection == "null" || first_selection == null)
- {
- $('#cascade_new_division').val("null");
- }
- else
- {
- $('#cascade_new_division').val(first_selection);
- //call the change function on the first cascading optionset
- $('#cascade_new_division').change();
- }
- if(second_selection == "null" || second_selection == null)
- {
- //disable the second optionset value
- $('#cascade_new_state').attr("disabled", "disabled");
- }
- else
- {
- $('#cascade_new_state').val(second_selection);
- }
- //hide the crm optionsets on the crm form
- parent.Xrm.Page.ui.controls.get("new_division").setVisible(false);
- parent.Xrm.Page.ui.controls.get("new_state").setVisible(false);
- });
- /*
- This function will fire when the first optionset is changed. It
- will use the json string to select the potential values for the
- second optionset and load the values. It will also copy the
- selected value of the first selection to the crm field and clear
- the second crm field to ensure that the cascading logic is
- respected.
- */
- $('#cascade_new_division').change(function () {
- $('#cascade_new_state').removeAttr("disabled");
- $("#cascade_new_state option[value!='null']").remove();
- var selected = $(this).val();
- parent.Xrm.Page.getAttribute("new_division").setValue(selected);
- parent.Xrm.Page.getAttribute("new_state").setValue(null);
- if (selected == "null" || selected == null) {
- $('#cascade_new_state').attr("disabled", "disabled");
- return;
- }
- //json string that is used to store the cascading logic.
- var obj = jQuery.parseJSON('{"cascade":[[100000000,[100000006,100000018,100000020,100000028,100000038,100000044]],[100000001,[100000029,100000031,100000037]],[100000002,[100000012,100000013,100000021,100000034,100000049]],[100000003,[100000014,100000015,100000022,100000024,100000026,100000033,100000040]],[100000004,[100000007,100000008,100000009,100000019,100000032,100000039,100000045,100000047,100000048]],[100000005,[100000000,100000016,100000023,100000041]],[100000006,[100000003,100000017,100000035,100000042]],[100000007,[100000002,100000005,100000011,100000025,100000027,100000030,100000043,100000050]],[100000008,[100000001,100000004,100000010,100000036,100000046]]]}');
- var possible = obj.cascade;
- var cascadedOptions;
- for(var i = 0, l = possible.length; i < l; i++){
- if (possible[i][0] == selected){
- cascadedOptions = possible[i][1];
- }
- }
- for (var i = 0, l = cascadedOptions.length; i < l; i++) {
- $("#cascade_new_state").append($('<option>', {
- value: cascadedOptions[i],
- text: parent.Xrm.Page.getAttribute("new_state").getOption(cascadedOptions[i]).text
- }));
- }
- });
- /*
- This function will fire when the second option set is changed.
- It will copy the value to the related crm field.
- */
- $('#cascade_new_state').change(function () {
- var selected = $(this).val();
- parent.Xrm.Page.getAttribute("new_state").setValue(selected);
- });
- </script>
- </BODY>
- </HTML>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement