我嘗試在輸入中突出顯示輸入內容中的文本,在這種情況下我使用了兩種方法。按我的理解,我需要回到上搜索類似這樣:<mark>${item}<mark/>
突出顯示類型反應的搜索文本
fuzzyContains = (text, search) => {
debugger
if (!text)
return false
if (!search)
return true
search = search.toLowerCase()
text = text.toString().toLowerCase()
let previousLetterPosition = -1
return search.split('').every(s => {
debugger
previousLetterPosition = text.indexOf(s, previousLetterPosition + 1)
return previousLetterPosition !== -1
})
}
handleSearch = search => {
const {data} = this.state
debugger
let filteredData = data.filter(x => Object.keys(x).some(key => {
debugger
this.fuzzyContains(x[`<mark>${key}<mark/>`], search)}))
this.setState({filteredData, search})
}
請好一點解釋用例。目前,你的解釋太模糊了。 「搜索」究竟意味着什麼?你想在輸入文本時輸入一個子字符串? – Chris
@克里斯是在文本輸入 –
打字時簡單地存儲在渲染方法在變量中輸入的文本。通過單詞使用JavaScript的拆分方法,並將CSS樣式添加到地圖函數中的每個單詞。 – fungusanthrax