index.js 4.14 KB
import React from 'react'
import UserManageBar from '@components/Common/WhiteBar/index'
import { Form, Button, Input, Tree, message, } from 'antd'
import Utils from '@src/utils/utils'
const FormItem = Form.Item;
const TreeNode = Tree.TreeNode
//创建所属机构树
class OrgOptionsForm extends React.Component {
  state = {
    checkedKeys: ['1', '2', '21'],
    checked: {
    }
  }
  componentDidMount() {
    //获取默认数据
    const { checkedKeys } = this.props
    this.setState({ checkedKeys })
    console.log(checkedKeys)
  }

  componentWillReceiveProps(nextProps) {
  }


  //查询角色列表
  queryList = () => {
    let data = this.props.form.getFieldsValue();
    if (!data.name || !data.name.replace(/\s+/g, "")) {

      message.warn('请输入角色名')
      return
    }
    this.props.getRoleParam(data.name.replace(/\s+/g, ""))
    this.setState({
      checkedKeys: []
    })
  }

  //重置
  reset = () => {
    this.props.getRoleInfo()
    this.props.form.resetFields()
    this.setState({
      checkedKeys: []
    })
    this.props.getCheckedObj({})
    this.props.getRoleObj({})
  }

  //递归获取树形机构图
  renderTreeNodes = (data) => {
    return data.map(item => {
      if (item.roles && item.roles.length > 0) {
        return <TreeNode title={<span> {Utils.formatTableColumn(item.name)}

        </span>} key={item.id}
          name={item.name}
        >

          {this.renderTreeNodes(item.roles)}
        </TreeNode>
      } else {
        return <TreeNode title={<span> {Utils.formatTableColumn(item.name)}

        </span>} key={item.id}
          name={item.name}
        >

        </TreeNode>
      }
    })
  }

  onCheck = (checkedKeys, e) => {
    let checkedobj = []
    if (e.checkedNodes && e.checkedNodes.length > 0) {
      checkedobj = e.checkedNodes.map(item => {
        return Object.assign({}, {
          key: item.key,
          value: item.props.name
        })
      })
    }
    this.setState({ checkedKeys, checkedobj });
    if (e.checkedNodes && e.checkedNodes.length >= 0) {
     this.props.getRoleObj(checkedobj, checkedKeys.checked)
    }
  }

  onSelect = (checkedKeys, e) => {
    // this.setState({
    //   checkedKeys,
    //   checked: {
    //     key: checkedKeys[0],
    //     val: e.node.props.name
    //   }
    // })
    // // console.log('子组件',this.state.checked);
    // if (checkedKeys && checkedKeys.length > 0) {
    //   this.props.getCheckedObj(
    //     {
    //       key: checkedKeys[0],
    //       val: e.node.props.name
    //     }
    //   )
    // } else {
    //   this.props.getCheckedObj(
    //     {

    //     }
    //   )
    // }
  }

  render() {
    const { getFieldDecorator } = this.props.form;
    const roleOptionsArr = this.props.roleOptionsArr || []

    return (
      <div>
        <Form layout="inline" >
          <FormItem  >
            <label >角色名称</label>
            {
              getFieldDecorator('name', {
              })(
                <Input placeholder="角色名称" />
              )
            }
          </FormItem>
          <FormItem style={{ marginTop: 38, }} >
            <Button onClick={(item) => { this.queryList(item) }} style={{ background: '#00BB29', color: "#fff", marginRight: 4 }} >查询</Button>
            <Button onClick={this.reset} style={{ background: '#ED1719', color: "#fff" }} type="danger">重置</Button>
          </FormItem>
        </Form>
        <UserManageBar title="详细数据" />
        <div className="treeTable" >
          {roleOptionsArr.length !== 0 ?
            <Tree
              checkable
              checkStrictly
              //defaultExpandAll
              className="orgTree"
              checkedKeys={this.state.checkedKeys}
              onCheck={(checkedKeys, e) => {
                this.onCheck(checkedKeys, e)
              }}
              onSelect={(checkedKeys, e) => { this.onSelect(checkedKeys, e) }}
            >
              {this.renderTreeNodes(roleOptionsArr)}
            </Tree>
            : <div className="ant-table-placeholder">No data</div>
          }
        </div>
      </div>
    );
  }
}
OrgOptionsForm = Form.create({})(OrgOptionsForm)

export default OrgOptionsForm