columnConfig.jsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import React from 'react'
  2. import { Form, Input, Button, Select, Table, Checkbox, Switch, Divider } from 'antd'
  3. import { connect } from 'dva'
  4. import COLUMN_TYPE from './columnType.json'
  5. const FormItem = Form.Item
  6. const SelectOption = Select.Option
  7. class DataSourceColumnConfig extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. }
  12. }
  13. onCheckAllChange() {
  14. }
  15. render() {
  16. const { dataSource, dispatch } = this.props;
  17. const columns = [{
  18. title: <div><Checkbox
  19. style={{ margin: '0 8px 0 0', display: dataSource.newOne.columns ? (dataSource.newOne.columns.length > 0 ? 'inline-block' : 'none') : 'none'}}
  20. 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}
  21. checked={dataSource.newOne.columns ? (dataSource.newOne.columns.filter(c => c.using).length === dataSource.newOne.columns.length) : false}
  22. onChange={(e) => {
  23. let target = e.target;
  24. let columns = dataSource.newOne.columns ? dataSource.newOne.columns.map(c => {
  25. c.using = target.checked;
  26. return c;
  27. }) : [];
  28. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  29. }}
  30. />启用</div>,
  31. dataIndex: 'using',
  32. key: 'using',
  33. width: 50,
  34. render: (v, r) => <Checkbox
  35. dataKey={r.key}
  36. onChange={(e) => {
  37. let target = e.target;
  38. let key = target.dataKey;
  39. let columns = dataSource.newOne.columns.map(c => {
  40. if(c.key === key) {
  41. c.using = target.checked;
  42. }
  43. return c;
  44. });
  45. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  46. }}
  47. checked={v}
  48. />
  49. }, {
  50. title: '列名',
  51. dataIndex: 'name',
  52. key: 'name',
  53. width: 80
  54. }, {
  55. title: '备注',
  56. dataIndex: 'description',
  57. key: 'description',
  58. width: 150,
  59. render: (text, record) => {
  60. return <Input
  61. value={text}
  62. onChange={(e) => {
  63. let columns = dataSource.newOne.columns.map(c => {
  64. if(c.key === record.key) {
  65. c['description'] = e.target.value;
  66. }
  67. return c;
  68. });
  69. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  70. }}
  71. >
  72. </Input>
  73. }
  74. }, {
  75. title: '数据类型',
  76. dataIndex: 'dataType',
  77. key: 'dataType',
  78. width: 80
  79. }, {
  80. title: '分析类型',
  81. dataIndex: 'columnType',
  82. key: 'columnType',
  83. width: 80,
  84. render: (text, record) => {
  85. return (
  86. <Select
  87. style={{ width: '100%' }}
  88. value={text}
  89. onChange={(value) => {
  90. let columns = dataSource.newOne.columns.map(c => {
  91. if(c.key === record.key) {
  92. c.columnType = value;
  93. c.groupable = c.columnType === 'categorical';
  94. c.bucketizable = ['time', 'scale', 'ordinal'].indexOf(record.columnType) !== -1;
  95. }
  96. return c;
  97. });
  98. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  99. }}
  100. >
  101. {
  102. COLUMN_TYPE.map( c => {
  103. let dataType = record.dataType;
  104. if(c.dataType.indexOf(dataType) !== -1) {
  105. return <SelectOption value={c.columnType} key={c.columnType}>{c.label}</SelectOption>
  106. }else {
  107. return null
  108. }
  109. }).filter((s)=>s!==null)
  110. }
  111. </Select>
  112. )
  113. }
  114. }, {
  115. title: '允许分组',
  116. dataIndex: 'groupable',
  117. key: 'groupable',
  118. width: 50,
  119. className: 'column-groupable',
  120. render: (value, record) => <Switch disabled={record.columnType!=='categorical'} checked={value} onChange={(checked) => {
  121. let columns = dataSource.newOne.columns.map(c => {
  122. if(c.key === record.key) {
  123. c.groupable = checked;
  124. }
  125. return c;
  126. });
  127. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  128. }}/>
  129. }, {
  130. title: '允许分段',
  131. dataIndex: 'bucketizable',
  132. key: 'bucketizable',
  133. width: 50,
  134. className: 'column-bucketizable',
  135. render: (value, record) => <Switch
  136. disabled={['time', 'scale', 'ordinal'].indexOf(record.columnType)===-1}
  137. checked={value}
  138. defaultChecked={true}
  139. onChange={(checked) => {
  140. let columns = dataSource.newOne.columns.map(c => {
  141. if(c.key === record.key) {
  142. c.bucketizable = checked;
  143. }
  144. return c;
  145. });
  146. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  147. }}
  148. />
  149. }, {
  150. title: '别名',
  151. dataIndex: 'alias',
  152. key: 'alias',
  153. width: 150,
  154. render: (text, record) => {
  155. return(
  156. <Input
  157. value={text}
  158. onChange={(e) => {
  159. const value = e.target.value;
  160. let columns = dataSource.newOne.columns.map(c => {
  161. if(c.key === record.key) {
  162. c['alias'] = value;
  163. }
  164. return c;
  165. });
  166. dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
  167. }}
  168. >
  169. </Input>
  170. )
  171. }
  172. }];
  173. return (
  174. <div>
  175. {
  176. dataSource.newOne.type==='database'?(
  177. <div>
  178. <Form size='small'>
  179. <Divider orientation="left">数据对象</Divider>
  180. <FormItem className='textarea-target'>
  181. <Input.TextArea
  182. disabled={!dataSource.newOne.address}
  183. placeholder={dataSource.newOne.address ? '输入表名或查询SQL,注意不能以分号结尾' : '请返回上一步选择数据库连接'}
  184. autosize={{ minRows: 3 }}
  185. value={dataSource.newOne.target}
  186. onChange={(e) => {
  187. dispatch({ type: 'dataSource/setNewModelInvalidSQL', value: false });
  188. dispatch({ type: 'dataSource/setNewModelField', name: 'target', value: e.target.value });
  189. }}
  190. />
  191. </FormItem>
  192. <div className='buttons'>
  193. <div className='errormessage' style={{ cursor: dataSource.newOne.invalidSQL ? 'text' : 'default', opacity: dataSource.newOne.invalidSQL ? '1' : '0' }}>未查询到列数据,请检查SQL是否正确</div>
  194. <Button disabled={!dataSource.newOne.address} onClick={() => {
  195. dispatch({ type: 'dataSource/importNewModelColumns' })
  196. }}>获取列数据</Button>
  197. </div>
  198. </Form>
  199. <Divider orientation="left">数据列</Divider>
  200. </div>
  201. ):null
  202. }
  203. <Table
  204. className='table-columnconfig'
  205. dataSource={dataSource.newOne.columns}
  206. columns={columns}
  207. locale={{
  208. emptyText: '未连接到数据对象'
  209. }}
  210. />
  211. </div>
  212. );
  213. }
  214. }
  215. function mapStateToProps({ present: { dataSource, dataConnect } }) {
  216. return { dataSource, dataConnect }
  217. }
  218. export default connect(mapStateToProps)(DataSourceColumnConfig);