columnConfig.jsx 9.1 KB

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