简体   繁体   English

使用 react-native 根据 listView 中的 rowid 递增和递减值

[英]Increment and decrement value based on rowid in listView using react-native

I am running a sample project.我正在运行一个示例项目。 In my project i am using listView.在我的项目中,我使用的是 listView。 If i am clicking on up-arrow button that the value will be increased and clicking on down-arrow button that the value will be decreased.如果我单击向上箭头按钮该值将增加并单击向下箭头按钮该值将减少。 When i click on any of these two buttons that the value will be changed in all rows in listView.当我单击这两个按钮中的任何一个时,listView 中所有行中的值都会更改。 How to change the value in only particular row in listView.如何仅更改 listView 中特定行的值。 Please give me any suggestion.请给我任何建议。

Here is my code:这是我的代码:

class Example extends Component {
  constructor(props){
      super(props);
      this.state={
        dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
         count:1,
      }
    }

incrementFunc(rowID:number, listItems){

        this.setState({
          count : this.state.count + 1
        },()=>{
          this.setState({
       dataSource:new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(array)
          })
        })
      }
      decrementFunc(rowID:number,listItems){
        this.setState({
          count : this.state.count- 1
        },()=>{
          if(this.state.count <= 1){
            this.setState({
              count : 1
            })
          }
        },()=>{
          this.setState({
     dataSource:new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(array)
          })
        })
      }
listOfItems(listItems, sectionID:number, rowID:number){
          return(
  <View style = {styles.buttonContainer}>
          <View style={styles.arrowsView}>
          <TouchableOpacity onPress ={this.incrementFunc.bind(this, rowID, listItems )}>
          <Image style={styles.arrowStyle} source={require('@images/up-arrow.png')} />
          </TouchableOpacity>

          </View>
          <View style={styles.numberView}>
          <Text style={{fontSize:15, color:'black'}}>{this.state.count}
            </Text>

          </View>
          <View style={styles.arrowsView}>
          <TouchableOpacity onPress ={this.decrementFunc.bind(this, rowID, listItems)}>
          <Image style={styles.arrowStyle} source={require('@images/down-arrow.png')} />
          </TouchableOpacity>
          </View>
          </View>
)
}
render(){
      return(
<View style={styles.listview}>
        <ListView
             dataSource={this.state.dataSource}
             renderRow={this.listOfItems.bind(this)}/>
        </View>
);
}
}

Here is my screenshot:这是我的截图: 在此处输入图片说明

count state variable is what you are using for all rows which will be the same since its an Integer, consider maintaining a state variable which is an object that will look something like this {rowId1: count1, rowId2, count2} Eg: {1: 15, 2: 10, 3: 20} count状态变量是您用于所有行的内容,因为它是整数,因此所有行都相同,考虑维护一个状态变量,该变量是一个看起来像这样的对象{rowId1: count1, rowId2, count2} Eg: {1: 15, 2: 10, 3: 20}

Since your callbacks send rowId as argument由于您的回调发送rowId作为参数

<TouchableOpacity onPress ={this.incrementFunc.bind(this, rowID, listItems )}>

Try setting the state in your incrementFunc like this尝试像这样在incrementFunc设置状态

incrementFunc(rowID:number, listItems){
    let count = {...this.state.count}
    count[rowId] = count[rowId] || 0;
    count[rowId] += 1;
    this.setState({ count },
      ()=>{
      this.setState({
   dataSource:new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(array)
      })
    })
  }

decrementFunc can be modified in a similar way. decrementFunc 可以用类似的方式修改。

And then in your listOfItems method然后在你的listOfItems方法中

<Text style={{fontSize:15, color:'black'}}>
   {this.state.count[rowId]}
</Text>

Have you tried to change the specific field in the array, instead of the total count?您是否尝试更改数组中的特定字段,而不是总数? If you use rowHasChanged function, it has to detect changes only if it occurs.如果您使用 rowHasChanged 函数,它必须仅在发生更改时检测更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM