问题
React native noob here , I wanna know if it's possible to use the FullTextSearch feature of sqlite in react, if so , tell me where i can learn more about it . Thanks !
回答1:
Use Realm Database for React Native, Realm is an object-oriented database. OO model makes it 10x faster than SQLite and saves you from running tons of query which is a common thing for a typical SQL database and fuse.js may help you to search text.
回答2:
realm = new Realm({
schema: [StudentScheme]
})
const mydata = realm.objects('Product_Info');
let filteredData = [];
let keywords = search.split(" ");
keywords.forEach((obj, index) => {
let databaseSearchResult = mydata.filtered("prodName LIKE[c] $0 OR prodDesc LIKE[c] $0", "*" + obj + "*")
Object.keys(databaseSearchResult).map(key => {
filteredData.push(databaseSearchResult[key])
})
});
<SearchBar
placeholder="Type Here..."
onChangeText={this.updateSearch}
value={this.state.search}
/>
<FlatList
data={this.state.filteredData}
renderItem={this.rowRenderer}
keyExtractor={(item, key) => key}
/>
=====================================================================
updateSearch = search => {
this.setState({search: search}, () => this.searchText(search.trim()));
};
searchText = (search) => {
console.log(" Detail Activity ------------- search -->" + search);
realm = new Realm({
schema: [StudentScheme]
})
const mydata = realm.objects('Product_Info');
let filteredData = {};
let keywords = search.split(" ");
keywords.forEach((obj, index) => {
let databaseSearchResult = mydata.filtered("prodName LIKE[c] $0 OR
prodDesc LIKE[c] $0", "*" + obj + "*")
Object.keys(databaseSearchResult).map(key => {
filteredData[`${Object.keys(filteredData).length}`] =
databaseSearchResult[key]
})
});
this.setState({
filteredData
}, () => {
console.log('Search-------------------------------FILTER DATA', this.state.filteredData)
let dataProvider = new DataProvider((r1, r2) => r1 !== r2)
let updatedDataProvider=dataProvider.cloneWithRows(filteredData)
this.setState({dataProvider: updatedDataProvider},()=>{
console.log("CALLBACKK ", this.state.dataProvider)
})
})
}
<SearchBar
placeholder="Type Here..."
onChangeText={this.updateSearch}
value={this.state.search}
/>
{
Object.keys(this.state.filteredData).map((key)=>(
this.rowRenderer(null, this.state.filteredData[key])
))
}
回答3:
var Realm = require('realm');
let realm;
let dataProvider = new DataProvider((r1, r2) => r1 !== r2)
realm = new Realm({
schema: [StudentScheme]
})
state = {
search: '',
dataProvider: new DataProvider((r1, r2) => r1 !== r2).cloneWithRows({}),
filteredData: {}
};
updateSearch = search => {
this.setState({search: search}, () => this.searchText(search.trim()));
};
searchText = (search) => {
console.log(" Detail Activity ------------- search -->" + search);
const mydata = realm.objects('Product_Info');
let filteredData = [];
let keywords = search.split(" ");
keywords.forEach((obj, index) => {
let databaseSearchResult = mydata.filtered("prodName LIKE[c] $0 OR prodDesc LIKE[c] $0 OR prodPrice LIKE[c] $0 ", "*" + obj + "*" )
Object.keys(databaseSearchResult).map(key => {
filteredData.push(databaseSearchResult[key])
})
});
this.setState({
filteredData
}, () => {
console.log('Search-------------------------------FILTER DATA \n', this.state.filteredData)
console.log('Search-------------------------------FILTER DATA--------------- \n');
})
}
fetchDB = () => {
var mydata = realm.objects('Product_Info');
this.setState({dataProvider: dataProvider.cloneWithRows(mydata), filteredData: mydata}) //TODO ...
}
<FlatList
data={this.state.filteredData}
renderItem={this.rowRenderer}
keyExtractor={(item, key) => key}
/>
来源:https://stackoverflow.com/questions/49348606/full-text-search-in-react-native