【发布时间】:2020-01-09 11:25:54
【问题描述】:
我需要的非常简单,在 SQL 中我会说我只需要一个 SORT ASCENDING 或 DESCENDING。
但我使用 GraphQL、AppSync、DynamoDB 和 React-Native 已经有一段时间了,但我仍然不知道如何按名称对输出列表进行排序...
我使用的是简单的场景,没有 React-Native Apollo,只有 Connect 组件。
我的输出列表:
class PlantScreenNoApollo extends React.Component {
...
render() {
return (
<View style={{flex:1, flexDirection:"column", justifyContent:"flex-start"}}>
<View style={{flex:1,
flexDirection:"row",
justifyContent:"center",
}}>
<TextInput style={styles.sTextInput}
onChangeText={ (text) => this._updateFilter(text) }
value={this.state.filter}/>
<Ionicons name={"ios-search"} size={30} />
</View>
<View style={{ flex:1,
flexDirection:"column",
justifyContent:"flex-start",
alignContent: "center",
minHeight: "70%",}}>
<Connect query={ this.state.filter!=="" ?
graphqlOperation(queries.listPlants, {
filter: {
name: {
contains: this.state.filter
}
}
})
:
graphqlOperation(queries.listPlants)
}>
{
( { data: { listPlants }, loading, error } ) => {
if(error) return (<Text>Error</Text>);
if(loading || !listPlants) return (<ActivityIndicator />);
return (
<FlatList
data={listPlants.items}
renderItem={({item}) => {
return (
<View style={{borderBottomWidth:1, borderBottomColor:"green", padding:5}}>
<TouchableOpacity onPress={()=>this._onPress(item)}>
<View style={styles.hcontainer}>
<Image source={{uri:this.state.logoURL}}
style={styles.iconImage}
/>
<View style={styles.vcontainer}>
<Text style={styles.textH3}>{item.name}</Text>
<View style={styles.hcontainerflexstart}>
{ item.tea && <Image source={{uri:this.state.teaIconURL}} style={styles.iconImageSmall} />}
{ item.bath && <Image source={{uri:this.state.bathIconURL}} style={styles.iconImageSmall} />}
{ item.insence && <Image source={{uri:this.state.insenceIconURL}} style={styles.iconImageSmall} />}
{ item.children && <Image source={{uri:this.state.childrenIconURL}} style={styles.iconImageSmall} />}
</View>
</View>
<Text style={styles.textP}>{item.description.substr(0,50) + "(...)"}</Text>
</View>
</TouchableOpacity>
</View>
);
}}
keyExtractor={(item, index) => item.id}
/>
);
}
}
</Connect>
</View>
<View style={{flex:1, flexDirection:"column", justifyContent:"flex-start"}}>
...
</View>
</View>
);
};
}
这是我的查询描述(由模型 + 代码生成自动生成)
export const listPlants = `query ListPlants(
$filter: ModelPlantFilterInput
$limit: Int
$nextToken: String
) {
listPlants(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
description
...
}
nextToken
}
}
我只想按名称对扫描进行排序。
我什至尝试创建二级索引,但它没有改变任何东西......
有谁知道实现这一目标的最佳方法?
【问题讨论】:
标签: react-native graphql aws-amplify aws-appsync