【问题标题】:sorting a react-native listview对反应原生列表视图进行排序
【发布时间】:2017-05-25 11:32:19
【问题描述】:

我正在尝试对我的 react-native View 中的列表进行排序
- 触发 sortArrayAsc 函数的 onClick 正在工作
- sortArrayAsc 结果集正在运行
- 取消注释调试器部分显示数据源中的值是新排序的顺序
我遇到的问题是,我认为使用 setState 会自动重新呈现列表......这没有发生。

如果没有,有什么想法我缺少什么或该怎么做?
任何指导/代码更新将不胜感激
干杯

export default class HomeScreen extends Component {
  constructor(props) {
    super(props);

    dataSource = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 } );
    sortedData = data;
    this.state = { dataSource: dataSource.cloneWithRows(sortedData) };
  }

  sortArrayAsc(array, key) {
    return array.sort(function (a,b) {
      console.info(b.amount)
      return b.amount < a.amount ? -1
           : b.amount > a.amount ? 1
           : 0
    })
  }

  _onPress() {
    sortedData = this.sortArrayAsc(sortedData, 'amount')
    let dataSource = this.state.dataSource.cloneWithRows(sortedData)
    //debugger
    this.setState = ( this.state.dataSource: dataSource )
  }

  _renderRow (rowData, sectionID) {
    return (
      <ListItem
        ... 
        onPress = { () => { this._onPress() } }
        ...
      />
    )
  }


  render() {
    return (
      <View>
        <List>
          <ListView
            dataSource = {this.state.dataSource}
            renderRow = {this._renderRow.bind(this)}
          />
        </List>
      </View>
    )
  }

};

【问题讨论】:

    标签: listview react-native listitem


    【解决方案1】:

    let  ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 } );
    export default class HomeScreen extends Component {
      constructor(props) {
        super(props);
        sortedData = data;   // this line seems redundent you can remove this
        this.state = { dataSource:ds.cloneWithRows(data) };//I used ds, just for the shake of name
      }
    
      sortArrayAsc(array, key) {
        return array.sort(function (a,b) {
          console.info(b.amount)
          return b.amount < a.amount ? -1
               : b.amount > a.amount ? 1
               : 0
        })
      }
    
      _onPress() {
        sortedData = this.sortArrayAsc(sortedData, 'amount')
        //no need of these lines
        /*let dataSource = //      this.state.dataSource.cloneWithRows(sortedData)*/
        
        //debugger
        //this.setState = ( this.state.dataSource: dataSource )
        /*you should look in the react doc for setState(https://facebook.github.io/react/docs/react-component.html), you may be trying to use 
    this.setState((prevState, props) => {
      return {myInteger: prevState.myInteger + props.step};
    });*/
        this.setState({
          dataSource:ds.cloneWithRows(sortedData)
        });
      }
    
      _renderRow (rowData, sectionID) {
        return (
          <ListItem
            ... 
            onPress = { () => { this._onPress() } }
            ...
          />
        )
      }
    
    
      render() {
        return (
          <View>
            <List>
              <ListView
                dataSource = {this.state.dataSource}
                renderRow = {this._renderRow.bind(this)}
              />
            </List>
          </View>
        )
      }
    
    };

    【讨论】:

    • 嗨,非常感谢您在整个过程中包含 cmets 和 setState 链接。我使用您的解决方案来调试我的原始解决方案,它归结为let dataSource = this.state.dataSource.cloneWithRows(sortedData) 行中的this.state,我最初包含this.state,因为我遇到了Cannot read cloneWithRows 错误,这解决了。不幸的是,它只是掩盖了问题!因此,这个单行更改dataSource = dataSource.cloneWithRows(sortedData) 正在工作。我还将纳入您的附加点 :-) 干杯
    【解决方案2】:

    对于setState,你可以试试这个:

    this.setState({ dataSource: dataSource });

    【讨论】:

    • 您好,感谢您的更新。不幸的是,它没有更新视图列表:-(
    • 嗨,除了 Manjeet 的更新,这一点也是必需的 :-) 即在我的原始 setState 代码中使用花括号。感谢您对此进行评论/评论,Kush R.
    【解决方案3】:

    尝试在渲染中记录控制台

    console.log(this.state.dataSource);
    

    查看它在渲染时是否打印了正确的值。为确保 dataSource 仅在 this.state.dataSource.cloneWithRows(sortedData) 之后设置新值,请将 setState 写入回调函数。

    【讨论】:

      【解决方案4】:

      您可以尝试将 array 传递给 dataSource.cloneWithRows(array) 而不是 object 吗?

      我不确定您的sortedDataarray 还是object

      因为我也遇到了ListView 的问题。呈现的项目与我提供的数据的排序方式不同。

      这是我的数据:

      let data = {
          key1: object,
          key2: object,
          key3: object,
          key4: object,
          key5: object,
          key6: object
      }
      

      这是我在使用以下方法记录结果时发现的:

      console.log(Object.keys(data))
      

      结果是:

      ["key3", "key1", "key5", "key4", "key2"]
      

      与 ListView 中呈现的项的顺序相同。因此,我将代码更改为:

      const dataSource = new ListView.DataSource(
          rowHasChanged: (r1, r2) => (r1 !== r2)
      })
      this.setState({
          ds: dataSource.cloneWithRows(data)
      })
      

      到:

      let dataArray = []
      
      // Sort the keys out
      let keys = Object.keys(data).sort()
      
      // Push them into dataArray
      keys.forEach(key => dataArray.push(data[key]))
      
      const dataSource = new ListView.DataSource(
          rowHasChanged: (r1, r2) => (r1 !== r2)
      })
      
      // Now I am passing array datatype instead of object datatype.
      this.setState({
          dataSource.cloneWithRows(dataArray)
      })
      

      我的问题解决了。

      【讨论】:

        猜你喜欢
        • 2017-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        相关资源
        最近更新 更多