AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 15.01.2011, 00:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,459 / 846 (79) +++++++
Регистрация: 28.10.2006
ukcrm: Dependent Option Sets (Continued)
Источник: http://blogs.msdn.com/b/ukcrm/archiv...continued.aspx
==============

Diggin’ On James Brown…

Last month I posted a sample solution which demonstrated how to use some of the capabilities of CRM 2011 (such as JScript libraries, global option sets and the new client-side object model) to develop re-usable, dependent option sets (pick-lists). In that example I had two pick-lists on a form, one containing a list of countries and the other a list of regions (EMEA, APAC, Americas etc.). Whenever a country was selected from the first pick-list, a JScript function would display the correct region in the second pick-list.

But what if I wanted the reverse scenario, where I select a region from one pick-list, and only those countries from that region are visible in the second pick-list?



Like before, the code to achieve the simple case is fairly easy to write, but what if I wanted to build a JScript library that would allow me to re-use my logic, without having to hard-code specific field names? In addition, CRM 2011 introduces the ability for multiple copies of the same field on a single form, so how would my code cope with deal with a field which had multiple controls?

Luckily, CRM 2011 introduces a much richer client programming model than previous versions of CRM, which makes this straightforward. I have two main JScript functions, formOnLoad() and attributeOnChange(), which do some simple error handling before calling updateDependentOptionSet(), which does most of the heavy lifting.

1: formOnLoad(context, attributeName, dependentAttributeName) { 2: 3: if ((this.isValidContext(context)) && (this.isValidOptionSet(attributeName)) && (this.isValidOptionSet(dependentAttributeName))) { 4: this.updateDependentOptionSet(attributeName, dependentAttributeName, true) 5: } 6: else { 7: alert("This function is expecting three parameters. Context, attribute name and dependent attribute name"); 8: } 9: } 10: 11: attributeOnChange(context, dependentAttributeName) { 12: 13: if ((this.isValidContext(context)) && (this.isValidOptionSet(dependentAttributeName))) { 14: var attributeName = context.getEventSource().getName(); 15: this.updateDependentOptionSet(attributeName, dependentAttributeName, false) 16: } 17: else { 18: alert("This function is expecting two parameters. Context and dependent attribute name"); 19: } 20: } 21: 22: updateDependentOptionSet(attributeName, dependentAttributeName, isFormOnLoad) { 23: 24: // Get the attribute 25: var attribute = Xrm.Page.data.entity.attributes.get(attributeName); 26: var attributeValue = attribute.getValue(); 27: 28: // Get the dependent attribute 29: var dependentAttribute = Xrm.Page.data.entity.attributes.get(dependentAttributeName); 30: var dependentAttributeValue = dependentAttribute.getValue(); 31: var dependentAttributeInitialValue = dependentAttribute.getInitialValue(); 32: 33: // There may be more than one control on the form for this dependent attribute, so get an array of these controls 34: var dependentAttributeControls = this.getAttributeControls(dependentAttributeName); 35: 36: // Make sure that we actually have an array with at least one control 37: if ((dependentAttributeControls != "undefined") && (dependentAttributeControls != null)) { 38: 39: // Loop through each control in the array 40: for (var i = 0; i 42: // For each control, first delete all the options 43: dependentAttributeControls[i].clearOptions(); 44: 45: // Now get an array of dependent options 46: var dependentAttributeValues = this.getDependentAttributeValues(attributeValue); 47: 48: // Make sure that we actually have an array with at least one option 49: if ((dependentAttributeValues != "undefined") && (dependentAttributeValues != null) && (dependentAttributeValues.length != 0)) { 50: 51: // Loop through each option in the array, and add each one to the control 52: for (var j = 0; j 54: } 55: } 56: else { 57: dependentAttribute.setValue(null); 58: } 59: } 60: } 61: // If this is the OnLoad event we need to restore the original value of the dependent attribute, since this got wiped out earlier when we called dependentAttributeControls[i].clearOptions() 62: if ((isFormOnLoad) && (dependentAttributeInitialValue != "undefined") && (dependentAttributeInitialValue != null)) { 63: dependentAttribute.setValue(dependentAttributeInitialValue); 64: } 65: }
Using the same option sets from the previous example, all I had to do was to add the country and region fields to a form, and wire up both the form OnLoad() and region field OnChange() events as shown below.





As before, I have selected the “Pass execution context as first parameter” checkbox, and specified the name of the relevant fields (in quotes). If you want to try this out on your own CRM 2011 environment, I have exported my solution as an unmanaged package, which you can download here.

Please note, there is a bug in CRM 2011 Beta (build 05.00.9585.101) build which affects the behaviour of the Xrm.Page.ui.control.clearOptions() method, so this solution will only work if you are using CRM 2011 Release Candidate (build 05.00.9688.34).

This posting is provided "AS IS" with no warranties, and confers no rights.




Источник: http://blogs.msdn.com/b/ukcrm/archiv...continued.aspx
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
ukcrm: CRM 2011 Dependent Option Set Example Blog bot Dynamics CRM: Blogs 0 02.12.2010 19:11
ukcrm: CRM 2011 Videos On Channel 9 (Part 3) Blog bot Dynamics CRM: Blogs 0 09.11.2010 19:05
ukcrm: UKCRM Is Now Tweeting Blog bot Dynamics CRM: Blogs 0 10.07.2009 17:05
Microsoft Dynamics CRM Team Blog: New Dependent Picklist Sample Blog bot Dynamics CRM: Blogs 0 09.06.2009 04:06
Dynamics AX Geek: Useful methods for sets / record2set example Blog bot DAX Blogs 0 28.10.2006 16:40
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 17:00.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.