【问题标题】:Search by specific field Meteor React Native按特定字段搜索 Meteor React Native
【发布时间】:2018-06-06 21:29:48
【问题描述】:

我尝试仅使用两个字段过滤搜索组件。现在,它返回列表中所有字段的结果。这意味着如果我的搜索是 2,它将返回每个包含 2 的对象。

json 文件如下所示:

{
  "deputes" : [ {
    "depute" : {
      "id" : 1,
      "id_an" : "718902",
      "lieu_naissance" : "Brest (Finistère)",
      "nom_circo" : "Alpes-Maritimes",
      "nom_de_famille" : "Roussel",
      "num_circo" : 3

还有一个要搜索的组件,如下所示:

class Flat_List extends Component{

  constructor(props){
    super(props);
    console.log(props)
    this._handleResults = this._handleResults.bind(this);
    this.state = {
      data : null
    }
  }

  _handleResults(results){
    console.log('handle results')
    this.setState({data: results})
  }

   render() {
    let listitems = this.state.data
    if (this.state.data == null)  {
      listitems = this.props.deputies
    }
    return(
    <View>

      <SearchBar
        ref={(ref) => this.searchBar = ref}
        data={this.props.deputies}
        handleResults={this._handleResults}
        allDataOnEmptySearch = {true}
        showOnLoad = {true}
        hideBack
        autoCorrect= {false}
      />

      <List>
        <FlatList style={styles.flatListStyle}
          data={listitems}
          keyExtractor={(item)=> item._id}
          renderItem={({item})=>(
             <DeputyDetail deputy={item.depute} navigation={this.props.navigation} /> )} />
      </List>

    </View>

这个组件如何只搜索两个字段? {num_circo} 和 {nom} ?

【问题讨论】:

    标签: javascript reactjs meteor react-native


    【解决方案1】:

    您可以从每个depute 对象中删除'id', 'id_an', 'lieu_naissance' 字段,以便&lt;SearchBar /&gt; 不会搜索这些。

    您可以添加以下代码来删除这些字段:

    componentWillMount(){
        var removeObjProps = ['id', 'id_an', 'lieu_naissance'];
        var filteredDep = Object.assign({}, this.props.deputies);
        filteredDep.deputies.forEach(function(dep){
            removeObjProps.forEach(function (val) {
                delete dep.depute[val];
            });
        });
        this.setState({filteredDep})
    }
    

    并将您的搜索栏替换为:

       <SearchBar
            ref={(ref) => this.searchBar = ref}
            data={this.state.filteredDep}
            handleResults={this._handleResults}
            allDataOnEmptySearch = {true}
            showOnLoad = {true}
            hideBack
            autoCorrect= {false}
          />
    

    【讨论】:

    • 不是删除其他字段的真正选项!我需要文件的其余部分来显示结果
    • 它会将你的副手复制到一个新的对象,即 filtersDep 中,并删除 filtersDep 中的字段。您的 this.props.deputies 将保持不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多