当前位置:   article > 正文

elementui el-table 封装表格_element table 封装

element table 封装

ps:   1.3版本(行内编辑)

案例:

完整代码:

可直接复制粘贴,但一定要全看完!

v-slot="scopeRows" 是vue3的写法;

vue2是 slot-scope="scope"

  1. <template>
  2. <!-- 简单表格、多层表头、页码、没有合并列行 -->
  3. <div class="maintenPublictable">
  4. <!--cell-style 改变某一列行的背景色 -->
  5. <!-- tree-props 配置树形子表
  6. row-click: 单击事件
  7. highlight-current-row:高亮选中某行
  8. default-expand-all:默认是否展开字列表
  9. current-change:管理选中时触发的事件
  10. selection-change:多选框
  11. row-key="id": id:一定要跟后台返回来的id一致,不一致,会出错
  12. -->
  13. <!-- :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" -->
  14. <el-table
  15. ref="table"
  16. :data="tableData"
  17. :height="getHeight"
  18. border
  19. highlight-current-row
  20. @selection-change="handleSelectionChange"
  21. :key="itemKey"
  22. :cell-style="tableCellStyle"
  23. @row-click="clickRow"
  24. row-key="ID"
  25. :default-expand-all="defaultall"
  26. :highlight-current-row="highlightCurrent"
  27. @current-change="handleCurrentChangeRow"
  28. :tree-props="{ children: 'Children', }"
  29. >
  30. <el-table-column
  31. type="index"
  32. width="55"
  33. label="序号"
  34. >
  35. </el-table-column>
  36. <el-table-column
  37. type="selection"
  38. v-if="isSelection"
  39. width="55"
  40. >
  41. </el-table-column>
  42. <!-- item.direction 方向,判断居中还是靠右 -->
  43. <template v-for="(item, index) in tableHeader">
  44. <!-- 1. 这是第一层 -->
  45. <!-- sortable: 排序 -->
  46. <el-table-column
  47. v-if="!item.Children"
  48. :key="index"
  49. :prop="item.prop"
  50. :label="item.label"
  51. header-align="center"
  52. :align="item.direction"
  53. :min-width="item.width"
  54. :sortable="item.sortable"
  55. >
  56. <!--需封装动态展示并且适合多种输入 todo -->
  57. <template
  58. v-slot="scopeRows"
  59. v-if="item.label === '在库数量'"
  60. >
  61. <el-input
  62. v-model="scopeRows.row.editRow.InStockQuantity"
  63. type="number"
  64. oninput="if(value<0)value=0"
  65. @blur="getRowData(scopeRows.row.editRow)"
  66. ></el-input>
  67. </template>
  68. <template
  69. v-slot="scopeRows"
  70. v-if="item.label === '在库金额'"
  71. >
  72. <el-input
  73. v-model="scopeRows.row.editRow.InStockAmount"
  74. type="number"
  75. @blur="getRowData(scopeRows.row.editRow)"
  76. oninput="if(value<0)value=0"
  77. ></el-input>
  78. </template>
  79. <template
  80. v-slot="scopeRows"
  81. v-if="item.label === '库位'"
  82. >
  83. <el-select
  84. v-model=" scopeRows.row.editRow.StoreLocationID"
  85. @change="getRowData(scopeRows.row.editRow)"
  86. >
  87. <el-option
  88. v-for="item in StorageLocation"
  89. :key="item.ID"
  90. :label="item.LocationName"
  91. :value="item.ID"
  92. >
  93. </el-option>
  94. </el-select>
  95. </template>
  96. <template
  97. v-slot="scopeRows"
  98. v-if="item.label === '风力发电有限公司1'"
  99. >
  100. <el-input
  101. v-model="scopeRows.row.Amount.Amount1"
  102. type="number"
  103. @blur="getRowData(scopeRows.row.Amount)"
  104. :disabled=" (rowID!=scopeRows.row.SubjectsID && !this.disableD) || (rowID==scopeRows.row.SubjectsID && this.disableD) "
  105. oninput="value=value.replace(/^0|[^0-9]/g, '')"
  106. ></el-input>
  107. </template>
  108. <template
  109. v-slot="scopeRows"
  110. v-if="item.label === '溪木源公司2'"
  111. >
  112. <el-input
  113. v-model="scopeRows.row.Amount.Amount2"
  114. type="number"
  115. @blur="getRowData(scopeRows.row.Amount)"
  116. :disabled="rowID!=scopeRows.row.SubjectsID "
  117. oninput="value=value.replace(/^0|[^0-9]/g, '')"
  118. ></el-input>
  119. </template>
  120. <template
  121. v-slot="scopeRows"
  122. v-if="item.label === '大风车有限公司3'"
  123. >
  124. <el-input
  125. v-model="scopeRows.row.Amount.Amount3"
  126. type="number"
  127. @blur="getRowData(scopeRows.row.Amount)"
  128. :disabled="rowID!=scopeRows.row.SubjectsID "
  129. oninput="value=value.replace(/^0|[^0-9]/g, '')"
  130. ></el-input>
  131. </template>
  132. <template
  133. v-slot="scopeRows"
  134. v-if="item.label === '新能源公司4'"
  135. >
  136. <el-input
  137. v-model="scopeRows.row.Amount.Amount4"
  138. type="number"
  139. :disabled="rowID!=scopeRows.row.SubjectsID "
  140. @blur="getRowData(scopeRows.row.Amount)"
  141. oninput="value=value.replace(/^0|[^0-9]/g, '')"
  142. ></el-input>
  143. </template>
  144. <template
  145. v-slot="scopeRows"
  146. v-if="item.label === '定边风力公司5'"
  147. >
  148. <el-input
  149. v-model="scopeRows.row.Amount.Amount5"
  150. type="number"
  151. @blur="getRowData(scopeRows.row.Amount)"
  152. :disabled="rowID!=scopeRows.row.SubjectsID "
  153. oninput="value=value.replace(/^0|[^0-9]/g, '')"
  154. ></el-input>
  155. </template>
  156. <!-- 入库登记用到的 inWarehouseRow: 在给表格赋值时定义的。 -->
  157. <template
  158. v-slot="scopeRows"
  159. v-if="item.label === '购买数量'"
  160. >
  161. <el-input
  162. v-model="scopeRows.row.inWarehouseRow.Quantity"
  163. type="number"
  164. @blur="getRowData(scopeRows.row.inWarehouseRow)"
  165. oninput="value=value.replace(/^0|[^0-9]/g, '')"
  166. ></el-input>
  167. </template>
  168. <template
  169. v-slot="scopeRows"
  170. v-if="item.label === '单价'"
  171. >
  172. <el-input
  173. v-model="scopeRows.row.inWarehouseRow.UnitPrice"
  174. type="number"
  175. @blur="getRowData(scopeRows.row.inWarehouseRow)"
  176. oninput="value=value.replace(/[^\d.]/g, '').replace(/^0+(\d)/, '$1').replace(/^\./, '0.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^\./g, '')"
  177. ></el-input>
  178. </template>
  179. <template
  180. v-slot="scopeRows"
  181. v-if="item.label === '操作'"
  182. >
  183. <el-button
  184. link
  185. type="primary"
  186. size="small"
  187. color="#b0b1b3"
  188. class="handleClick_Btn"
  189. @click="handleClick(scopeRows.row)"
  190. >保存</el-button>
  191. </template>
  192. </el-table-column>
  193. <!-- 二级表头 -->
  194. <el-table-column
  195. v-else
  196. :key="index + 1"
  197. :prop="item.prop"
  198. :label="item.label"
  199. :type="item.type"
  200. :align="item.align || 'center'"
  201. >
  202. <template v-for="(childItem, index) in item.Children">
  203. <!-- 三级表头 -->
  204. <el-table-column
  205. v-if="!childItem.Children"
  206. :key="index"
  207. :prop="childItem.prop"
  208. :label="childItem.label"
  209. header-align="center"
  210. :align="childItem.direction"
  211. :min-width="childItem.width"
  212. >
  213. </el-table-column>
  214. <el-table-column
  215. v-else
  216. :key="index + 1"
  217. :prop="childItem.prop"
  218. :label="childItem.label"
  219. :type="childItem.type"
  220. :align="childItem.align || 'center'"
  221. >
  222. <template v-for="(childItem, index) in item.Children">
  223. <!-- 这是第三层 -->
  224. <el-table-column
  225. v-if="!childItem.Children"
  226. :key="index"
  227. :prop="childItem.prop"
  228. :label="childItem.label"
  229. header-align="center"
  230. :align="childItem.direction"
  231. :min-width="childItem.width"
  232. >
  233. </el-table-column>
  234. <el-table-column
  235. v-else
  236. :key="index + 1"
  237. :prop="childItem.prop"
  238. :label="childItem.label"
  239. :type="childItem.type"
  240. :align="childItem.align || 'center'"
  241. >
  242. </el-table-column>
  243. </template>
  244. </el-table-column>
  245. </template>
  246. </el-table-column>
  247. </template>
  248. <!-- 表格最后一列是否是勾选框【完成情况】 -->
  249. <el-table-column
  250. v-if="isSelect"
  251. align="center"
  252. >
  253. <template
  254. slot="header"
  255. slot-scope="scope"
  256. >
  257. <el-checkbox
  258. @change="allCheck(isAllcheck, tableData, ClickIdsList, isIndeterminate)"
  259. size="large"
  260. v-model="isAllcheck"
  261. :indeterminate="isIndeterminate"
  262. ></el-checkbox>
  263. 完成情况
  264. </template>
  265. <template slot-scope="scope">
  266. <!-- <el-button @click="Ones(scope)">122333</el-button> -->
  267. <el-checkbox
  268. @change="OnesClick(scope.row)"
  269. v-model="scope.row.check"
  270. class="ml-4"
  271. size="large"
  272. ></el-checkbox>
  273. </template>
  274. </el-table-column>
  275. </el-table>
  276. <!-- 分页 -->
  277. <div v-if="showFenYe">
  278. <el-pagination
  279. @size-change="handleSizeChange"
  280. @current-change="handleCurrentChange"
  281. :current-page="page.currentPage"
  282. :page-sizes="[5, 10, 15, 20]"
  283. :page-size="page.pagesize"
  284. layout="total, sizes, prev, pager, next, jumper"
  285. :total="page.total"
  286. >
  287. </el-pagination>
  288. </div>
  289. </div>
  290. </template>
  291. <script>
  292. // import preventBack from "vue-prevent-browser-back"; //阻止返回
  293. import { PublicFunction } from "@/utils/vuePublic";
  294. export default {
  295. name: "maintenPublictable",
  296. components: {},
  297. props: {
  298. // 接收的是:是否有分页、是否有勾选
  299. columns: {
  300. type: Object,
  301. default: {},
  302. },
  303. // 接收的是:页码
  304. pagination: {
  305. type: Object,
  306. default: {},
  307. },
  308. // 接收的是:传递过来的库位数据
  309. storageLocationSelect: {
  310. type: Array,
  311. default: [],
  312. },
  313. // 接收的是:传递过来的库位数据
  314. GoodsID: {
  315. type: Array,
  316. default: [],
  317. },
  318. // 接收的是:传递过来的计划编制的行点击的id
  319. SubjectsID: {
  320. type: Number,
  321. default: "",
  322. },
  323. },
  324. data() {
  325. return {
  326. tableHeader: [], //表头
  327. tableData: [], //数据
  328. itemKey: "",
  329. types: 1, //用于合并,判断是否需要合并
  330. //#region 与父组件关联
  331. getHeight: 20, //高度
  332. isSelect: false, //勾选框
  333. showFenYe: false, //是否有分页
  334. isSelection: false, //是否有多选框
  335. isTag: false, //是否有标签
  336. defaultall: false, //是否默认展开所有行(用于有树状结构的表格)
  337. highlightCurrent: false, //高亮选中
  338. //#endregion
  339. // 页码
  340. page: {
  341. currentPage: 1, //当前页
  342. pagesize: 5, //当前页的条数
  343. total: 20, //总数
  344. },
  345. //多选框
  346. multipleSelection: [],
  347. //#region 用于右侧的完成情况
  348. //选择
  349. isAllcheck: false, //全选
  350. ClickIdsList: [], //已选择集合组
  351. isIndeterminate: false, //部分选中,默认是false
  352. //#endregion,
  353. //仓库对应的库位数据
  354. StorageLocation: [],
  355. // //选中的行
  356. // 行id
  357. rowID: null,
  358. disableD: true,
  359. //#endregion
  360. };
  361. },
  362. // mixins: [preventBack], //注入  阻止返回上一页
  363. created() {},
  364. watch: {
  365. // 监听对象的写法(监听页码的变化)
  366. pagination: {
  367. handler(newValue, oldVal) {
  368. // console.log("监听111", oldVal);
  369. // console.log("监听222", newValue);
  370. this.page.total = newValue.total;
  371. },
  372. deep: true, // 深度监听
  373. // immediate: true, //写上它,初始化时也会调用监听
  374. },
  375. //接收库位的数据
  376. storageLocationSelect: {
  377. handler(n, o) {
  378. this.StorageLocation = n;
  379. },
  380. deep: true, // 深度监听
  381. immediate: true, //写上它,初始化时也会调用监听
  382. },
  383. },
  384. mounted() {
  385. this.init();
  386. // console.log(this.columns, "初始化:接收父组件传过来的值", this.pagination);
  387. },
  388. methods: {
  389. a(val, el) {
  390. console.log(val, "zheshi ", el);
  391. },
  392. Test(val) {
  393. console.log(val);
  394. },
  395. Ones(value) {
  396. console.log("12121212", value);
  397. },
  398. //在父组件初始化时,需要获得页码,所以子组件初始化时把页码传过去
  399. init() {
  400. let _this = this;
  401. _this.getHeight = this.columns.getHeight;
  402. _this.defaultall = this.columns.defaultall; //是否展开所有行
  403. _this.isSelect = this.columns.isSelect; //右侧的完成情况
  404. _this.isTag = this.columns.isTag; //是否有标签
  405. _this.showFenYe = this.columns.showFenYe;
  406. _this.isSelection = this.columns.isSelection; //左侧的多选框
  407. _this.highlightCurrent = this.columns.highlightCurrent; //高亮选中
  408. _this.page.total = this.pagination.total;
  409. _this.$emit("getPage", {
  410. data: {
  411. pageSize: _this.page.pagesize,
  412. pageNum: _this.page.currentPage,
  413. isTypes: 1,
  414. },
  415. });
  416. // //每次使用就调一次
  417. // this.SetDataTableHeader()
  418. this.rowID = null;
  419. },
  420. //一页有多少条数据
  421. handleSizeChange(val) {
  422. let _this = this;
  423. _this.page.pagesize = val;
  424. // 子传父
  425. _this.$emit("getPage", {
  426. data: {
  427. pageSize: _this.page.pagesize,
  428. pageNum: _this.page.currentPage,
  429. isTypes: 2,
  430. },
  431. });
  432. },
  433. //第几页/切换页码
  434. handleCurrentChange(val) {
  435. let _this = this;
  436. _this.page.currentPage = val;
  437. _this.$emit("getPage", {
  438. data: {
  439. pageSize: _this.page.pagesize,
  440. pageNum: _this.page.currentPage,
  441. isTypes: 2,
  442. },
  443. });
  444. },
  445. //表头
  446. SetDataTableHeader(GetDataLists) {
  447. //重新渲染,itemKey用于处理Table不渲染的问题
  448. this.itemKey = Math.random();
  449. //重新渲染数据表
  450. this.tableHeader = GetDataLists;
  451. // console.log("表头", this.tableHeader);
  452. // this.$forceUpdate()
  453. },
  454. //table值
  455. SettableData(tabledata, flag) {
  456. this.itemKey = Math.random();
  457. // console.log(tabledata, "tabledata");
  458. let _this = this;
  459. _this.tableData = tabledata;
  460. // console.log("接收父组件传过来的表格数据", tabledata);
  461. this.tableData.forEach((item, index) => {
  462. //仓库初始化编辑
  463. item.editRow = {
  464. InStockQuantity: "", //在库数量
  465. InStockAmount: "", //在库金额
  466. StoreLocationID: "", //库位
  467. };
  468. /**1是编辑,0是新增 */
  469. if (flag == 1) {
  470. item.inWarehouseRow = {
  471. Quantity: item.Quantity,
  472. UnitPrice: item.UnitPrice,
  473. };
  474. } else if (flag == 0) {
  475. item.inWarehouseRow = {
  476. Quantity: "",
  477. UnitPrice: "",
  478. };
  479. }
  480. //如果flag为true的情况下,是编辑,进行赋值, 为false是新增
  481. if (flag == true) {
  482. //计划编制的字段
  483. item.Amount = {
  484. Amount1: item.Amount1,
  485. Amount2: item.Amount2,
  486. Amount3: item.Amount3,
  487. Amount4: item.Amount4,
  488. Amount5: item.Amount5,
  489. };
  490. } else {
  491. item.Amount = {
  492. Amount1: "",
  493. Amount2: "",
  494. Amount3: "",
  495. Amount4: "",
  496. Amount5: "",
  497. };
  498. }
  499. });
  500. },
  501. //左侧:多选框
  502. handleSelectionChange(val) {
  503. this.multipleSelection = val;
  504. // console.log("左侧:勾选数据", this.multipleSelection);
  505. this.$emit("handleSelectionChange", val);
  506. },
  507. //#region 下面这个是用于最右侧的完成情况
  508. //全选 调取公共js文件的方法
  509. allCheck(isAll, tableData, checkList, isCheck) {
  510. //接收传过来的值
  511. let objData = PublicFunction.allCheck(
  512. isAll,
  513. tableData,
  514. checkList,
  515. isCheck
  516. );
  517. this.isAllcheck = objData.isAll;
  518. this.ClickIdsList = objData.checkList;
  519. },
  520. //单行选择
  521. OnesClick(rows) {
  522. if (rows.check) {
  523. this.ClickIdsList.push(rows.id);
  524. } else {
  525. let index = this.ClickIdsList.indexOf(rows.id);
  526. this.ClickIdsList.splice(index, 1);
  527. }
  528. // console.log("勾选111", this.ClickIdsList);
  529. this.isIndeterminate =
  530. this.ClickIdsList.length > 0 &&
  531. this.ClickIdsList.length < this.tableData.length;
  532. this.isAllcheck = this.ClickIdsList.length == this.tableData.length;
  533. },
  534. //#endregion
  535. // 合并单元格
  536. objectSpanMethod({ row, column, rowIndex, columnIndex }, tableData, types) {
  537. if (types === 1) {
  538. switch (
  539. columnIndex // 将列索引作为判断值
  540. ) {
  541. // 通过传递不同的列索引和需要合并的属性名,可以实现不同列的合并(索引0,1 指的是页面上的0,1)
  542. case 2:
  543. return PublicFunction.MergeCol(tableData, "itemDetail", rowIndex);
  544. case 1:
  545. return PublicFunction.MergeCol(tableData, "item", rowIndex);
  546. }
  547. } else {
  548. //保障作业
  549. switch (columnIndex) {
  550. case 1:
  551. return PublicFunction.MergeCol(tableData, "item", rowIndex);
  552. }
  553. }
  554. //判断检查内容是否为空
  555. // if (
  556. // tableData[columnIndex].checkContent != undefined ||
  557. // tableData[columnIndex].checkContent != null
  558. // ) {
  559. // } else {
  560. // }
  561. },
  562. // 提交(在父组件点击提交时调用这个方法)
  563. childSumbit() {
  564. // console.log(
  565. // "子组件提交",
  566. // this.tableData,
  567. // this.ClickIdsList,
  568. // this.multipleSelection
  569. // );
  570. let param = {
  571. tabledata: this.tableData,
  572. ClickIdsList: this.ClickIdsList,
  573. multipleSelection: this.multipleSelection,
  574. };
  575. // 把值传给父组件
  576. this.$emit("sumbitData", param);
  577. },
  578. //行点击事件
  579. clickRow(row, column, event) {
  580. // let _this = this;
  581. // _this.$router.push({
  582. // name: "equipSchedule", //路由名称
  583. // params: {
  584. // data: row, //参数
  585. // },
  586. // });
  587. this.rowID = row.SubjectsID;
  588. this.$emit("rowClick", row);
  589. },
  590. // 行选中时间
  591. handleCurrentChangeRow(val) {
  592. this.$emit("handleCurrentChangeRow", val);
  593. },
  594. // 改变某一列的行的背景色
  595. tableCellStyle({ row, column, rowIndex, columnIndex }) {
  596. // console.log("背景色:");
  597. // console.log("row===:", row);
  598. // console.log("column===:", column);
  599. // console.log("rowIndex===:", rowIndex);
  600. // console.log("columnIndex===:", columnIndex);
  601. //如果是第一列
  602. if (columnIndex === 1) {
  603. //如果这一行的字段==未维护
  604. if (row.state == "未维护") {
  605. // 如果是未维护——背景色浅蓝色,字体色蓝色;
  606. return "background:#ecf5ff; color:#409eff";
  607. } else if (row.state == "已维护") {
  608. // 如果是已维护——背景色绿色,字体色白色;
  609. return "background:#67c23aa6;color:#fff ";
  610. } else if (row.state == "部分维护") {
  611. // 如果是已维护——背景色棕色,字体色白色;
  612. return "background:#e6a23cab;color:#fff ";
  613. } else {
  614. }
  615. } else {
  616. }
  617. },
  618. //获取行内编辑的数据
  619. getRowData(val) {
  620. console.log(val, "获取行内编辑的数据");
  621. this.$emit("getEditRow", val);
  622. },
  623. /**
  624. * 操作列
  625. */
  626. handleClick(val) {
  627. // console.log(val, "操作列");
  628. this.$emit("getclickRow", val);
  629. },
  630. },
  631. //#endregion
  632. };
  633. </script>
  634. <style scoped>
  635. .maintenPublictable ::v-deep .el-table th,
  636. ::v-deep .el-table thead.is-group th.el-table__cell {
  637. background: linear-gradient(147deg, #70c0ff, #2f9fff);
  638. color: #fff;
  639. padding: 0;
  640. margin: 0;
  641. }
  642. /*****滚动条影藏 */
  643. ::v-deep .el-table--scrollable-y ::-webkit-scrollbar {
  644. display: none !important;
  645. }
  646. /**lable名字 */
  647. ::v-deep .el-checkbox__label {
  648. color: #fff;
  649. }
  650. ::v-deep .el-table__header {
  651. height: 4rem;
  652. }
  653. /*按钮样式 */
  654. /* ::v-deep .el-button:hover,
  655. ::v-deep .el-button:focus {
  656. color: #f3f3f3 !important;
  657. font-weight: bold;
  658. } */
  659. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/621621
推荐阅读