2016-09-18 3 views
1

検索バーが上部にあり、結果はかなり基本的なリストビューにレンダリングされます。検索結果(文字列)にハイライト表示された検索バーに入力されたすべての単語(大文字と小文字は区別されません)を保持したいと思います。私は基本的な分割を行う時点では文字列をさまざまなテキスト要素に分割します

let split = question.title.split(this.state.searchInput); 

し、それをレンダリングする:接続されていない2つの単語を入力する時に明らかに動作しません

<Text style={styles.title}> 
{split[0]} 
</Text> 
    {split.length > 1 ? 
    <View style={{flexDirection: 'row'}}> 
     <Text style={styles.fatTextResult}> 
     {this.state.searchInput} 
     </Text> 
     <Text style={styles.title}> 
     {split[1]} 
     </Text> 
    </View> 
    : 
    null 
    } 

このスプリット検索結果どうすればそれを達成できますか?

それはこのようになりますをatm ..

enter image description here

答えて

1

これを試してみてください:

highlightWord(word , search){ 

    var newWord = word.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,""); 

    if (search.toLowerCase().indexOf(newWord.toLowerCase()) != -1){ // if word in question 
     // highlight it 
     return <Text style={{fontWeight:'bold'}}>{word}</Text> 
    } 
    else{ 
     return <Text>{word}</Text> 
    } 
} 

renderRow(question){ 

    let split = question.title.split(' '); //divide question in words 
    textViews = []; 
    for (var i=0 ; i<split.length ; i++){ 
     var word = split[i]; // get words 
     textViews.push(highlightWord(word,this.state.searchInput)); 
    } 

    // return all words together (highlighted or not) 
    return <View style={{flexDirection:'row'}}>{ textViews }</View> 
} 

EDIT

私は句読点を含む単語を処理するためにhighlightWordの最初の行を追加しました。

+0

これはうまくいきましたが、句読点で終わる単語はありません...アイデアはありますか? – dv3

+0

これは句読点付きの単語で動作するはずです。 – leo7r

関連する問題