react native SectionList组件实现多选
如下图所示:
代码如下:
import React, { useRef, Component } from 'react';
import {
Platform,
Text,
View,
TextInput,
TouchableOpacity,
ScrollView,
Image,
Button,
SectionList,
StyleSheet,
ToastAndroid,
Dimensions,
Alert,
} from 'react-native'; import Icon from 'react-native-vector-icons/Ionicons';
{
titleId: "1",
titleName: "水果",
data: [
{ id: '01', name: '香蕉', selected: false },
{ id: '02', name: '梨', selected: false },
{ id: '03', name: '葡萄', selected: false },
{ id: '04', name: '猕猴桃', selected: false },
{ id: '05', name: '苹果', selected: false },
{ id: '06', name: '桃子', selected: false },
{ id: '07', name: '西瓜', selected: false },
{ id: '08', name: '橘子', selected: false },
]
},
{
titleId: "2",
titleName: "菜品",
data: [
{ id: '09', name: '辣椒', selected: false },
{ id: '10', name: '白菜', selected: false },
{ id: '11', name: '青菜', selected: false },
{ id: '12', name: '茄子', selected: false },
{ id: '13', name: '南瓜', selected: false },
{ id: '14', name: '土豆', selected: false },
{ id: '15', name: '西红柿', selected: false },
{ id: '16', name: '粉条', selected: false },
{ id: '17', name: '豇豆', selected: false },
{ id: '18', name: '牛肉', selected: false },
{ id: '19', name: '猪肉', selected: false },
{ id: '20', name: '鸡翅', selected: false },
{ id: '21', name: '鸡爪', selected: false },
{ id: '22', name: '鸭肉', selected: false }, ]
}, ]; export default class TestScreen extends Component {
constructor(props) {
super(props);
this.state = {
sourceData: DATA,
selectedItem: []//选中的项
}
}; render() {return (
<View style={styles.container}><SectionList
sections={this.state.sourceData}
keyExtractor={(item, index) => index.toString()}
extraData={this.state}
stickySectionHeadersEnabled={true}//吸顶效果
renderItem={this._renderItem} //cell
renderSectionHeader={({ section: { titleName } }) => (
<View style={{ height: 40, justifyContent: 'center', backgroundColor: 'rgba(232,240,248,1)' }}>
<Text style={[, { color: "#0a3989", textAlign: 'center', fontSize: CommonVar.userStyle.titleFontSize + 2 }]}>{titleName}</Text>
</View>
)}
ItemSeparatorComponent={() => {
return <View style={{ borderWidth: 0.2, borderColor: "#d2d2d2" }} />
}}
/>
</View>
)
} _renderItem = (info) => {
// console.log(info);
if (info.item.selected == true) {
return <TouchableOpacity onPress={this._itemPress.bind(this, info.item, info.index)}>
<View style={{ height: 45, flexDirection: 'row', justifyContent: 'space-between', backgroundColor: '#FFFFFF' }}>
<Text style={{ marginLeft: 10, alignSelf: 'center', color: "#000000" }}>{info.item.name}</Text>
<Icon name="ios-checkmark-outline" color='blue' size={25} style={{ alignSelf: 'center', marginRight: 5 }} />
</View>
</TouchableOpacity>
} else {
return <TouchableOpacity onPress={this._itemPress.bind(this, info.item, info.index)}>
<View style={{ height: 45, flexDirection: 'row', justifyContent: 'space-between', backgroundColor: '#FFFFFF' }}>
<Text style={{ marginLeft: 10, alignSelf: 'center', color: "#000000" }}>{info.item.name}</Text>
<Icon name="ios-square-outline" color='#d2d2d2' size={25} style={{ alignSelf: 'center', marginRight: 5 }} />
</View>
</TouchableOpacity>
}
} _itemPress(selectItem, index) {
var $this = this;
this.state.sourceData.forEach(function (item1, lev1Index) {
item1.data.forEach(function (item2, lev2Index) {
if (item2.id == selectItem.id) {
//循环数据是否存在,存在就移除
var isExist = false;
$this.state.selectedItem.forEach(function (obj, objIndex) {
if (obj.id == selectItem.id && obj.titleId == item1.titleId) {
//找到存在的对象删除掉
$this.state.selectedItem.splice(objIndex, 1);
isExist = true;
}
})
if (isExist == false) {
//不存在就加到集合中去
$this.state.selectedItem.push({ id: selectItem.id, titleId: item1.titleId });
}
$this.state.sourceData[lev1Index].data[index].selected = !selectItem.selected;
}
})
})
console.log(this.state.selectedItem)
this.setState({ sourceData: this.state.sourceData })
}
} const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#FFFFFF"
}, })
react native SectionList组件实现多选的更多相关文章
- 封装 React Native 原生组件(iOS / Android)
封装 React Native 原生组件(iOS / Android) 在 React Native中,有很多种丰富的组件了,例如 ScrollView.FlatList.SectionList.Bu ...
- React Native 之 组件化开发
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React Native的组件ListView
React Native的组件ListView类似于iOS中的UITableView和UICollectionView,也就是说React Native的组件ListView既可以实现UITableV ...
- React Native交互组件之Touchable
React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...
- React Native常用组件在Android和IOS上的不同
React Native常用组件在Android和IOS上的不同 一.Text组件在两个平台上的不同表现 1.1 height与fontSize 1.1.1只指定font,不指定height 在这种情 ...
- react native 常用组件汇总
react-native-uploader //文件上传https://github.com/aroth/react-native-uploader jpush-react-native //官方版本 ...
- React Native常用组件Image使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React Native 中组件的生命周期
概述 就像 Android 开发中的 View 一样,React Native(RN) 中的组件也有生命周期(Lifecycle).所谓生命周期,就是一个对象从开始生成到最后消亡所经历的状态,理解生命 ...
- React Native 获取组件(Component)在屏幕上的位置
年后主客户端的需求以及老的业务迁移RN,现在疯狂的在学RN.在迁移需求的时候遇到需要获取组件在屏幕上的绝对位置.页面如下: 就需要展开的时候获取sectionHeader(默认排序)在屏幕上的具体位置 ...
- React Native常用组件之ListView
1. ListView常用属性 ScrollView 相关属性样式全部继承 dataSource ListViewDataSource 设置ListView的数据源 initialListSize n ...
随机推荐
- YMOI 2019.6.8
题解 YMOI 2019.6.8 前言 第二回考试,承让拿了第一次rank1,(●ˇ∀ˇ●) 题解 这次考试总体发挥比较好,每一道题都尽可能得取得了所能及的所有分.虽然多少还是有失误,不过在所难免.保 ...
- 你知道这个提高 Java 单元测试效率的 IDEA 插件吗
前言 2023年我们公司主抓代码质量,所以单元测试必不可少,而且都写到了年底的绩效目标中了.在考虑如何达成这个目标的过程中,我发现了一个关于单元测试的IDEA插件--SquareTest,它可以帮助我 ...
- 在Typescript项目中,使用ESLint和Prettier,以及解决保存代码后ESLint配置冲突问题
首先,检查项目中根目录.eslintrc.js文件,该文件中定义了ESLint的基础配置,找到其中的rules 例如: const prettierConfig = require('./.prett ...
- 实操记录之-----Ant Design of Vue 增强版动态合并单元格,自动根据数据进行合并,可自定义横纵向合并
前几天搞了个简易版的动态合并单元格 但是需求有变化,就只能稍微改改了~~ 欢迎路过的各位大佬指出我代码的问题~~~~ 另: 代码执行效率不是很高,如果需要大量渲染更多数据建议可以直接使用原生 < ...
- 发布并部署NCF站点的那些事
简介 开工第一天,祝大家2023年钱兔无量,技术兔飞猛进 为更加方便大家一站式打通所有使用NCF的环节,在新年开工的第一天给大家带来如何发布最新版本的站点 无论你的网站在开发环境做的多么的炫酷,实用, ...
- Unity之生成扫描二维码
Unity之生成扫描二维码 Unity之生成扫描二维码 前言 开篇 Unity版本及使用插件 正题 前期准备 首先生成二维码 然后需要扫描二维码 该使用了 挂载脚本绑定按钮和输入框 运行内容 生成二维 ...
- wangeditor富文本编辑和vue3
官网: wangEditor https://www.wangeditor.com/v5/ 为啥用这个富文本编辑器(我觉得官网写自己优势已经非常好了没有啥可补充的了) 文档特别的全和友好 安装 ya ...
- Djanngo-bbs项目
1.项目开发基本流程 1.需求分析 2.架构设计 3.分组开发 4.提交测试 5.交付上线 2.项目流程 仿造博客园项目(核心:文章的增删改查) 1.表分析: 1.1用户表 1.2个人站点表 1.3文 ...
- CentOS即将停止维护,拥抱阿里“龙蜥“(Anolis OS),VMware安装Anolis OS与介绍
一.前言 大家在自己电脑来进行服务器的一些操作时,基本都是使用CentOS 7或者是CentOS 8,但是2021年底CentOS 8宣布停止了维护:CentOS 7 在2024年6月30日也会停止维 ...
- (一) MdbCluster分布式内存数据库——基础架构介绍
(一) MdbCluster分布式内存数据库--基础架构介绍 这个项目是怎么开始的我已经有些记不清楚了,大概是原来的内存数据库很不好用,一次次地让我们踩坑,我又自以为是地觉得可以做一个更好的出来. ...