首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >基于传递的参数过滤json数据

基于传递的参数过滤json数据

提问于 2018-06-06 08:10:48
回答 1关注 0查看 379

我有下面的JSON文件。我想根据外键(FK)过滤数据。如果我将参数作为1传递到我的页面,那么我希望过滤掉前三行,其中FK键为1,id为1、2和3。我希望他们被显示为列表视图。

同样的,如果我将参数传递为2,那么我希望筛选出id为4、5和6的行以及fk的2。

FK参数来自具有listview的上一页,如果用户选择特定项,则该项ID将传递给此页面。

代码语言:txt
复制
{
    "data":[
        { 
            "id": 1,
            "fk": 1,
            "Loc": "101 Test drive, TX",
            "Long": "12365",
            "Lat" : "87766",
            "Phone": "123-456-7899"
        },
        {
            "id": 2,
            "fk": 1,
            "Loc": "201 Test drive, CA",
            "Long": "12365",
            "Lat" : "87766",
            "Phone": "123-456-9999"
        },
        {
            "id": 3,
            "fk": 1,
            "Loc": "201 Test drive, CA",
            "Long": "12365",
            "Lat" : "87766",
            "Phone": "123-456-9999"
        },
        {
            "id": 4,
            "fk": 2,
            "Loc": "111 Test drive, CA",
            "Long": "12365876",
            "Lat" : "877669999",
            "Phone": "123-456-4040"
        }, 
        {
            "id": 5,
            "fk": 2,
            "Loc": "201 Test drive, CA",
            "Long": "12365",
            "Lat" : "87766",
            "Phone": "123-456-9999"
        },
        {
            "id": 6,
            "fk": 2,
            "Loc": "999 Test drive, CA",
            "Long": "12365",
            "Lat" : "87766",
            "Phone": "123-456-8484"
        },
        {
            "id": 7,
            "fk": 3,
            "Loc": "888 Test drive, CA",
            "Long": "12365432",
            "Lat" : "87766111",
            "Phone": "123-999-8484"
        }
    ]
}

下面是我的全部代码。我确实将data.filter应用到了ComponentdidMount()中,但现在我得到了一个空列表。我不知道我做错了什么:

代码语言:txt
复制
 import React, { Component } from 'react';

import { StyleSheet, Text, View, ListView, ActivityIndicator, TextInput } from 'react-native';

import { StackNavigator } from 'react-navigation';



class MainActivity extends Component {

  constructor(props) {

    super(props);

    this.state = {

      // Default Value of this State.
      Loading_Activity_Indicator: true,
      text:'',

    }
    this.arrayholder=[];
  }

  componentDidMount() {

    const data = require('./data.json')
     var newList = data.filter(obj => obj.fk === 2)

        let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
        this.setState({
          Loading_Activity_Indicator: false,
          dataSource: ds.cloneWithRows(newList),
        }, function() {

          // In this block you can do something with new state.
          this.arrayholder = newList ;
        });


  }

  SearchFilterFunction(text){

    const newData = this.arrayholder.filter(function(item){
        const itemData = item.fruit_name.toUpperCase()
        const textData = text.toUpperCase()
        return itemData.indexOf(textData) > -1
    })
    this.setState({
        dataSource: this.state.dataSource.cloneWithRows(newData),
        text: text
    })
}

  ListViewItemSeparator = () => {
    return (
      <View
        style={{
          height: .5,
          width: "100%",
          backgroundColor: "#000",
        }}
      />
    );
  }

  Navigate_To_Second_Activity=(fruit_name)=>
    {
      //Sending the JSON ListView Selected Item Value On Next Activity.
      this.props.navigation.navigate('Second', { JSON_ListView_Clicked_Item: fruit_name });

    }

  static navigationOptions =
    {

     title: 'MainActivity',

    };


  render()
  {
    if (this.state.Loading_Activity_Indicator) {
      return (
        <View style={styles.ActivityIndicator_Style}>

          <ActivityIndicator size = "large" color="#009688"/>

        </View>
      );
    }

    return (

      <View style={styles.MainContainer}>

  <TextInput 
       style={styles.TextInputStyleClass}
       onChangeText={(text) => this.SearchFilterFunction(text)}
       value={this.state.text}
       underlineColorAndroid='transparent'
       placeholder="Search Here"
        />

     <ListView

          dataSource={this.state.dataSource}

          renderSeparator= {this.ListViewItemSeparator}

          renderRow={(rowData) => <Text style={styles.rowViewContainer} 
          onPress={this.Navigate_To_Second_Activity.bind(this, rowData.Loc)} >{rowData.Loc}</Text>}

        />

      </View>
    );
  }
}

class SecondActivity extends Component
{
  static navigationOptions =
  {
     title: 'SecondActivity',
  };

  render()
  {
     return(
        <View style = { styles.MainContainer }>

           <Text style = { styles.TextStyle }> { this.props.navigation.state.params.JSON_ListView_Clicked_Item } </Text>

        </View>
     );
  }
}

export default MyNewProject = StackNavigator(
{
  First: { screen: MainActivity },

  Second: { screen: SecondActivity }
});

const styles = StyleSheet.create(
{
  MainContainer:
  {
     justifyContent: 'center',
     flex:1,
     margin: 10

  },

  TextStyle:
  {
     fontSize: 23,
     textAlign: 'center',
     color: '#000',
  },

  rowViewContainer: 
  {

    fontSize: 17,
    paddingRight: 10,
    paddingTop: 10,
    paddingBottom: 10,

  },

  ActivityIndicator_Style:
  {

    flex: 1, 
    alignItems: 'center', 
    justifyContent: 'center',
    left: 0, 
    right: 0, 
    top: 0, 
    bottom: 0,

  },

  TextInputStyleClass:{

    textAlign: 'center',
    height: 40,
    borderWidth: 1,
    borderColor: '#009688',
    borderRadius: 7 ,
    backgroundColor : "#FFFFFF"

    }

});

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档