前端二进制流文件下载
/*
*@param data:二进制流文件
*@param fileName:文件名称
*/
const downLoadExcel = (data, fileName = "") => {
const fileName = `${fileName}.xlsx`;
const blob = new Blob([data], { type: "application/vnd.ms-excel" });
if (typeof window.navigator.msSaveBlob !== "undefined") {
window.navigator.msSaveBlob(blob, fileName);
} else {
const blobURL = window.URL.createObjectURL(blob);
const tempLink = document.createElement("a");
tempLink.style.display = "none";
tempLink.href = blobURL;
tempLink.setAttribute("download", fileName);
if (typeof tempLink.download === "undefined") {
tempLink.setAttribute("target", "_blank");
} else {
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.remokeObjectURL(blobURL);
}
}
};
获取url上参数
const getUrlParams=(url,keyName)=>{
const splitUrl = url.split('?')[1]?.split('&')
const obj = {}
splitUrl.forEach(item => {
const [code,value] = item.split('=')
obj[code]=value
});
return obj[keyName]
}
嵌套关系的数组根据子元素的id获取所有父元素的某个特定属性值的集合
const getParentCustomKeyValueArr = (
data = [],
nodeFiledValue,
customKey = "id",
fieldName = "id",
parentFieldName = "parentId",
childrenName = "children"
) => {
let arrRes = [];
if (data.length == 0) {
if (!!nodeFiledValue && fieldName == customKey) {
arrRes.unshift(data);
}
return arrRes;
}
const fn = (data2 = [], nodeFiledValue2) => {
for (let index = 0; index < data2.length; index++) {
const node = data2[index];
if (node[fieldName] == nodeFiledValue2) {
arrRes.unshift(node[customKey]);
fn(data, node[parentFieldName]);
break;
} else {
if (!!node[childrenName]) {
fn(node[childrenName], nodeFiledValue2);
}
}
}
return arrRes;
};
arrRes = fn(data, nodeFiledValue);
return arrRes;
};
例如
const temp = [
{
label: "天下第一",
id: 1,
children: [
{
label: '天下第一子',
id: 11,
parentId: 1,
children: [
{
label: '天下第一孙',
id: 111,
parentId: 11,
},
],
},
{
label: '天下第二',
id: 12,
parentId: 1,
children: [
{
label: '天下第二-子',
id: 121,
parentId: 12,
},
],
},
],
},
{
label: '天下第二',
id: 2,
children: [
{
label: 21,
id: 21,
parentId: 2,
children: [
{
label: 211,
id: 211,
parentId: 21,
},
],
},
],
},
];
getParentCustomKeyValueArr(temp, "111", "label")
//得到 ['天下第一', '天下第一子', '天下第一孙']