/*
|
* @FilePath: liao.js
|
* @Author: 小飞侠
|
* @Date: 2024-03-01 09:11:24
|
* @LastEditors: Please set LastEditors
|
* @LastEditTime: 2024-03-21 09:47:04
|
* Copyright: 2024 MinTinge CO.,LTD. All Rights Reserved.
|
* @Descripttion: 写入你的描述
|
*/
|
|
|
|
/**
|
* @description: 在树形数组中找到满足条件的项
|
* @param {*} tree 目标数组
|
* @param {*} predicate 筛选函数
|
* @return {*}
|
* @author: 小飞侠
|
* @Date: 2024-02-29 15:19:46
|
* @LastEditors:
|
* @LastEditTime: Do not Edit
|
*/
|
function filterTreeWithjQuery(tree, predicate) {
|
var results = [];
|
|
function traverse(node) {
|
if (predicate(node)) {
|
results.push(node);
|
}
|
if (node.children && Array.isArray(node.children)) {
|
$.each(node.children, traverse);
|
}
|
}
|
|
$.each(tree, traverse);
|
|
return results;
|
}
|
/**
|
* @description: 判断一个字符串是不是数字字符串
|
* @param {*} str
|
* @return {*}
|
* @author: 小飞侠
|
* @Date: 2024-03-01 08:59:13
|
* @LastEditors:
|
* @LastEditTime: Do not Edit
|
*/
|
function isNumericString(str) {
|
const regex = /^-?\d+(\.\d+)?$/; // 匹配整数或小数,包括负数
|
return regex.test(str);
|
}
|
|
|
|
|
/**
|
* 当使用了formatAreaInPut 后 可使用此函数获取一个由地址id组成的areaId 字符串,用,号分割
|
* @param {Object} field -表单。
|
*/
|
function GetAreaCodeFormFileid(field,idname) {
|
let areaCode="";
|
if (isNumericString(field[`${idname}_1`])) {
|
areaCode+=`${field[`${idname}_1`]}`;
|
if (isNumericString(field[`${idname}_2`]))
|
areaCode+=`,${field[`${idname}_2`]}`;
|
if (isNumericString(field[`${idname}_3`]))
|
areaCode+=`,${field[`${idname}_3`]}`;
|
} else {
|
|
}
|
return areaCode;
|
}
|
|
/**
|
* 生成一个地址选择组件,必须绑定一个叫 areaId 的 组件 <input type="hidden"name="areaId" >"
|
* 会生成几个areaId-1,areaId-3,areaId-3
|
* @param {Object} coreHelper - 核心辅助对象,用于执行HTTP请求等操作。
|
*/
|
function formatAreaInPut(coreHelper,idname,areaid="") {
|
let area=0;
|
if(areaid!=="")
|
area= areaid.split(",").reverse()[0];
|
|
coreHelper.Post("Api/Tools/GetAreaCheckedList", { id: Number(area) }, function (data) {
|
if (debug) { console.log(data); } //开启调试返回数据
|
var str = "";
|
$.each(data, function (i, n) {
|
str += `<select lay-ignore name='${idname}_` + (i + 1) + "' dep='" + (i + 1) + "' class='select-address'>";
|
str += "<option value='' >请选择</option>";
|
$.each(n.list, function (h, z) {
|
if (n.hasOwnProperty('info') && n.info?.id == z.id) {
|
str += "<option value='" + z.id + "' selected='selected'>" + z.name + "</option>";
|
} else {
|
str += "<option value='" + z.id + "' >" + z.name + "</option>";
|
}
|
});
|
str += "</select>";
|
});
|
$(`input[name='${idname}']`).after(str);
|
//以上数据输出完,以下绑定事件
|
$.each(data, function (i, n) {
|
if (i < (data.length)) {
|
$(`select[name='${idname}_` + (i + 1) + "']").change(function () {
|
changeareaIdArea(i + 1, data.length,idname);
|
});
|
}
|
});
|
});
|
function changeareaIdArea(i, max_i,idname) {
|
//清除后面节点
|
for (var x = i + 1; x <= 6; x++) { //最多6层,足够了
|
$(`select[name='${idname}_` + x + "']").remove();
|
}
|
var val = $(`select[name='${idname}_` + i + "']").val();
|
if (val != "") {
|
//取子节点数据,然后显示下一级
|
coreHelper.Post("Api/Tools/GetAreaChildren", { id: val }, function (e) {
|
if (debug) { console.log(e); } //开启调试返回数据
|
|
if (e.length > 0) {
|
var str = "";
|
str += `<select lay-ignore name='${idname}_` + (i + 1) + "' dep='" + (i + 1) + "' class='select-address'>";
|
str += "<option value='' >请选择</option>";
|
$.each(e, function (h, z) {
|
str += "<option value='" + z.id + "' >" + z.name + "</option>";
|
});
|
str += "</select>";
|
$(`select[name='${idname}_` + i + "']").after(str);
|
//以上数据输出完,以下绑定事件
|
$(`select[name='${idname}_` + (i+1) + "']").change(function () {
|
changeareaIdArea(i + 1, i + 2,idname);
|
});
|
|
//如果有返回值,就说明省市区没有选择到最终节点
|
if (1 == 1) {
|
$(`input[name='${idname}']`).val("");
|
} else {
|
$(`input[name='${idname}']`).val($(`select[name='${idname}_` + i + "']").val());
|
}
|
} else {
|
$(`input[name='${idname}']`).val($(`select[name='${idname}_` + i + "']").val());
|
}
|
});
|
} else {
|
if (1 == 1) {
|
$(`input[name='${idname}']`).val("");
|
} else {
|
//第一级的元素就直接赋值为空就是了
|
if (i == 1) {
|
$(`input[name='${idname}']`).val("");
|
} else {
|
i--;
|
$(`input[name='${idname}']`).val($(`select[name='${idname}` + i + "']").val());
|
}
|
|
}
|
}
|
}
|
}
|