The values or the Employee Names are retrieved from Firebase database. I need to search the employees by Name, so as to easily access them on the ListView. For example, if i type'S' , all the employee names with "S" should be displayed same as the ListView, which is touchable.

从Firebase数据库中检索值或员工姓名。我需要按名称搜索员工,以便在ListView上轻松访问它们。例如,如果我键入'S',则所有具有“S”的员工姓名应显示为与可触摸的ListView相同。

Help me implement the Search functionality from the List Items in the ListView.

帮助我实现ListView中列表项的搜索功能。

class EmployeeList extends Component {
    componentWillMount() {
        this.props.employeesFetch();

        this.createDataSource(this.props);
      }

      componentWillReceiveProps(nextProps) {
        // nextProps are the next set of props that this component
        // will be rendered with
        // this.props is still the old set of props

        this.createDataSource(nextProps);
      }

      createDataSource({ employees }) {
        const ds = new ListView.DataSource({
          rowHasChanged: (r1, r2) => r1 !== r2
        });

        this.dataSource = ds.cloneWithRows(employees);
      }

      renderRow(employee) {
        return <ListItem employee={employee} />;
      }



      render() {

        return (
         <View>
         <TextInput 
       style={styles.TextInputStyleClass}
       underlineColorAndroid='transparent'
       placeholder="Quick Search"
        />
          <ListView
            enableEmptySections
            dataSource={this.dataSource}
            renderRow={this.renderRow}
          />
        </View>
        );
      }
    }

    const styles = {
      TextInputStyleClass:{

        textAlign: 'center',
        height: 40,
        borderWidth: 1,
        borderColor: '#ddd',
        borderRadius: 3 ,
        backgroundColor : "#FFFFFF"

        }

  };

    const mapStateToProps = state => {
      const employees = _.map(state.employees, (val, uid) => {
        return { ...val, uid };
      });

      return { employees };
    };

    export default connect(mapStateToProps, { employeesFetch })(EmployeeList);

1 个解决方案

#1


0

Try these methods.

试试这些方法。

In your state have these variables

在你的州有这些变量

i) Search text (which you are entering)
ii) a boolean value for knowing whether the user is searching or not
iii) an array for storing filtered array

i)搜索文本(您正在输入)ii)用于了解用户是否正在搜索的布尔值iii)用于存储已过滤数组的数组

In your textinput onTextChange function, check if the text is empty or not .

在textinput onTextChange函数中,检查文本是否为空。

If it is empty set the boolean value as false,
else set the boolean true and filter the array based on search string (you can use lodash for this)

如果为空,则将布尔值设置为false,否则将布尔值设置为true并根据搜索字符串过滤数组(可以使用lodash)

In your list view render the dataSource based on the boolean value.

在列表视图中,基于布尔值呈现dataSource。

i.e) dataSource={isSearching ? filteredArray : this.dataSource}

即)dataSource = {isSearching? filteredArray:this.dataSource}

更多相关文章

  1. 在Javascript中将带有空格的字符串数组转换为小写,然后在Webkit中
  2. 返回指定时间段相同间隔数组
  3. JavaScript数组中的唯一计数,按计数排序
  4. Javascript sort()不适用于Chrome中超过10个对象的数组
  5. 获取JavaScript数组元素的最大长度
  6. 对多维数组中的列进行排序
  7. 获取拆分字符串数组的最后一个元素
  8. JS将字符串转换为数组
  9. 将textarea值附加到现有数组javascript

随机推荐

  1. Linux实用程序编写了一个matlab .mat文件
  2. 【精】Linux磁盘I/O性能监控之iostat详解
  3. Linux入门笔记之一:系统分区及挂载点
  4. ldconfig报错 :libstdc++.so.6.0.18-gdb.p
  5. linux下的C语言编程(总结篇)
  6. linux 的终端字体色和背景色的修改方法(三
  7. 操作系统的理念—以windows和linux为例
  8. ATT汇编与Intel汇编的区别,摘自《深入分析
  9. 使用Python获得鼠标增量!(在Linux中)
  10. Linux多线程总结