xm-select 树形选择器配置与父子节点联动示例
var\u0020xm_one\u0020=\u0020xmSelect.render({\n\u0020\u0020\u0020el:\u0020'#tree_one',\n\u0020\u0020\u0020tips:\u0020'请选择行业标签',\n\u0020\u0020\u0020empty:\u0020'呀,数据为空',\n\u0020\u0020\u0020filterable:\u0020true,\n\u0020\u0020\u0020searchTips:\u0020'请在此搜索标签',\n\u0020\u0020\u0020paging:\u0020false,\n\u0020\u0020\u0020pageSize:\u002010,\n\u0020\u0020\u0020radioed:\u0020false,\n\u0020\u0020\u0020repeat:\u0020false,\n\u0020\u0020\u0020clickClose:\u0020false,\n\u0020\u0020\u0020max:\u00200,\n\u0020\u0020\u0020height:\u0020'200px',\n\u0020\u0020\u0020theme:\u0020{\n\u0020\u0020\u0020\u0020\u0020color:\u0020'#409eff',\n\u0020\u0020\u0020\u0020},\n\u0020\u0020\u0020model:\u0020{\n\u0020\u0020\u0020\u0020label:\u0020{\n\u0020\u0020\u0020\u0020\u0020type:\u0020'block',\n\u0020\u0020\u0020\u0020\u0020block:\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020//最大显示数量, 0:不限制\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020showCount:\u00201,\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020//是否显示删除图标\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020showIcon:\u0020true,\n\u0020\u0020\u0020\u0020\u0020\u0020} \n\u0020\u0020\u0020\u0020} \n\u0020\u0020\u0020},\n\u0020\u0020\u0020tree:\u0020{\n\u0020\u0020\u0020\u0020//是否显示树状结构\n\u0020\u0020\u0020\u0020show:\u0020true,\n\u0020\u0020\u0020\u0020//是否展示三角图标\n\u0020\u0020\u0020\u0020showFolderIcon:\u0020true,\n\u0020\u0020\u0020\u0020//是否显示虚线\n\u0020\u0020\u0020\u0020line:\u0020false,\n\u0020\u0020\u0020\u0020//间距\n\u0020\u0020\u0020\u0020indent:\u002010,\n\u0020\u0020\u0020\u0020//默认展开节点的数组, 为 true 时, 展开所有节点\n\u0020\u0020\u0020\u0020// expandedKeys:\u0020false,\n\u0020\u0020\u0020\u0020//是否严格遵守父子模式\n\u0020\u0020\u0020\u0020strict:\u0020true,\n\u0020\u0020\u0020\u0020//是否开启极简模式\n\u0020\u0020\u0020\u0020simple:\u0020true,\n\u0020\u0020\u0020\u0020//点击节点是否展开\n\u0020\u0020\u0020\u0020clickFolder:\u0020true,\n\u0020\u0020\u0020\u0020//点击节点是否选中\n\u0020\u0020\u0020\u0020accordion:\u0020true,\n\u0020\u0020\u0020},\n\u0020\u0020\u0020data:\u0020industryList,\n\u0020\u0020\u0020on:\u0020function\u0020(data)\u0020{\n\u0020\u0020\u0020\u0020var\u0020ids\u0020=\u0020[];\n\u0020\u0020\u0020\u0020var\u0020parentIds\u0020=\u0020[];\n\n\u0020\u0020\u0020\u0020data.arr.forEach(function\u0020(item)\u0020{\n\u0020\u0020\u0020\u0020\u0020ids.push(item.value);\n\n\u0020\u0020\u0020\u0020\u0020if\u0020(item.parent\u0020!=\u00200)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020parentIds.push(item.parent);\n\u0020\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020});\n\n\u0020\u0020\u0020\u0020var\u0020uniqueIds\u0020=\u0020Array.from(new\u0020Set(ids));\n\u0020\u0020\u0020\u0020var\u0020uniqueParentIds\u0020=\u0020Array.from(new\u0020Set(parentIds));\n\n\u0020\u0020\u0020\u0020var\u0020combinedIds\u0020=\u0020uniqueIds.concat(uniqueParentIds).join(',');\n\n\u0020\u0020\u0020\u0020$('#industry').val(combinedIds);\n\u0020\u0020\u0020},\n});\n\nif\u0020(xm_one.tree\u0020&&\u0020xm_one.tree.obj)\u0020{\n\u0020\u0020\u0020xm_one.tree.obj.on('clickCheck',\u0020function\u0020(data)\u0020{\n\u0020\u0020\u0020\u0020var\u0020item\u0020=\u0020data.item;\n\u0020\u0020\u0020\u0020var\u0020isParent\u0020=\u0020item.children.length\u0020>\u00200;\n\n\u0020\u0020\u0020\u0020if\u0020(isParent)\u0020{\n\u0020\u0020\u0020\u0020\u0020if\u0020(item.checked)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020xm_one.tree.setCheck(item.children,\u0020true);\n\u0020\u0020\u0020\u0020\u0020} else {\n\u0020\u0020\u0020\u0020\u0020\u0020xm_one.tree.setCheck(item.children,\u0020false);\n\u0020\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020} else {\n\u0020\u0020\u0020\u0020\u0020var\u0020parent\u0020=\u0020item.parent;\n\n\u0020\u0020\u0020\u0020\u0020if\u0020(item.checked)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020// 如果所有兄弟节点都被选中,则将父节点设置为选中状态\n\u0020\u0020\u0020\u0020\u0020\u0020var\u0020siblings\u0020=\u0020parent.children;\n\u0020\u0020\u0020\u0020\u0020\u0020var\u0020uncheckedSiblings\u0020=\u0020siblings.filter(function\u0020(sibling)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020return\u0020!sibling.checked;\n\u0020\u0020\u0020\u0020\u0020\u0020});\n\n\u0020\u0020\u0020\u0020\u0020\u0020if\u0020(uncheckedSiblings.length\u0020===\u00200)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020xm_one.tree.setCheck(parent,\u0020true);\n\u0020\u0020\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020\u0020} else {\n\u0020\u0020\u0020\u0020\u0020\u0020// 如果取消选中当前节点,则取消选中父节点\n\u0020\u0020\u0020\u0020\u0020\u0020xm_one.tree.setCheck(parent,\u0020false);\n\u0020\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020});\n}基于xm-select版本为1.2.4应该怎么修改内容:根据提供的代码,基于xm-select版本为1.2.4进行修改的步骤如下:
-
将
radio属性修改为radioed,即将radio: false修改为radioed: false。 -
将
tree.showLine属性修改为tree.line,即将showLine: false修改为line: false。 -
将
tree.clickExpand属性修改为tree.clickFolder,即将clickExpand: true修改为clickFolder: true。 -
将
tree.clickCheck属性修改为tree.accordion,即将clickCheck: true修改为accordion: true。 -
将
xm_one.tree.$el修改为xm_one.tree.obj。
修改后的代码如下:javascriptvar xm_one = xmSelect.render({ el: '#tree_one', tips: '请选择行业标签', empty: '呀,数据为空', filterable: true, searchTips: '请在此搜索标签', paging: false, pageSize: 10, radioed: false, repeat: false, clickClose: false, max: 0, height: '200px', theme: { color: '#409eff', }, model: { label: { type: 'block', block: { showCount: 1, showIcon: true, } } }, tree: { show: true, showFolderIcon: true, line: false, indent: 10, strict: true, simple: true, clickFolder: true, accordion: true, }, data: industryList, on: function (data) { var ids = []; var parentIds = [];
data.arr.forEach(function (item) { ids.push(item.value);
if (item.parent != 0) { parentIds.push(item.parent); } });
var uniqueIds = Array.from(new Set(ids)); var uniqueParentIds = Array.from(new Set(parentIds));
var combinedIds = uniqueIds.concat(uniqueParentIds).join(',');
$('#industry').val(combinedIds); },});
if (xm_one.tree && xm_one.tree.obj) { xm_one.tree.obj.on('clickCheck', function (data) { var item = data.item; var isParent = item.children.length > 0;
if (isParent) { if (item.checked) { xm_one.tree.setCheck(item.children, true); } else { xm_one.tree.setCheck(item.children, false); } } else { var parent = item.parent;
if (item.checked) { var siblings = parent.children; var uncheckedSiblings = siblings.filter(function (sibling) { return !sibling.checked; });
if (uncheckedSiblings.length === 0) { xm_one.tree.setCheck(parent, true); } } else { xm_one.tree.setCheck(parent, false); } } });}
请注意,以上修改是基于xm-select版本为1.2.4进行的,如果使用的是其他版本,请根据具体版本的文档进行相应修改
原文地址: https://www.cveoy.top/t/topic/qfCJ 著作权归作者所有。请勿转载和采集!