1. 程式人生 > >React Native之學習ListView的單選以及記錄資料

React Native之學習ListView的單選以及記錄資料

var mSelectWhat = -1; var ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }); //當且僅當cell中的任意兩行不相等時才重新渲染\ export default class Designer extends Component { constructor(props) { super(props); this.state = { isShowSearch: false, //是否顯示搜尋框 dataSource
: ds.cloneWithRows([]) }; this.checkedArr = [] } static propTypes = { //屬性 }; static defaultProps = { //屬性預設值 }; componentWillMount() { GesstDesignTeam((success) => { // 1處 服務介面獲取資料 LetAll = success.result; alert(success.result) success.result.map((o, i) => { if (!this.props.mID) { 識別第二次進來的引數是否有點選過 LetAll[i]['isCheck'] = false; } else { if (this.props.mID == o.user_id) { //判斷點選了誰 LetAll[i]['isCheck'] = true; } else { LetAll[i]['isCheck'] = false; } }
}) this.setState({ dataArr: ds.cloneWithRows(LetAll) }) }) } onBack() { if (this.props.getname) { //回撥傳值給上個頁面 this.props.getname(mSelectWhat != -1 ? LetAll[mSelectWhat] : []); } this.props.navigator.pop(); } changeSearch() { this.setState({ isShowSearch: !this.state.isShowSearch }) } renderHeadLeft() { return ( <TouchableOpacity onPress={this.onBack.bind(this)}> <Text style={{ color: '#aaa' }}>取消</Text> </TouchableOpacity> ) } ChangeCheck(item, index) { //判斷點選了誰 if (item.isCheck) { LetAll[index]['isCheck'] = false; mSelectWhat = -1; } else { LetAll.map((o, i) => { if (i == index) { LetAll[i]['isCheck'] = true mSelectWhat = i; } else { LetAll[i]['isCheck'] = false } }) } this.setState({ dataArr: ds.cloneWithRows(LetAll) }); }
renderRow(rowData, sectionID, rowID, highlightRow) { return ( <ListItem onPress={this.ChangeCheck.bind(this, rowData, rowID)} key={rowID} style={{ backgroundColor: 'white', marginLeft: 0, paddingLeft: _getWidth(15) }}> <Left > <Text style={{ lineHeight: 20 }}>{rowData.name}</Text> </Left> {rowData.isCheck && <Image style={{ width: 20, height: 20 }} source={require('../../../img/isChecked.png')}></Image>} </ListItem> ) } render() { return ( <Container style={{ backgroundColor: _backgroundColor }}> <Content> <ListView enableEmptySections={true} dataSource={this.state.dataSource} renderRow={this.renderRow.bind(this)} /> </Content> </Container> ); } }