Skip to content

Commit

Permalink
1、通过父子组件相互控制传值解决表格 删除、禁启用、批量删除等功能
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjunhuan666 committed Aug 5, 2020
1 parent 25fa0a6 commit ef77ebe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/components/TableComp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ class TableComp extends Component {
componentDidMount = () => {
// console.log(requestUrl[this.props.config.url])
this.getDepartmentListFun()
this.props.onRef(this)
}


// 获取部门列表
getDepartmentListFun = () => {
getDepartmentListFun = (data) => {
const { pageNumber, pageSize } = this.state
this.setState({tableLoading: true})
// const data = {
// pageNumber,
// pageSize
// }
// if(name) {
// data.name = name
// }
Expand All @@ -47,6 +44,9 @@ class TableComp extends Component {
pageSize
}
}
if(data){
requestData.data.name = data
}

TableList(requestData).then(res => {
const newData = res.data.data
Expand All @@ -55,18 +55,23 @@ class TableComp extends Component {
tableData: newData.data,
tableLoading: false
})
// 控制父组件serachFlag开关的值
this.props.sonSetSearchFlag()
}

}).catch( err => {
this.setState({tableLoading: false})
// 控制父组件serachFlag开关的值
this.props.sonSetSearchFlag()
})
}
/*
复选框
*/
onCheckBox = (selectedRowKeys) => {
// this.setState({ selectedRowKeys });
console.log(selectedRowKeys)
// console.log("son-selectedRowKeys",selectedRowKeys)
this.props.sonToCheck(selectedRowKeys)
}


Expand Down
6 changes: 6 additions & 0 deletions src/pages/DepartmentAdd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ class AddDepartment extends Component{
})
if(!values.name){
message.info('部门名称不能为空')
this.setState({
loading: false
})
return false
}
if(!values.content){
message.info('请进行相关描述')
this.setState({
loading: false
})
return false
}
const requestAddData = values
Expand Down
40 changes: 37 additions & 3 deletions src/pages/DepartmentList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ class DepartmentList extends Component{
componentDidMount = () => {
// this.getDepartmentListFun()
}
// 父控制子组件
onRef = (ref) => {
this.table = ref
}

//子调用父,控制searchFlag的值
sonSetSearchFlag = () => {
this.setState({
searchFlag: false
})
}

// 子向父传多选selectedRowKeys的值
sonToCheck = (selectedRowKeys) => {
this.setState({
selectedRowKeys
})
}

/*
删除modal框的控制
Expand All @@ -99,6 +117,9 @@ class DepartmentList extends Component{
// console.log(res.data.data)
message.success(res.data.message)
// this.getDepartmentListFun()
//调用子组件更新列表
this.table.getDepartmentListFun()

this.setState({
confirmLoading: false,
visible: false,
Expand All @@ -125,6 +146,8 @@ class DepartmentList extends Component{
pageSize: 10
})
// this.getDepartmentListFun()
//调用子组件更新列表
this.table.getDepartmentListFun(this.state.name)
}


Expand Down Expand Up @@ -153,6 +176,9 @@ class DepartmentList extends Component{
this.setState({switchId: null})
// this.setState({flag: false})
// this.getDepartmentListFun()

//调用子组件更新列表
this.table.getDepartmentListFun()
}).catch(err => {
this.setState({switchId: null})
// this.setState({flag: false})
Expand All @@ -163,10 +189,13 @@ class DepartmentList extends Component{
deleteDep = ({id}) => {
const { selectedRowKeys } = this.state
if(!id){ // 批量删除
if(selectedRowKeys.length === 0) return false;
if(selectedRowKeys.length === 0){
message.info('请选择要删除的数据!')
return false
};
id = selectedRowKeys.join()
}
// console.log(id)
console.log(id)
this.setState({
visible: true,
id
Expand All @@ -186,7 +215,12 @@ class DepartmentList extends Component{
</Form.Item>
</Form>

<TableComp config = {this.state.tableConfig}/>
<TableComp
onRef={this.onRef}
sonToCheck = {this.sonToCheck}
config = {this.state.tableConfig}
sonSetSearchFlag = {this.sonSetSearchFlag}
/>

<Button onClick={this.deleteDep} style={{marginTop:'10px'}}>批量删除</Button>
<Modal
Expand Down

0 comments on commit ef77ebe

Please sign in to comment.