【发布时间】:2016-06-23 11:11:03
【问题描述】:
你好 :) 我通过向我的 SearchView 添加过滤器模态得到以下问题
我构建了一个
SearchPage,其中可以列出几个事件。这一切都很好。现在我正在尝试将filter添加到我的SearchPage。如果我手动设置过滤器,它工作得很好->现在我的问题:如果我尝试更改 Switch 的开关值,它会设置回根目录,因为该值的状态未设置
我解释的步骤:
我正在尝试打开一个
Modal查看我的所有filter都在哪里列出,我可以使用Switch在哪里设置true/false。我的想法是通过为它创建一个JSON来获取所有filter Settings:
module.exports = {
"filter":
{
"track": [
{
"id": 1,
"description": "IoT & Living tomorrow"
},
{
"id": 2,
"description": "Smart & Digital Retail"
},
{
"id": 3,
"description": "Startups, Digital Culture & Collaboration"
}
]
}
}
上面的 JSON 只是为了举例 - 通常它比跟踪要大得多,主题也更多
现在我导入
JSON并将其保存在var filter。我在这里检查了数据的格式是否正确->filter.track -> All my JSON Objects现在我用
filter Modal创建了一个我的班级
import React, {Component} from 'react';
import {
ListView,
Modal,
StatusBar,
StyleSheet,
Text,
TouchableOpacity,
View,
Switch
} from 'react-native';
var filter = require('../JSON/filter');
class PopoverFilter extends Component {
constructor(props) {
super();
// ds for the menu entries
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
eventTracks: ds.cloneWithRows(filter.filter.track)
}
this.show = this.show.bind(this);
}
render() {
return(
<Modal>
<ListView
style={styles.mainView}
renderRow={this.renderMenuEntries.bind(this)}
dataSource={this.state.eventTracks}/>
</Modal>
);
}
renderMenuEntries(entry) {
var switchState = entry.description;
return(
<View style={styles.switchView}>
<Text style={[styleHelper.fonts.titleSize, styles.text]}>{entry.description}</Text>
<Switch onValueChange={(value) => this.switchChanged(switchState, value)}
value={this.state.switchState}/>
</View>
);
}
switchChanged(field, value) {
var obj = {};
obj[field] = value;
this.setState(obj);
}
}
var styles = StyleSheet.create({
});
module.exports = PopoverFilter;
请忽略缺少的样式,并且模态中还有更多对象,但对于这种情况并不重要。
最重要的是,我尝试通过
renderMenuEntries方法渲染每个 Switch,并为它们提供所有条目 -> 只是 Switch 设置不正确。至于我试图改变开关的值,它会立即回到它的根。并且没有设置任何状态。
也许我的解决方案是不可能的,我必须将每个状态都设为静态 - 但如果我以后可以在不更改整个代码的情况下设置动态过滤器,这个解决方案会非常好
【问题讨论】:
标签: json filter react-native state