当前位置:   article > 正文

Vue+ElementUI中表格嵌套的使用方法_element表格展开嵌套表格

element表格展开嵌套表格

需求:在开发中会遇到很多表格嵌套表格的使用,一个父级表格通过展开行要打开子级的表格,如果利用官网中的展开行的方式去实现其实是有点困难的

  1. 首先实现行展开,这个是用到了elementUI中的一个属性通过设置 type="expand" 和 Scoped slot 可以开启展开行功能,el-table-column 的模板会被渲染成为展开行的内容,展开行可访问的属性与使用自定义列模板时的 Scoped slot 相同。代码如下

  1. <el-table-column type="expand" class="childtable">
  2. <template slot-scope="props">
  3. <el-table
  4. ref="multipleTable"
  5. :data="props.row.sonData"
  6. style="width: 100%; margin-left: 0"
  7. max-height="360"
  8. :row-style="rowStyle"
  9. :header-cell-style="rowStyle"
  10. :cell-style="rowStyle"
  11. >
  12. <el-table-column width="55">
  13. <template slot-scope="scope">
  14. <el-checkbox v-model="scope.row.ischecked" @change="changeVal(scope.row)"></el-checkbox>
  15. </template>
  16. </el-table-column>
  17. <el-table-column prop="xuhao" label="序号"> </el-table-column>
  18. <el-table-column
  19. prop="name"
  20. label="姓名"
  21. :filters="fiterNameData"
  22. :filter-method="filterTag"
  23. filter-placement="bottom-end"
  24. >
  25. </el-table-column>
  26. <el-table-column prop="info" label="专业"> </el-table-column>
  27. <el-table-column
  28. prop="sfz"
  29. label="身份证号"
  30. :filters="fiterNumberData"
  31. :filter-method="fiterNumber"
  32. filter-placement="bottom-end"
  33. >
  34. </el-table-column>
  35. <el-table-column
  36. prop="phone"
  37. label="联系电话"
  38. :filters="fiterPhoneData"
  39. :filter-method="fiterPhone"
  40. filter-placement="bottom-end"
  41. >
  42. </el-table-column>
  43. </el-table>
  44. </template>
  45. </el-table-column>

2.在data中声明一个数组,用来渲染表格的展示,数据可以类似于树形结构的模式来整理

  1. tableData: [
  2. //外层table的数据
  3. {
  4. id: "230211181611f0bWSLJG7copryh9Wv0",
  5. xuhao: 1,
  6. sameId: 1,
  7. zyml: "信息安全",
  8. yjyz: "信息安全",
  9. ejzy: "大数据安全研究",
  10. sjzy: "大数据管理",
  11. zylb: "组长级",
  12. addr: "南昌市",
  13. type: "遴选",
  14. number: 20,
  15. paixu:1,
  16. sonData: [
  17. //把要嵌套的内层table数据,放在父级table的一个字段里
  18. {
  19. cid: "fthgs",
  20. sameId: 1,
  21. name: "陈涛",
  22. info: "信息安全",
  23. sfz: "612454779799",
  24. phone: "134154647",
  25. ischecked: false,
  26. paixu:1,
  27. xuhao: 1,
  28. },
  29. {
  30. cid: "trh",
  31. sameId: 1,
  32. name: "张三",
  33. info: "信息安全",
  34. sfz: "4564648979789",
  35. phone: "46497987",
  36. ischecked: false,
  37. paixu:1,
  38. xuhao: 2,
  39. },
  40. {
  41. cid: "5823435",
  42. sameId: 1,
  43. name: "李四",
  44. info: "信息安全",
  45. sfz: "666797964665",
  46. phone: "4687899999999",
  47. ischecked: false,
  48. paixu:1,
  49. xuhao: 3,
  50. },
  51. ],
  52. },
  53. ],

3.因为子级的每一行的数据是单选的模式,我这里用了el-checkbox去实现的,所以在子级里面绑定了一个字段ischecked去控制,选中还是取消选中

  1. <el-table-column width="55">
  2. <template slot-scope="scope">
  3. <el-checkbox v-model="scope.row.ischecked" @change="changeVal(scope.row)"></el-checkbox>
  4. </template>
  5. </el-table-column>

4.表格默认进入展开第一行,这个可以在父级的table身上绑定一个ref属性,在初始化的时候默认展开第一行

  1. this.$nextTick(() => {
  2. this.$refs.isExpand.toggleRowExpansion(this.tableData[0], true);
  3. });

最终的实现效果;

附上完整代码

  1. <template>
  2. <div>
  3. <div class="bg">
  4. <div class="lxbox">
  5. <!-- 头部 -->
  6. <div class="top">
  7. <span class="title">遴选</span>
  8. <div class="img"></div>
  9. </div>
  10. <!-- 中间表格 -->
  11. <div class="lvtables">
  12. <el-table :data="tableData" style="width: 100%" ref="isExpand" max-height="860">
  13. <el-table-column type="expand" class="childtable">
  14. <template slot-scope="props">
  15. <el-table
  16. ref="multipleTable"
  17. :data="props.row.sonData"
  18. style="width: 100%; margin-left: 0"
  19. max-height="360"
  20. :row-style="rowStyle"
  21. :header-cell-style="rowStyle"
  22. :cell-style="rowStyle"
  23. >
  24. <el-table-column width="55">
  25. <template slot-scope="scope">
  26. <el-checkbox v-model="scope.row.ischecked" @change="changeVal(scope.row)"></el-checkbox>
  27. </template>
  28. </el-table-column>
  29. <el-table-column prop="xuhao" label="序号"> </el-table-column>
  30. <el-table-column
  31. prop="name"
  32. label="姓名"
  33. :filters="fiterNameData"
  34. :filter-method="filterTag"
  35. filter-placement="bottom-end"
  36. >
  37. </el-table-column>
  38. <el-table-column prop="info" label="专业"> </el-table-column>
  39. <el-table-column
  40. prop="sfz"
  41. label="身份证号"
  42. :filters="fiterNumberData"
  43. :filter-method="fiterNumber"
  44. filter-placement="bottom-end"
  45. >
  46. </el-table-column>
  47. <el-table-column
  48. prop="phone"
  49. label="联系电话"
  50. :filters="fiterPhoneData"
  51. :filter-method="fiterPhone"
  52. filter-placement="bottom-end"
  53. >
  54. </el-table-column>
  55. </el-table>
  56. </template>
  57. </el-table-column>
  58. <el-table-column prop="xuhao" label="序号"></el-table-column>
  59. <el-table-column prop="zyml" label="专业门类"></el-table-column>
  60. <el-table-column prop="yjyz" label="一级专业"></el-table-column>
  61. <el-table-column prop="ejzy" label="二级专业"></el-table-column>
  62. <el-table-column prop="sjzy" label="三级专业"></el-table-column>
  63. <el-table-column prop="zylb" label="专业类别"></el-table-column>
  64. <el-table-column prop="addr" label="常住工作地"></el-table-column>
  65. <el-table-column prop="type" label="组建方式"></el-table-column>
  66. <el-table-column prop="number" label="可抽取人数"></el-table-column>
  67. </el-table>
  68. </div>
  69. <!-- 底部按钮 -->
  70. <div class="bottom">
  71. <el-button plain>取消</el-button>
  72. <el-button type="primary" @click="submit">确定</el-button>
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. </template>
  78. <script>
  79. export default {
  80. data() {
  81. return {
  82. tableData: [
  83. //外层table的数据
  84. {
  85. id: "230211181611f0bWSLJG7copryh9Wv0",
  86. xuhao: 1,
  87. sameId: 1,
  88. zyml: "信息安全",
  89. yjyz: "信息安全",
  90. ejzy: "大数据安全研究",
  91. sjzy: "大数据管理",
  92. zylb: "组长级",
  93. addr: "南昌市",
  94. type: "遴选",
  95. number: 20,
  96. paixu:1,
  97. sonData: [
  98. //把要嵌套的内层table数据,放在父级table的一个字段里
  99. {
  100. cid: "fthgs",
  101. sameId: 1,
  102. name: "陈涛",
  103. info: "信息安全",
  104. sfz: "612454779799",
  105. phone: "134154647",
  106. ischecked: false,
  107. paixu:1,
  108. xuhao: 1,
  109. },
  110. {
  111. cid: "trh",
  112. sameId: 1,
  113. name: "张三",
  114. info: "信息安全",
  115. sfz: "4564648979789",
  116. phone: "46497987",
  117. ischecked: false,
  118. paixu:1,
  119. xuhao: 2,
  120. },
  121. {
  122. cid: "5823435",
  123. sameId: 1,
  124. name: "李四",
  125. info: "信息安全",
  126. sfz: "666797964665",
  127. phone: "4687899999999",
  128. ischecked: false,
  129. paixu:1,
  130. xuhao: 3,
  131. },
  132. {
  133. cid: "frgvsdfg",
  134. sameId: 1,
  135. name: "王五",
  136. info: "信息安全",
  137. sfz: "156496797",
  138. phone: "65444478788",
  139. ischecked: false,
  140. paixu:1,
  141. xuhao: 4,
  142. },
  143. ],
  144. },
  145. {
  146. id: "230211181611f0bWSLJG7cop156454ryh9Wv0",
  147. xuhao: 2,
  148. sameId: 2,
  149. zyml: "信息安全",
  150. yjyz: "信息安全",
  151. ejzy: "大数据安全研究",
  152. sjzy: "大数据管理",
  153. zylb: "组长级",
  154. addr: "南昌市",
  155. type: "遴选",
  156. number: 20,
  157. paixu:2,
  158. sonData: [
  159. //把要嵌套的内层table数据,放在父级table的一个字段里
  160. {
  161. cid: "gdfgvdsfg",
  162. sameId: 2,
  163. name: "陈涛",
  164. info: "信息安全",
  165. sfz: "612454779799",
  166. phone: "134154647",
  167. ischecked: false,
  168. paixu:2,
  169. xuhao: 1,
  170. },
  171. {
  172. cid: "rdgrvfdsfgv",
  173. sameId: 2,
  174. name: "张三",
  175. info: "信息安全",
  176. sfz: "4564648979789",
  177. phone: "46497987",
  178. ischecked: false,
  179. paixu:2,
  180. xuhao: 2,
  181. },
  182. {
  183. cid: "gfhsd",
  184. sameId: 2,
  185. name: "李四",
  186. info: "信息安全",
  187. sfz: "666797964665",
  188. phone: "4687899999999",
  189. ischecked: false,
  190. paixu:2,
  191. xuhao: 3,
  192. },
  193. {
  194. cid: "hfdgvsd",
  195. sameId: 2,
  196. name: "王五",
  197. info: "信息安全",
  198. sfz: "156496797",
  199. phone: "65444478788",
  200. ischecked: false,
  201. paixu:2,
  202. xuhao: 4,
  203. },
  204. ],
  205. },
  206. {
  207. id: "dasdsda14564ad9s9ad4",
  208. xuhao: 3,
  209. sameId: 3,
  210. zyml: "信息安全",
  211. yjyz: "信息安全",
  212. ejzy: "大数据安全研究",
  213. sjzy: "大数据管理",
  214. zylb: "组长级",
  215. addr: "南昌市",
  216. type: "遴选",
  217. number: 20,
  218. paixu:3,
  219. sonData: [
  220. //把要嵌套的内层table数据,放在父级table的一个字段里
  221. {
  222. cid: "fgvsdgsefvcdfxghrtf",
  223. sameId: 3,
  224. name: "陈涛",
  225. info: "信息安全",
  226. sfz: "612454779799",
  227. phone: "134154647",
  228. ischecked: false,
  229. paixu:3,
  230. xuhao: 1,
  231. },
  232. {
  233. cid: "25543274258",
  234. sameId: 3,
  235. name: "张三",
  236. info: "信息安全",
  237. sfz: "4564648979789",
  238. phone: "46497987",
  239. ischecked: false,
  240. paixu:3,
  241. xuhao: 2,
  242. },
  243. {
  244. cid: "rtfefbhdfxrtfg34 ",
  245. sameId: 3,
  246. name: "李四",
  247. info: "信息安全",
  248. sfz: "666797964665",
  249. phone: "4687899999999",
  250. ischecked: false,
  251. paixu:3,
  252. xuhao: 3,
  253. },
  254. {
  255. cid: "47527ddfxgbrf",
  256. sameId: 3,
  257. name: "王五",
  258. info: "信息安全",
  259. sfz: "156496797",
  260. phone: "65444478788",
  261. ischecked: false,
  262. paixu:3,
  263. xuhao: 4,
  264. },
  265. ],
  266. },
  267. {
  268. id: "djaoud9q7y98ye2oh3r",
  269. xuhao: 4,
  270. sameId: 4,
  271. zyml: "信息安全",
  272. yjyz: "信息安全",
  273. ejzy: "大数据安全研究",
  274. sjzy: "大数据管理",
  275. zylb: "组长级",
  276. addr: "南昌市",
  277. type: "遴选",
  278. number: 20,
  279. paixu:4,
  280. sonData: [
  281. //把要嵌套的内层table数据,放在父级table的一个字段里
  282. {
  283. cid: "452574dgvdbvdfc",
  284. sameId: 4,
  285. name: "陈涛",
  286. info: "信息安全",
  287. sfz: "612454779799",
  288. phone: "134154647",
  289. ischecked: false,
  290. paixu:4,
  291. xuhao: 1,
  292. },
  293. {
  294. cid: "kifdhgoiuy89jkh98yugb",
  295. sameId: 4,
  296. name: "张三",
  297. info: "信息安全",
  298. sfz: "4564648979789",
  299. phone: "46497987",
  300. ischecked: false,
  301. paixu:4,
  302. xuhao: 2,
  303. },
  304. {
  305. cid: "nmkjsfhdf87ye74gfhkdvgcfyu",
  306. sameId: 4,
  307. name: "李四",
  308. info: "信息安全",
  309. sfz: "666797964665",
  310. phone: "4687899999999",
  311. ischecked: false,
  312. paixu:4,
  313. xuhao: 3,
  314. },
  315. {
  316. cid: "njuifewhdsiuy798gbug",
  317. sameId: 4,
  318. name: "王五",
  319. info: "信息安全",
  320. sfz: "156496797",
  321. phone: "65444478788",
  322. ischecked: false,
  323. paixu:4,
  324. xuhao: 4,
  325. },
  326. ],
  327. },
  328. ],
  329. // 姓名筛选
  330. fiterNameData: [
  331. {
  332. text: "陈涛",
  333. value: "陈涛",
  334. },
  335. {
  336. text: "张三",
  337. value: "张三",
  338. },
  339. {
  340. text: "李四",
  341. value: "李四",
  342. },
  343. {
  344. text: "王五",
  345. value: "王五",
  346. },
  347. ],
  348. // 身份证筛选
  349. fiterNumberData: [
  350. {
  351. text: "612454779799",
  352. value: "612454779799",
  353. },
  354. {
  355. text: "4564648979789",
  356. value: "4564648979789",
  357. },
  358. {
  359. text: "666797964665",
  360. value: "666797964665",
  361. },
  362. {
  363. text: "156496797",
  364. value: "156496797",
  365. },
  366. ],
  367. // 联系电话筛选
  368. fiterPhoneData: [
  369. {
  370. text: "134154647",
  371. value: "134154647",
  372. },
  373. {
  374. text: "46497987",
  375. value: "46497987",
  376. },
  377. {
  378. text: "4687899999999",
  379. value: "4687899999999",
  380. },
  381. {
  382. text: "65444478788",
  383. value: "65444478788",
  384. },
  385. ],
  386. //多选的值
  387. father: [],
  388. son: [],
  389. };
  390. },
  391. methods: {
  392. // 调整行样式
  393. rowStyle({ row, rowIndex }) {
  394. let bgcolor = {
  395. background: "#f1f2f3",
  396. borderBottom: "1px solid #d9d9d9",
  397. };
  398. return bgcolor;
  399. },
  400. // 过滤姓名
  401. filterTag(value, row) {
  402. return row.name === value;
  403. },
  404. // 过滤身份证
  405. fiterNumber(value, row) {
  406. return row.operation === value;
  407. },
  408. // 过滤电话号码
  409. fiterPhone(value, row) {
  410. return row.phone === value;
  411. },
  412. changeVal(val){
  413. if(val.ischecked==true){
  414. this.handleSelectionChange(val)
  415. }else{
  416. this.son.forEach((item,index)=>{
  417. if(val.cid==item.cid){
  418. this.son.splice(index,1)
  419. this.father.splice(index,1)
  420. }
  421. })
  422. }
  423. },
  424. // 单选框的选中值
  425. handleSelectionChange(val) {
  426. let that=this
  427. this.tableData.forEach((item) => {
  428. if(item.sonData && val.sameId==item.sameId){
  429. let index=that.father.findIndex((item3)=>{
  430. return item3.sameId==val.sameId
  431. })
  432. if(index>=0){
  433. that.father.splice(index,1)
  434. }
  435. that.father.push(item)
  436. item.sonData.forEach((item2)=>{
  437. if(item2.cid!=val.cid){
  438. item2.ischecked = false;
  439. }else if(item2.cid==val.cid){
  440. let index2=that.son.findIndex((item4)=>{
  441. return item4.sameId==item2.sameId
  442. })
  443. if(index>=0){
  444. that.son.splice(index2,1)
  445. }
  446. that.son.push(item2)
  447. }
  448. })
  449. }
  450. });
  451. },
  452. // 点击确定按钮
  453. submit() {
  454. if (this.tableData.length==this.father.length && this.tableData.length==this.son.length) {
  455. this.$message({
  456. message: "恭喜你,提交成功",
  457. type: "success",
  458. });
  459. } else {
  460. this.$message({
  461. message: "您还没有完成选中信息哦!!!",
  462. type: "warning",
  463. });
  464. }
  465. },
  466. },
  467. mounted() {
  468. this.$nextTick(() => {
  469. this.$refs.isExpand.toggleRowExpansion(this.tableData[0], true);
  470. });
  471. },
  472. };
  473. </script>
  474. <style lang="scss">
  475. @import "../style/util.scss";
  476. .el-table-filter {
  477. // background-color: pink;
  478. width: vw(180) !important;
  479. height: vh(160) !important;
  480. padding-right: vw(5);
  481. // left: vw(720) !important;
  482. .el-checkbox__inner {
  483. width: vw(20);
  484. height: vh(20);
  485. }
  486. .el-checkbox__input.is-checked .el-checkbox__inner::after {
  487. width: vw(8);
  488. height: vh(8);
  489. left: vw(4);
  490. top: vh(2);
  491. }
  492. .el-table-filter__wrap {
  493. max-height: vh(100);
  494. padding: vw(10);
  495. }
  496. .el-table-filter__bottom {
  497. // padding: vw(5)
  498. }
  499. .el-table-filter__bottom button {
  500. font-size: vw(18);
  501. padding: vw(10);
  502. }
  503. .el-table-filter__checkbox-group label.el-checkbox {
  504. height: vh(30);
  505. }
  506. }
  507. </style>
  508. <style lang="scss" scoped>
  509. @import "../style/util.scss";
  510. .bg {
  511. width: 100vw;
  512. height: 100vh;
  513. // background: rgba(0, 0, 0, 0.3);
  514. position: relative;
  515. user-select: none;
  516. .lxbox {
  517. // width: vw(1471);
  518. width: 100%;
  519. height: vh(792);
  520. height: 100vh;
  521. box-sizing: border-box;
  522. background: #ffffff;
  523. border-radius: 0px 0 vw(4) vw(4);
  524. position: absolute;
  525. // top: 50%;
  526. left: 50%;
  527. transform: translateX(-50%);
  528. .top {
  529. width: vw(1471);
  530. width: 100%;
  531. height: vh(55);
  532. background: #fff;
  533. display: flex;
  534. justify-content: space-between;
  535. padding: vh(12) vw(24);
  536. box-sizing: border-box;
  537. border-bottom: 1px solid rgba(0, 0, 0, 0.09);
  538. .title {
  539. font-family: "PingFangSC-Medium";
  540. font-size: vw(16);
  541. color: rgba(0, 0, 0, 0.85);
  542. line-height: vh(24);
  543. font-weight: 600;
  544. }
  545. .img {
  546. width: vw(24);
  547. height: vh(24);
  548. background: url("../assets/close.png") no-repeat;
  549. background-size: contain;
  550. }
  551. }
  552. .lvtables {
  553. width: vw(1471);
  554. width: 100%;
  555. height: vh(690);
  556. height: vh(900);
  557. overflow: auto;
  558. padding: 0 vw(24);
  559. box-sizing: border-box;
  560. &::-webkit-scrollbar {
  561. width: 0;
  562. }
  563. }
  564. .bottom {
  565. width: vw(1471);
  566. width: 100%;
  567. position: absolute;
  568. bottom: 0;
  569. height: vh(55);
  570. background: #fff;
  571. border-top: 1px solid rgba(0, 0, 0, 0.09);
  572. display: flex;
  573. padding-right: vw(32);
  574. box-sizing: border-box;
  575. justify-content: flex-end;
  576. align-items: center;
  577. }
  578. }
  579. >>> .el-button {
  580. width: vw(88);
  581. height: vh(32);
  582. font-size: vw(16);
  583. }
  584. >>> .el-button--default {
  585. margin-right: vw(14);
  586. color: rgba(0, 0, 0, 0.65);
  587. font-weight: 400;
  588. height: vh(38);
  589. line-height: vh(35);
  590. }
  591. >>> .el-button--primary {
  592. font-weight: 400;
  593. height: vh(38);
  594. line-height: vh(35);
  595. }
  596. >>> .el-checkbox__inner {
  597. width: vw(22);
  598. height: vh(22);
  599. }
  600. >>> .el-checkbox__inner::after {
  601. width: vw(10);
  602. height: vh(10);
  603. border: vw(2) solid #fff;
  604. border-left: 0;
  605. border-top: 0;
  606. left: vw(4);
  607. top: vh(0);
  608. }
  609. >>> .el-checkbox__input.is-checked .el-checkbox__inner::after {
  610. transform: rotate(40deg) scaleY(1.2);
  611. }
  612. >>> .el-checkbox__input.is-indeterminate .el-checkbox__inner::before {
  613. transform: scaleY(0.5);
  614. top: 45%;
  615. border: 1px solid #fff;
  616. }
  617. >>> .el-table .cell {
  618. line-height: vh(48) !important;
  619. text-align: center !important;
  620. }
  621. >>> .el-table__expand-icon {
  622. -webkit-transform: rotate(0deg);
  623. transform: rotate(0deg);
  624. margin-right: vw(20);
  625. position: absolute;
  626. left: 34%;
  627. top: 34%;
  628. user-select: none;
  629. color: rgb(175, 175, 175);
  630. }
  631. >>> .el-table__expand-icon .el-icon-arrow-right {
  632. font-weight: bold;
  633. }
  634. >>> .el-table__expand-icon .el-icon-arrow-right::before {
  635. content: "\e6d9";
  636. border: 1px solid rgb(175, 175, 175);
  637. padding: vw(3);
  638. font-size: vw(10);
  639. }
  640. >>> .el-table__expand-icon--expanded .el-icon-arrow-right::before {
  641. content: "\e6d8";
  642. font-weight: bold;
  643. padding: vw(3);
  644. font-size: vw(10);
  645. }
  646. }
  647. >>> .el-table-filter {
  648. width: vw(500) !important;
  649. height: vh(200) !important;
  650. }
  651. >>> .el-table thead {
  652. font-family: "PingFangSC-Medium";
  653. font-size: vw(16);
  654. color: #333333;
  655. letter-spacing: 0;
  656. font-weight: 500;
  657. }
  658. >>> .el-table__empty-block {
  659. min-height: vh(35);
  660. line-height: vh(35);
  661. }
  662. >>> .el-table__header {
  663. .el-checkbox {
  664. display: none;
  665. }
  666. }
  667. >>>.el-table td.el-table__cell div{
  668. font-family: 'PingFangSC-Regular';
  669. font-size: vw(16);
  670. color: #333333;
  671. line-height: vh(36);
  672. }
  673. >>>.el-table__column-filter-trigger i{
  674. transform: scale(1.3);
  675. margin-left: vw(5);
  676. background: #FFFFFF;
  677. border: 1px solid rgba(230,230,230,1);
  678. }
  679. >>>.el-icon-arrow-down:before{
  680. content: "\e790" !important;
  681. }
  682. >>>.el-table__body-wrapper {
  683. &::-webkit-scrollbar {
  684. // width: 0 !important;
  685. }
  686. }
  687. </style>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/279887
推荐阅读
相关标签
  

闽ICP备14008679号