function
// 千分位函数进行“,”分隔
function toThousands(num) {
const isNumber = (numb) => {
if (typeof numb === 'number') return true;
if (typeof numb === 'string') return /^[+|-]?\d+(\.\d+)?$/.test(numb);
return false;
};
if (isNumber(num)) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
return num;
}
// 数字转中文数字
function numFormat(temp) {
const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
const unit = ['', '十', '百', '千'];
const strArr = temp.toString().split('').reverse();
let newNum = '';
strArr.map((e, i) => {
newNum = (i == 0 && e == 0 ? "" : (i > 0 && e == 0 && strArr[i - 1] == 0 ? "" : changeNum[e] + (e == 0 ? unit[0] : unit[i]))) + newNum;
});
return newNum;
}
function Index(){
// 级联选择器数据的异步处理
const [categoryList, setCategoryList] = useState();
// 此方法可用于第一次请求级联选择器数据,也可作为异步请求方法改变dataSource数据结构
async function getCategoryList(catId) {
// 首先按照自己的方法请求数据与传参
const catList = await request('queryCategoryList', {
catId // 类目id
});
function factorial(i) {
// 在递归方法找到当前选择项时,给当前选择项push进children数组
const whereToPush = i.find((item) => item.value == id?.value);
if (whereToPush) {
whereToPush.children = [...cGList];
} else {
// 否则继续查找并删除所有的空children
i.map((item) => {
if (item.children && item.children.length) {
// 此处用了递归方法去查找当前选择项
factorial(item.children);
} else if (item.isLeaf) {
// 对于部分级联选择器中会根据当前层级有children而显示下一级箭头,但是后端返回数据会每一级都有children的情况去删除children空数组
delete item.children;
}
});
}
return i;
}
// 对于要统一将key转换成label的字段,可以先将对象转换成字符串,而后统一replace处理
const cGList = JSON.parse(JSON.stringify(catList).replace(/text/g, 'label'))
if (!categoryList) {
setCategoryList([...factorial(cGList)]);
} else {
const cgList = factorial(categoryList);
setCategoryList([...cgList]);
}
}
return (
<CascaderSelect
name={`cateId${idx + 1}`}
dataSource={categoryList}
hasClear
loadData={getCategoryList}
/>
)
}
// cookie存取数据方法
{*
以下都以存取filter字段为例,有几个点要注意下:
1.setCookie三个参数分别是名字,值,过期时间,
2. setCookie存的时候尽量存json字符串,就是尽量JSON.stringify(值)一下,如:setCookie('filter', JSON.stringify(filter), 1)
3. getCookie取值的时候要做下容错判断,以免值改变或者取值的时候值为空,会报错,判断可以这样写JSON.parse(getCookie('filter')?getCookie('filter'):'{}')
*}
function getCookie=(cname)=>{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
setCookie('filter', JSON.stringify(filter), 1)
const filter=JSON.parse(getCookie('filter')?getCookie('filter'):'{}')
正则
给字符串添加分隔符
场景:获取到字符串20000706,希望经过转换可以获取到2000-07-06的字符串。
解决方案:
①:String birthday = “20000706”;
birthday = birthday.replace(/(.{4})(.{2})/,“$1-$2-”);
只适用于JS文件。
②:String birthday = “20000706”;
String day = birthday.substring(0, 4);
String month = birthday.substring(4,6);
String date = birthday.substring(6, 8);
birthday = day + "-" + month + "-" +date;
substring(int beginIndex,int endIndex);beginIndex的下标是从0开始的,endIndex的下标是从1开始的。
适用于JAVA文件。
https://www.csdn.net/tags/NtTaUgwsNjkzNzUtYmxvZwO0O0OO0O0O.html[转载自此]