lu
2025-04-14 ecb7a60de1639f520712ce95f99414b0dd2c9713
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using Gs.DevApp.Entity;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
 
 
namespace Gs.DevApp.DevFrm.Work
{
    public partial class UcDictionarySelect : DevExpress.XtraEditors.XtraForm
    {
        private List<dynamic> lstCheckedKeyID = new List<dynamic>();//选择集合
        string strTitle = "";
        string strWhere = "";
        public UcDictionarySelect(string _strTitle, string _strWhere)
        {
            InitializeComponent();
            this.strTitle = _strTitle;
            this.strWhere = _strWhere;
            this.Text = _strTitle;
            getTree();
            tlMenu.CustomDrawNodeCheckBox += TreeList1_CustomDrawNodeCheckBox;
            //  tlMenu.OptionsBehavior.Editable = true;
            tlMenu.OptionsSelection.EnableAppearanceFocusedCell = false;
            //  tlMenu.OptionsSelection.MultiSelect = true;
            // tlMenu.OptionsSelection.MultiSelectMode = TreeListMultiSelectMode.CellSelect;
            btnIn.Click += (s, e) =>
            {
                findOrigin(tlMenu);
                var list = new List<dynamic>();
                foreach (dynamic key in lstCheckedKeyID)
                {
                    list.Add(new
                    {
                        dicCode = key.dicCode,
                        dicTxt = key.dicTxt
                    });
                };
                UpdateParent?.Invoke(this, new UpdateParentEventArgs { DynamicList = list });
                Close();
            };
            tlMenu.IndicatorWidth = 50;
            tlMenu.CustomDrawNodeIndicator += (s, ee) =>
            {
                if (ee.IsNodeIndicator)
                {
                    var index = ee.Node.TreeList.GetVisibleIndexByNode(ee.Node);
                    ee.Info.DisplayText = (index + 1).ToString();
                }
            };
        }
 
        /// <summary>
        ///     选择后的回调事件
        /// </summary>
        public event EventHandler<UpdateParentEventArgs> UpdateParent;
        private void TreeList1_CustomDrawNodeCheckBox(object sender, DevExpress.XtraTreeList.CustomDrawNodeCheckBoxEventArgs e)
        {
            // 判断当前节点是否为叶子节点(无子节点)
            if (e.Node.Nodes.Count == 0)
            {
                // 允许绘制复选框(默认行为)
            }
            else
            {
                // 取消绘制复选框
                e.Handled = true;
            }
        }
 
        // 可选:处理节点展开事件,确保动态加载的子节点生效
        private void treeList1_BeforeExpand(object sender, BeforeExpandEventArgs e)
        {
            // 若子节点是动态加载的,在此处加载数据
            // LoadChildNodes(e.Node);
        }
        /// <summary>
        ///     读取列表
        /// </summary>
        private void getTree()
        {
            string _where = " and 1=1 and " + this.strWhere;
            var pgq = new PageQueryModel(1, 999999, "a.defect_name", "asc", "", _where);
            var json = JsonConvert.SerializeObject(pgq);
            try
            {
                var strReturn =
                    UtilityHelper.HttpPost("", "MesDefectCodeManager/GetListPage", json);
                var dd = UtilityHelper.ReturnToTablePage(strReturn);
                var dt = dd.rtnData.list;
                tlMenu.DataSource = dt;
                tlMenu.KeyFieldName = "guid";
                tlMenu.ParentFieldName = "pid";
                tlMenu.Tag = "defectName";
                tlMenu.EndUpdate();
                this.tlMenu.CollapseAll();
                //  tlMenu.OptionsBehavior.Editable = true;
                tlMenu.OptionsBehavior.AllowRecursiveNodeChecking = false;
                tlMenu.BestFitColumns();
            }
            catch (Exception ex)
            {
                MsgHelper.Warning("提示:" + ex.Message);
            }
        }
 
 
        #region MyRegion
 
        /// <summary>
        /// 获取选择状态的数据主键ID集合
        /// </summary>
        /// <param name="parentNode">父级节点</param>
        private void GetCheckedKeyID(TreeListNode parentNode)
        {
            if (parentNode.Nodes.Count == 0)
            {
                if (parentNode.CheckState == CheckState.Checked)
                {
                    DataRowView drv = tlMenu.GetDataRecordByNode(parentNode) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为DataRowView啊)
                    if (drv != null)
                    {
                        string KeyFieldName = (string)drv["defectName"];
                        string dicCode = (string)drv["defectCode"];
                        lstCheckedKeyID.Add(new
                        {
                            dicCode = dicCode,
                            dicTxt = KeyFieldName
                        });
                    }
                }
                return;//递归终止
            }
            foreach (TreeListNode node in parentNode.Nodes)
            {
                if (node.CheckState == CheckState.Checked && parentNode.Nodes.Count == 0)
                {
                    DataRowView drv = tlMenu.GetDataRecordByNode(node) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为DataRowView啊)
                    if (drv != null)
                    {
                        string KeyFieldName = (string)drv["defectName"];
                        string dicCode = (string)drv["defectCode"];
                        lstCheckedKeyID.Add(new
                        {
                            dicCode = dicCode,
                            dicTxt = KeyFieldName
                        });
                    }
                }
                GetCheckedKeyID(node);
            }
        }
        /// <summary>
        /// 获取选中的节点
        /// </summary>
        /// <param name="tree"></param>
        private void findOrigin(DevExpress.XtraTreeList.TreeList tree)
        {
            this.lstCheckedKeyID.Clear();
            if (tree.Nodes.Count > 0)
            {
                foreach (TreeListNode root in tree.Nodes)
                {
                    GetCheckedKeyID(root);
                }
            }
        }
        #endregion
    }
}