下拉列表联动处理(Python实现)原创
18人赞赏了该文章
4,179次浏览
编辑于2021年06月19日 10:00:31
先说下需求:
# 设置了两个下拉列表,怎样做到一个下拉列表控制另一个下拉列表呢?
# 例如第一个下拉列表内容为ABCD,第二个下拉列表为A1、A2、A3、B1、B2、B3、C1、C2、C3、D1、D2、D3,怎样做到第一个下拉列表选择A的时候,再选第二个下拉列表只显示A1、A2、A3呢?
实现思路:
1、重写OnInitialize事件,动态绑定两个下拉框的数据
2、重写DataChanged事件,根据第一个下拉框的选中值,动态加载填充第二个下拉框的值
import clr clr.AddReference("Kingdee.BOS") from Kingdee.BOS import LocaleValue from Kingdee.BOS.Core.Metadata import EnumItem from Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel import ComboFieldEditor from System.Collections.Generic import * from System import StringComparison def OnInitialize(e): array1 = ["A", "B", "C", "D"] newList = List[EnumItem]() seq = 0 for item in array1: newItem = EnumItem() newItem.Seq = seq newItem.Value = item newItem.Caption = LocaleValue(item, this.View.Context.UserLocale.LCID) newList.Add(newItem) seq = seq + 1 #获取fieldEditor来设置下拉的选项列表 fieldEditor = this.View.GetControl("FCombo1") fieldEditor.SetComboItems(newList) newList2 = List[EnumItem]() seq = 0 dict = {'A': ['A1', 'A2', 'A3'], 'B': ['B1', 'B2', 'B3'], 'C': ['C1', 'C2', 'C3'], 'D': ['D1', 'D2', 'D3']} for item in dict['A']: newItem = EnumItem() newItem.Seq = seq newItem.Value = item newItem.Caption = LocaleValue(item, this.View.Context.UserLocale.LCID) newList2.Add(newItem) seq = seq + 1 fieldEditor2 = this.View.GetControl("FCombo2") fieldEditor2.SetComboItems(newList2) def DataChanged(e): if e.Field.Key.Equals("FCOMBO1", StringComparison.OrdinalIgnoreCase): combo1Value = this.View.Model.GetValue("FCombo1").ToString() dict = {'A': ['A1', 'A2', 'A3'], 'B': ['B1', 'B2', 'B3'], 'C': ['C1', 'C2', 'C3'], 'D': ['D1', 'D2', 'D3']} array1 = dict[combo1Value] if len(array1) > 0: newList = List[EnumItem]() seq = 0 for item in array1: newItem = EnumItem() newItem.Seq = seq newItem.Value = item newItem.Caption = LocaleValue(item, this.View.Context.UserLocale.LCID) newList.Add(newItem) seq = seq + 1 fieldEditor2 = this.View.GetControl("FCombo2") fieldEditor2.SetComboItems(newList)
public override void OnInitialize(InitializeEventArgs e) { base.OnInitialize(e); //新的枚举列表 List<EnumItem> newList = new List<EnumItem>(); string[] array = { "A", "B", "C", "D" }; for (var i = 0; i < array.Length; i++) { newList.Add(new EnumItem { Seq = i, Value = array[i], Caption = new LocaleValue(array[i], this.Context.UserLocale.LCID) }); } var dic = new Dictionary<string, List<string>> { { "A", new List<string>() { "A1", "A2", "A3"} }, { "B", new List<string>() { "B1", "B2", "B3"} }, { "C", new List<string>() { "C1", "C2", "C3"} }, { "D", new List<string>() { "D1", "D2", "D3"} } }; List<EnumItem> FCombo2 = new List<EnumItem>(); int index = 0; foreach (var item in dic["A"]) { FCombo2.Add(new EnumItem { Seq = index, Value = item, Caption = new LocaleValue(item, this.Context.UserLocale.LCID) }); index++; } //获取fieldEditor来设置下拉的选项列表 ComboFieldEditor fieldEditor = this.View.GetControl<ComboFieldEditor>("FCombo1"); fieldEditor.SetComboItems(newList); ComboFieldEditor fieldEditor2 = this.View.GetControl<ComboFieldEditor>("FCombo2"); fieldEditor2.SetComboItems(FCombo2); } public override void DataChanged(BOS.Core.DynamicForm.PlugIn.Args.DataChangedEventArgs e) { switch (e.Field.Key.ToUpper()) { case "FCOMBO1": var combo1Value = this.View.Model.GetValue("FCombo1").ToString(); var dic = new Dictionary<string, List<string>> { { "A", new List<string>() { "A1", "A2", "A3"} }, { "B", new List<string>() { "B1", "B2", "B3"} }, { "C", new List<string>() { "C1", "C2", "C3"} }, { "D", new List<string>() { "D1", "D2", "D3"} } }; var combo2 = new List<string>(); var result = dic.TryGetValue(combo1Value, out combo2); if (result) { //新的枚举列表 List<EnumItem> newList = new List<EnumItem>(); foreach (var item in combo2) { newList.Add(new EnumItem { Seq = combo2.IndexOf(item), Value = item, Caption = new LocaleValue(item, this.Context.UserLocale.LCID) }); } //获取fieldEditor来设置下拉的选项列表 ComboFieldEditor fieldEditor = this.View.GetControl<ComboFieldEditor>("FCombo2"); fieldEditor.SetComboItems(newList); } break; } }
下拉列表联动处理.rar(2.04KB)
赞 18
18人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读