columnConfig.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import React from 'react'
  2. import { Form, Input, Button, Select, Table, Checkbox, Divider, Icon, Popconfirm } from 'antd'
  3. import { connect } from 'dva'
  4. import COLUMN_TYPE from './columnType.json'
  5. import { Resizable } from 'react-resizable';
  6. const FormItem = Form.Item
  7. const SelectOption = Select.Option
  8. const ResizeableTitle = (props) => {
  9. const { onResize, width, ...restProps } = props;
  10. if (!width) {
  11. return <th {...restProps} />;
  12. }
  13. return (
  14. <Resizable width={width} height={0} onResize={onResize}>
  15. <th {...restProps} />
  16. </Resizable>
  17. );
  18. };
  19. class DataSourceColumnConfig extends React.Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. widths: [ 50, 100, 150, 100, 150, 200 ],
  24. visibleConfirm: false
  25. }
  26. }
  27. components = {
  28. header: {
  29. cell: ResizeableTitle,
  30. },
  31. };
  32. handleVisibleChange = (visible) => {
  33. const { columns } = this.props.dataSource.newOne;
  34. this.setState({ visibleConfirm: visible && (columns && columns.length > 0) });
  35. }
  36. handleResize = index => (e, { size }) => {
  37. this.setState(({ widths }) => {
  38. const nextWidths = [...widths];
  39. nextWidths[index] = size.width;
  40. return { widths: nextWidths };
  41. });
  42. };
  43. render() {
  44. const { dataSource, dispatch, loading } = this.props;
  45. const { widths } = this.state;
  46. const columns = [{
  47. title: <div><Checkbox
  48. style={{ margin: '0 8px 0 0', display: dataSource.newOne.columns ? (dataSource.newOne.columns.length > 0 ? 'inline-block' : 'none') : 'none'}}
  49. indeterminate={dataSource.newOne.columns ? (dataSource.newOne.columns.filter(c => c.using).length > 0 && dataSource.newOne.columns.filter(c => c.using).length < dataSource.newOne.columns.length) : false}
  50. checked={dataSource.newOne.columns ? (dataSource.newOne.columns.filter(c => c.using).length === dataSource.newOne.columns.length) : false}
  51. onChange={(e) => {
  52. let target = e.target;
  53. let columns = dataSource.newOne.columns ? dataSource.newOne.columns.map(c => {
  54. c.using = target.checked;
  55. return c;
  56. }) : [];
  57. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  58. }}
  59. />启用</div>,
  60. dataIndex: 'using',
  61. key: 'using',
  62. width: widths[0],
  63. render: (v, r) => <Checkbox
  64. dataKey={r.key}
  65. onChange={(e) => {
  66. let target = e.target;
  67. let key = target.dataKey;
  68. let columns = dataSource.newOne.columns.map(c => {
  69. if(c.key === key) {
  70. c.using = target.checked;
  71. }
  72. return c;
  73. });
  74. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  75. }}
  76. checked={v}
  77. />
  78. }, {
  79. title: '列名',
  80. dataIndex: 'name',
  81. key: 'name',
  82. width: widths[1],
  83. }, {
  84. title: '备注',
  85. dataIndex: 'description',
  86. key: 'description',
  87. width: widths[2],
  88. }, {
  89. title: '数据类型',
  90. dataIndex: 'dataType',
  91. key: 'dataType',
  92. width: widths[3]
  93. }, {
  94. title: '分析类型',
  95. dataIndex: 'columnType',
  96. key: 'columnType',
  97. width: widths[4],
  98. render: (text, record) => {
  99. return (
  100. <Select
  101. style={{ width: '100%' }}
  102. value={text}
  103. onChange={(value) => {
  104. let columns = dataSource.newOne.columns.map(c => {
  105. if(c.key === record.key) {
  106. c.columnType = value;
  107. c.groupable = c.columnType === 'categorical';
  108. c.bucketizable = ['time', 'scale', 'ordinal'].indexOf(record.columnType) !== -1;
  109. }
  110. return c;
  111. });
  112. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  113. }}
  114. >
  115. {
  116. COLUMN_TYPE.map( c => {
  117. let dataType = record.dataType;
  118. if(c.dataType.indexOf(dataType) !== -1) {
  119. return <SelectOption value={c.columnType} key={c.columnType}>{c.label}</SelectOption>
  120. }else {
  121. return null
  122. }
  123. }).filter((s)=>s!==null)
  124. }
  125. </Select>
  126. )
  127. }
  128. // }, {
  129. // title: '允许分组',
  130. // dataIndex: 'groupable',
  131. // key: 'groupable',
  132. // width: 50,
  133. // className: 'column-groupable',
  134. // render: (value, record) => <Switch disabled={record.columnType!=='categorical'} checked={value} onChange={(checked) => {
  135. // let columns = dataSource.newOne.columns.map(c => {
  136. // if(c.key === record.key) {
  137. // c.groupable = checked;
  138. // }
  139. // return c;
  140. // });
  141. // dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  142. // }}/>
  143. // }, {
  144. // title: '允许分段',
  145. // dataIndex: 'bucketizable',
  146. // key: 'bucketizable',
  147. // width: 50,
  148. // className: 'column-bucketizable',
  149. // render: (value, record) => <Switch
  150. // disabled={['time', 'scale', 'ordinal'].indexOf(record.columnType)===-1}
  151. // checked={value}
  152. // defaultChecked={true}
  153. // onChange={(checked) => {
  154. // let columns = dataSource.newOne.columns.map(c => {
  155. // if(c.key === record.key) {
  156. // c.bucketizable = checked;
  157. // }
  158. // return c;
  159. // });
  160. // dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  161. // }}
  162. // />
  163. }, {
  164. title: '别名',
  165. dataIndex: 'alias',
  166. key: 'alias',
  167. // width: widths[5],
  168. render: (text, record) => {
  169. return(
  170. <Input
  171. value={text}
  172. placeholder={record.description ? record.description.substring(0, 10) : record.name}
  173. onChange={(e) => {
  174. const value = e.target.value;
  175. let columns = dataSource.newOne.columns.map(c => {
  176. if(c.key === record.key) {
  177. c['alias'] = value;
  178. }
  179. return c;
  180. });
  181. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  182. }}
  183. >
  184. </Input>
  185. )
  186. }
  187. // }, {
  188. // title: '允许过滤',
  189. // dataIndex: 'filterable',
  190. // key: 'filterable',
  191. // render: (value, record) => <Switch />
  192. }].map((col, index) => ({
  193. ...col,
  194. onHeaderCell: column => ({
  195. width: column.width,
  196. onResize: this.handleResize(index),
  197. }),
  198. }));
  199. return (
  200. <div>
  201. {
  202. dataSource.newOne.type==='database'?(
  203. <div>
  204. <Form size='small'>
  205. <Divider orientation="left">数据对象</Divider>
  206. <FormItem className='textarea-target'>
  207. <Input.TextArea
  208. disabled={!dataSource.newOne.address}
  209. placeholder={dataSource.newOne.address ? '输入表名或查询SQL,注意不能以分号结尾' : '请返回上一步选择数据库连接'}
  210. autosize={{ minRows: 3 }}
  211. value={dataSource.newOne.target}
  212. onChange={(e) => {
  213. dispatch({ type: 'dataSource/setNewModelInvalidSQL', value: false });
  214. dispatch({ type: 'dataSource/setNewModelField', name: 'target', value: e.target.value });
  215. }}
  216. />
  217. </FormItem>
  218. <div className='buttons'>
  219. <div className='errormessage' style={{ cursor: dataSource.newOne.invalidSQL ? 'text' : 'default', opacity: dataSource.newOne.invalidSQL ? '1' : '0' }}>未查询到列数据,请检查SQL是否正确</div>
  220. <Popconfirm
  221. title="已存在列数据,确定要覆盖吗?"
  222. visible={this.state.visibleConfirm}
  223. onVisibleChange={this.handleVisibleChange}
  224. onConfirm={() => {
  225. this.setState({
  226. visibleConfirm: false
  227. });
  228. dispatch({ type: 'dataSource/importNewModelColumns' });
  229. }}
  230. onCancel={() => {
  231. this.setState({
  232. visibleConfirm: false
  233. });
  234. }}
  235. okText="确定"
  236. cancelText="取消"
  237. >
  238. <Button disabled={!dataSource.newOne.address || loading.models.dataSource} onClick={() => {
  239. if(!dataSource.newOne.columns || dataSource.newOne.columns.length === 0) {
  240. dispatch({ type: 'dataSource/importNewModelColumns' });
  241. }
  242. }}>
  243. {
  244. loading.models.dataSource ? <Icon type="loading" /> : ''
  245. }
  246. {'获取数据列'}
  247. </Button>
  248. </Popconfirm>
  249. </div>
  250. </Form>
  251. <Divider orientation="left">数据列</Divider>
  252. </div>
  253. ):null
  254. }
  255. <Table
  256. className='table-columnconfig'
  257. bordered
  258. components={this.components}
  259. dataSource={dataSource.newOne.columns}
  260. columns={columns}
  261. locale={{
  262. emptyText: '未连接到数据对象'
  263. }}
  264. />
  265. </div>
  266. );
  267. }
  268. }
  269. function mapStateToProps({ present: { dataSource, dataConnect, loading } }) {
  270. return { dataSource, dataConnect, loading }
  271. }
  272. export default connect(mapStateToProps)(DataSourceColumnConfig);