美文网首页
react todo list

react todo list

作者: 栗子daisy | 来源:发表于2020-08-19 10:10 被阅读0次
    let data = [
      { id: 0, column: "_id", type: "t", popular: 0 },
      { id: 1, column: "_index", type: "t", popular: 1 },
      { id: 2, column: "_type", type: "t", popular: 0 },
      { id: 3, column: "collectTime", type: "#", popular: 0 },
      { id: 5, column: "logLine", type: "t", popular: 1 },
      { id: 6, column: "svr_ip", type: "t", popular: 0 },
    ]
    
    constractor(props){
    this.state={data:[]}}
    componentDidMount(){
    data.forEach(i=>{i.selected=false})
    this.setState({data})
    }
    
    return(
    <Sider style={{ width: "500px" }}>
                  <Card
                    title="selected"
                    bordered={false}
                    style={{ width: "200px" }}
                  >
                    <Collapse style={{ width: "100%" }}>
                      {selectedPanelList}
                    </Collapse>
                  </Card>
                  <Card title="avalible" bordered={false}>
                    <Collapse className="avalibleBox">
                     {this.state.data.filter( i=>i.selected===false && i.popular===1).length>0?
    (<div className="popularTitle">popular</div>):""}
                      {avaliblePanelList}
                    </Collapse>
                  </Card>
                </Sider>)
    
     const selectedPanelList =
          this.state.data.filter(item => item.selected).length > 0 ? (
            this.state.data
              .filter(item => item.selected)
              .map(({ id, type, column, selected }, index) => (
                <Panel
                  header={panelHeader(type, column)}
                  key={index}
                  extra={panelExtra(column, selected, "remove")}
                >
                  <div>{text}</div>
                </Panel>
              ))
          ) : (
            <Panel header={panelHeader("?", "_sourse")} key="0">
              <div>{text}</div>
            </Panel>
          )
        const avaliblePanelList = this.state.data
          .filter(item => !item.selected)
          .sort((x, y) => y.popular - x.popular)
          .map(({ id, type, column, selected, popular }, index) => (
            <Panel
              className={popular ? "popular" : ""}
              header={panelHeader(type, column)}
              key={index}
              extra={panelExtra(column, selected, "add")}
            >
              <div>{text}</div>
            </Panel>
          ))
    
    const panelHeader = (type, column) => (
          <span>
            <b style={{ fontWeight: "bold" }}>{type}</b> {column}
          </span>
        )
        // add/remove按钮事件
        const panelExtra = (column, selected, content) => (
          <Button
            onClick={event => {
              event.stopPropagation()
              let newdata = this.state.data.map(item => {
                if (item.column === column) {
                  item.selected = !selected
                }
                return item
              })
            // 其他的交互操作
              let newColums = this.state.columns
              if (content === "remove") {
                newColums = newColums.filter(item => item.dataIndex !== column)
              } else if (content === "add") {
                newColums.push({ title: column, dataIndex: column, key: column })
              }
          
              this.setState({
                data: newdata,
                columns: newColums,
              })
            }}
          >
            {content}
          </Button>
        )
    

    相关文章

      网友评论

          本文标题:react todo list

          本文链接:https://www.haomeiwen.com/subject/gckrjktx.html