嵌套类型表格API

表格样式

insertTable.png

使用说明

  1. import React, {Component} from 'react';
  2. import {createPage} from 'nc-lightapp-front';
  3. class IcInsertTable extends Component {
  4. constructor(props) {
  5. super(props);
  6. let {form, button, table, insertTable, search} = this.props;
  7. let {getInsertTableValue, setInsertTableValue} = insertTable;
  8. this.getInsertTableValue = getInsertTableValue;//获取嵌套表格当前所有数据
  9. this.setInsertTableValue = setInsertTableValue;//设置表格数据
  10. }
  11. componentWillMount() {
  12. //查询外层表格数据
  13. // Ajax({
  14. // url:'',
  15. // data:'',
  16. // success:function (res) {
  17. //
  18. // }
  19. // });
  20. let data = {
  21. data: [
  22. {a: "令狐冲", b: "男", c: 41, dd: "操作", key: "1"},
  23. {a: "杨过", b: "男", c: 67, dd: "操作", key: "2"},
  24. {a: "郭靖", b: "男", c: 25, dd: "操作", key: "3"},
  25. {a: "令狐冲", b: "男", c: 41, dd: "操作", key: "4"},
  26. {a: "杨过", b: "男", c: 67, dd: "操作", key: "5"},
  27. {a: "郭靖", b: "男", c: 25, dd: "操作", key: "6"},
  28. {a: "令狐冲", b: "男", c: 41, dd: "操作", key: "7"},
  29. {a: "杨过", b: "男", c: 67, dd: "操作", key: "8"},
  30. {a: "郭靖", b: "男", c: 25, dd: "操作", key: "9"}
  31. ],
  32. pageInfo:{
  33. pageIndex:1,
  34. pageSize:5,
  35. totalDataNum:9
  36. }
  37. };
  38. this.setInsertTableValue('insertTable1', data);
  39. }
  40. //保存,获取表格当前数据
  41. saveData = () => {
  42. let val = this.getInsertTableValue('insertTable1');
  43. console.log(val)
  44. };
  45. // react:界面渲染函数
  46. render() {
  47. let {form, button, table, insertTable, search} = this.props;
  48. let {createButton} = button;
  49. let {createInsertTable, getInsertTableValue} = insertTable;
  50. return (
  51. <div className="insertTablePage" style={{"margin": "30px"}}>
  52. <div>嵌套类型表格</div>
  53. <div className="btnArea">
  54. <NCButton className="saveBtn" colors="primary" onClick={ this.saveData.bind(this) }>保存</NCButton>
  55. </div>
  56. { createInsertTable(
  57. 'insertTable1', //表格id,支持一个页面多表格
  58. setTableBodyData.bind(this), //点击加号展开,设置表格数据
  59. pageIndexChange.bind(this), //点击分页时,设置表格数据(不用分页,可不传)
  60. pageSizeChange.bind(this) //切换显示条数,重新请求数据(不用切换pageSize,可不传)
  61. )}
  62. </div>
  63. );
  64. }
  65. }
  66. export default createPage({
  67. initTemplate:addOperationColumn//添加页面模板信息
  68. })(IcInsertTable);
  69. //添加页面模板信息
  70. export default function addOperationColumn(meta) {
  71. let metas = {
  72. searchArea: {
  73. moduleType: 'search',
  74. items: [{
  75. key: 'bankname',
  76. label: '银行',
  77. itemType: 'refer',
  78. initialValue: null
  79. },
  80. {
  81. key: 'investtype',
  82. label: '存款类型',
  83. itemType: 'select',
  84. initialValue: null,
  85. options: [{
  86. display: '活期',
  87. value: 0
  88. },
  89. {
  90. display: '三个月',
  91. value: 1
  92. },
  93. {
  94. display: '半年',
  95. value: 2
  96. },
  97. {
  98. display: '三年',
  99. value: 3
  100. },
  101. {
  102. display: '五年',
  103. value: 4
  104. }
  105. ]
  106. },
  107. {
  108. key: 'beginmny',
  109. label: '起始理财金额',
  110. itemType: 'input',
  111. initialValue: null
  112. },
  113. {
  114. key: 'endmny',
  115. label: '截止理财金额',
  116. itemType: 'input',
  117. initialValue: null
  118. },
  119. {
  120. key: 'begindate',
  121. label: '起始购买日期',
  122. itemType: 'datepicker',
  123. initialValue: null
  124. },
  125. {
  126. key: 'enddate',
  127. label: '截止购买日期',
  128. itemType: 'datepicker',
  129. initialValue: null
  130. }
  131. ]
  132. },
  133. insertTable: {
  134. insertTable1: {
  135. // pagination: false,
  136. columns: [
  137. {
  138. title: '用户名',
  139. dataIndex: 'a',
  140. key: 'a',
  141. width: 100
  142. },
  143. {
  144. id: '123',
  145. title: '性别',
  146. dataIndex: 'b',
  147. key: 'b',
  148. width: 100
  149. },
  150. {
  151. title: '年龄',
  152. dataIndex: 'c',
  153. key: 'c',
  154. width: 200
  155. },
  156. {
  157. title: "操作",
  158. dataIndex: "d",
  159. key: "d",
  160. render(text, record, index) {
  161. if(record.a == "令狐冲"){
  162. return (
  163. <span
  164. onClick={() => {
  165. alert("这是第" + index + "列,内容为:" + JSON.stringify(record));
  166. }}
  167. >
  168. 新增
  169. </span>
  170. );
  171. }else{
  172. return (
  173. <span
  174. onClick={() => {
  175. alert("这是第" + index + "列,内容为:" + JSON.stringify(record));
  176. }}
  177. >
  178. 修改
  179. </span>
  180. );
  181. }
  182. }
  183. }
  184. ]
  185. }
  186. },
  187. }
  188. return metas
  189. }
  190. //点击加号展开内嵌表格时,业务组请求表格数据,并且返回该数据
  191. export default function setTableBodyData(record) { //record为点击的当前行信息
  192. //发送ajax请求内嵌表格数据,并return该数据
  193. let insertTableInfo = {};
  194. //内嵌表格列信息
  195. insertTableInfo.columns = [
  196. {
  197. title: "用户名", dataIndex: "a", key: "a", width: 100
  198. },
  199. {id: "123", title: "性别", dataIndex: "b", key: "b", width: 100},
  200. {title: "年龄", dataIndex: "c", key: "c", width: 200},
  201. ];
  202. //内嵌表格数据信息
  203. if (record.a == "杨过") {
  204. //假数据
  205. insertTableInfo.data = [
  206. {a: "麻花藤", b: "男", c: 41, d: "操作", key: "1"}
  207. ]
  208. } else {
  209. insertTableInfo.data =
  210. [
  211. {a: "麻花藤", b: "男", c: 41, d: "操作", key: "1"},
  212. {a: "麻花藤", b: "男", c: 41, d: "操作", key: "2"}
  213. ]
  214. }
  215. return insertTableInfo
  216. };
  217. //点击分页按钮时,请求下一页数据,并且返回该数据
  218. export default function pageIndexChange(index) {
  219. let data = {
  220. pageIndex:index
  221. };
  222. let setInsertTableValue = this.setInsertTableValue;
  223. Ajax({
  224. url:'',
  225. data:data,
  226. success:function (res) {
  227. if(res.state){
  228. setInsertTableValue('insertTable1',res.data)
  229. }
  230. }
  231. })
  232. }
  233. //切换每页显示条数时,重新请求数据,并且返回
  234. export default function pageSizeChange(pageSize){}同上
API 参数 说明
createInsertTable(id,setTableBodyData,pageIndexChange,pageSizeChange) 1、id(表格自定义id)
2、setTableBodyData(点击加号展开内嵌表格时,回调函数)
3、pageIndexChange(切换分页时,回调函数)
4、pageSizeChange(切换显示条数,回调函数)
创建嵌套类表格
setInsertTableValue(id,newData) 1、id(表格id)
2、newData(新数据)

| 设置嵌套类表格数据 | | getInsertTableValue(id) | 1、id(表格id)

| 获取当前表格所有数据 |