当前位置:网站首页>[taro] applet picker dynamically obtains data
[taro] applet picker dynamically obtains data
2022-07-23 16:30:00 【Web old monkey】
In the applet ,picker Selectors provide several basic data formats , Such as time , date , Such area , It has basically met most of the needs .
But facing the customers and product managers who have opened their brains , The official has also left us a personalized space , stay picker Properties of the , Set the value to multiSelector, Then pass in a two-dimensional array data with unlimited length , To achieve a personalized multi selector .
When the amount of data is small , We can directly request the backend to send a message with a length of 3 Or a longer two-dimensional array , Insert into range in . But most linkage selector requirements are product categories , Customized regional data, etc , This kind of data is very large , And it's getting bigger and bigger , Request all data at once , Maybe not .
For example, a product classification data has a primary classification , second level , Not too many , But there are three levels , Four level classification , The amount of data increases as a set , Plus the relevant data carried , Maybe an interface is as light as 100 many kb, Heavy regulation 500+kb, At this time, it is necessary to obtain level by level .
One 、 Look at logic first
The logic is like this , The yellow square is assigned a value , Select below .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-WCdhYOZl-1658392588806)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d0790c78af4c436f8725d3888be77795~tplv-k3u1fbpfcp-zoom-in-crop-mark:4536:0:0:0.image?)]
Generally, when entering the page 2 States ,picker With or without value , That is, the new form is null , When modifying, there are values that need to be echoed .
Exhibition
Data interface
getRangeData({parentCode:''}: Pass in code Get the lower level data list , The top-level data list is not returned
- Anyway , The first step is through the interface provided by the back end (
getRangeData({parentCode:''})) Get the first column of datac1. - If it's new , Directly through
c1Of the first value of code, thec1[0].codePass in the interface to get secondary datac2, The same is true for the third column of data . - If it is a modification , Here comes a value like this , Corresponding to the selected values in the three columns , take value Of the first value of code Incoming interface , That is, through
selected[0].codeGet the second column of data , The third column does the same .
const selected = [
{name: 'xxx',code: 0},
{name: 'xxx',code: 01},
{name: 'xxx',code: 012}
]
- All the data obtained are arrays , Combine the obtained data into a two-dimensional array and assign values , namely
setRange([c1,c2,c3]), Now picker The basic functions of have been completed , It's ready to show .
operation
If you can show it , Users can click to operate , The function is half finished . As you scroll through each column , Still need to continue to get data , Dynamically modify the data of the next column , It's simpler here .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-yJV9i95R-1658392588807)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/cd90b550d4de4450b07a58d8ebe05e0c~tplv-k3u1fbpfcp-zoom-in-crop-mark:4536:0:0:0.image?)]
- adopt
onColumnChangeMethod , Get the currently scrolling column (column) And scroll to the index value (value), You can use therange[column][value].codeGet and modify the value of the next column . - When you click OK , set out
onChangeMethod , Get the index value selected in the current three columns (value), Through this index and range Data combination[range[0][value[0]],range[1][value[1]],range[2][value[2]]], You get the selected value .
So much logic as a whole , The most painful thing is the combination , More twists and turns .
Two 、 Code up
view part
When the value is empty , Please select , When there is a value, the selected value is displayed
<Picker
mode="multiSelector"
range={range}
rangeKey="name"
value={pickerValue.current}
onChange={e => onChange(e)}
onColumnChange={e => colChange(e)}
>
{selected ? (
<Text>{selected.map(i => i.name).join()}</Text>
) : (
<Text style={
{ color: "#686868" }}> Please select </Text>
)}
</Picker>
js part
What is used here is taro-react Framework to implement this method , I use vue It has also been realized once , The code structure is basically not bad .
Initialization data
const pickerValue = useRef([0, 0, 0]); // Index value of selector
const [range, setRange] = useState<Array<any>>([]); // The value of the selector
const [selected, setSelected] = useState<Array<any>>([]); // Selected value
useEffect(() => {
const init = async () => {
const p = await getRangeData({});
if (typeof params.selected === "undefined") {
// When the value is empty , Get the value of the next column by classifying the first value
const pp = await getRangeData({parentCode: p.data[0].code});
const ppp = await getRangeData({parentCode: pp.data[0].code});
setRange([p.data, pp.data, ppp.data]);
} else {
// When there is value , Get the value of the next column by the selected value
const s = params.selected
const pp = await getRangeData({parentCode: s[0].code});
const ppp = await getAddrList({parentCode: c[1].code});
setRange([p.data, pp.data, ppp.data]);
setSelected(s);
}
};
init();
}, []);
Column selection
Now ,picker The default value of is successfully inserted , The next step is to dynamically obtain values when switching
const colChange = e => {
const { column, value } = e.detail;
pickerValue.current[column] = value;
if (column === 0) { // Sliding column 1
let c0 = range[0][value]; // Get the selected value of column 1
const p = await getRangeData({ parentCode: c0.code });
setRange(v => [v[0], p.data, v[2]]);
} else if (column === 1) {
let c1 = range[1][value]; // ditto
const p = await getRangeData({ parentCode: c1.code });
setRange(v => [v[0], v[1], p.data]);
}
};
When pciker When switching , Get scrolling columns and positions , Then get the selected value of the current column through the index and get the following values
Here's a point to note , our
rangeData is an array , It's a reference type , If you use the following method to modify the valuesetRange(v => { v[1] = p.data return v });It can be modified successfully , But the page refresh cannot be triggered , That is, the value of the second column will not change , You must use a deep copy method to trigger a page refresh
Confirm selection
Click OK to confirm , You can get the index values of three columns , And with the existing range One to one correspondence of data , Got it. 3 Selected values , Tell the selected value to be filtered into selected in , That is, all functions have been completed
const onChange = e => {
let v = e.detail.value;
let s0 = range[0][v[0]];
let s1 = range[1][v[1]];
let s2 = range[2][v[2]];
setSelected([
{name: s0.name, code: s0.code}
{name: s1.name, code: s1.code}
{name: s2.name, code: s2.code}
]);
};
summary
Function is relatively simple , But the first time I wrote it, there were many mistakes , It took half a day to write , It has been optimized for half a day , Spent a day on this little feature , Other projects in the future need to dynamically load lists , You can use this method .
边栏推荐
- Nport serial server principle, moxa serial server nport-5130 detailed configuration
- Flutter | 给 ListView 添加表头表尾最简单的方式
- Of the 24 almost inevitable JVM interview questions, I only know 7. How many can you answer?
- 七、jmeter发出请求的逻辑
- Mailbox communication-
- Esp8266 nodemcu flash file system (spiffs)
- [in simple terms] from self information to entropy, from relative entropy to cross entropy, nn Crossentropyloss, cross entropy loss function and softmax, multi label classification
- Basic concept and deployment of kubernetes
- 千万别让富坚义博看到这个
- FPGA-HLS-乘法器(流水线对比普通仿真)
猜你喜欢

table自定义表格的封装

Flutter | 指定页面回传值的类型

反转链表画图演示

Governance and network security of modern commercial codeless development platform
![[suctf 2018]multisql (MySQL precompiled)](/img/ae/501b7f9c6d8259c3c799e4ff0b568b.png)
[suctf 2018]multisql (MySQL precompiled)

Flutter 组件的生命周期、State 管理及局部重绘 | 开发者说·DTalk

单片机内部IO口保护电路及IO口电气特性以及为什么不同电压IO之间为什么串联一个电阻?

智慧民航新业态崭露头角,图扑数字孪生入局民航飞联网
![[cloud native] continuous integration and deployment (Jenkins)](/img/3a/2cd6f0c768bd920b3de6d4f5b13cd5.png)
[cloud native] continuous integration and deployment (Jenkins)

Memory methods of big end mode and small end mode
随机推荐
AWS 6 AWS IOT
Using mobile phones too much may lose your job
Ali Er Mian: when does MySQL use table locks and row locks?
Another award | opensca was selected as the "top ten open source software products in the world" at the China Software Expo
数据库的备份和还原
UiPath Studio Enterprise 22.4 Crack
Mysql客户端到服务端字符集的转换
es6把多个class方法合并在一起
FPGA HLS multiplier (pipeline vs. ordinary simulation)
Custom JSTL tag of JSP
Packaging and use of alamofire framework
低佣金账户怎么开?安全吗?
Go language learning - Review package, interface, file operation
牛客-TOP101-BM35
MySQL 灵魂 16 问,你能撑到第几问?
Why does fatal always appear when using opengaussjdbc? (tag database keyword user)
Calendar calendar class
2022蓝帽杯初赛wp
[cloud native] continuous integration and deployment (Jenkins)
自定义一个对象