modal.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {Modal, Input, Select, message, Table, Button} from 'antd';
  2. import axios from '@/utils/axios.js'
  3. import {useEffect, useState} from 'react'
  4. import './modal.scss'
  5. import {getChild} from '@/utils/getDict.js'
  6. import {useSelector} from "react-redux";
  7. export default function ModalHooks({isModalOpen, closeModal, row,submitValue}) {
  8. const [messageApi, contextHolder] = message.useMessage();
  9. const [tableData, setTableData] = useState([])
  10. let dict = useSelector((state) => state.counter.dictData);
  11. const options = getChild(dict,'consumptionProportion')
  12. // const options = []
  13. /**
  14. *确定时候的回调
  15. */
  16. function handleOk() {
  17. // 验证表单
  18. for (const item of tableData) {
  19. if(!item.name|| !item.value){
  20. messageApi.error('未填写完整,无法提交')
  21. return
  22. }
  23. }
  24. const str = JSON.stringify(tableData)
  25. submitValue(str)
  26. }
  27. /**
  28. * 设置每个input的数据类型
  29. * @param value 数据值
  30. * @param index 下标
  31. * @param dataName 名称
  32. */
  33. function setValue(value,index,dataName){
  34. let newData = JSON.parse(JSON.stringify(tableData))
  35. newData[index][dataName]=value
  36. setTableData(newData)
  37. }
  38. // 关闭页面
  39. const handleCancel = () => {
  40. closeModal(false);
  41. };
  42. // table的数值
  43. const columns = [{
  44. title: '占比次数',
  45. dataIndex: 'name',
  46. render: (text, record, index) => (
  47. <Select
  48. value={text}
  49. onChange={(val)=>setValue(val,index,'name')}
  50. options={options}
  51. style={{width:'80px'}}
  52. fieldNames={{
  53. label:'name',
  54. value:'name'
  55. }}
  56. />
  57. )
  58. }, {
  59. title: '占比次数',
  60. dataIndex: 'value',
  61. render: (text, record,index) => <Input value={text} onChange={(e)=>setValue(e.target.value,index,'value')}></Input>,
  62. },
  63. {
  64. title: '操作',
  65. render: (text, record, index) => (
  66. <Button danger onClick={() => removeRowList(index)}>删除</Button>
  67. )
  68. }
  69. ]
  70. /**
  71. * 删除每一行的数据
  72. * @param index
  73. */
  74. function removeRowList(index) {
  75. let newData = JSON.parse(JSON.stringify(tableData))
  76. newData.splice(index, 1)
  77. setTableData(newData)
  78. }
  79. function add(){
  80. let newData = JSON.parse(JSON.stringify(tableData))
  81. newData.push({
  82. name:null,
  83. value:''
  84. })
  85. setTableData(newData)
  86. }
  87. // 表单请求
  88. useEffect(() => {
  89. let newData = JSON.parse(JSON.stringify(row))
  90. setTableData(newData)
  91. }, [row])
  92. return (
  93. <div className="form">
  94. {contextHolder}
  95. <Modal title="修改数据" open={isModalOpen} onOk={handleOk} onCancel={handleCancel} width={900}
  96. cancelText="取消" okText="确定">
  97. <Button type="primary" style={{marginBottom: '10px'}} onClick={add}>新增占比</Button>
  98. <Table
  99. dataSource={tableData}
  100. pagination={false}
  101. bordered={true}
  102. rowKey="name"
  103. columns={columns}
  104. >
  105. </Table>
  106. </Modal>
  107. </div>
  108. )
  109. }