scale-table.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <style lang="less" scoped>
  2. .scaleTable {
  3. min-height: 100px;
  4. .table_box {
  5. padding: 0 10px;
  6. }
  7. .btn_box {
  8. width: 100%;
  9. height: 100%;
  10. display: flex;
  11. align-items: center;
  12. justify-content: space-around;
  13. .btn_div {
  14. flex: 1;
  15. display: flex;
  16. align-items: center;
  17. justify-content: center;
  18. }
  19. img {
  20. width: 16px;
  21. height: 16px;
  22. cursor: pointer;
  23. }
  24. }
  25. .table_select_btn {
  26. display: flex;
  27. flex-direction: column;
  28. justify-content: center;
  29. align-items: center;
  30. /deep/ .el-button--text {
  31. padding: 3px 0;
  32. margin: 0;
  33. }
  34. }
  35. /deep/.el-input--mini .el-input__inner {
  36. width: 100%;
  37. }
  38. /deep/.el-table th {
  39. padding: 2px 0;
  40. }
  41. }
  42. </style>
  43. <template>
  44. <div class="scaleTable" :id="`scaleTableRef[${tableIndex}]`">
  45. <div class="table_box">
  46. <el-table
  47. v-if="tableShow"
  48. :data="tableData"
  49. header-row-class-name="header_row_class_name"
  50. border
  51. style="width: 100%; margin: 20px 0"
  52. :span-method="objectSpanMethod"
  53. :ref="`scaleTableRef[${tableIndex}]`"
  54. >
  55. <el-table-column prop="issueId" label="组" width="70px">
  56. <template slot-scope="scope">
  57. <div class="btn_box">
  58. <div class="btn_div">
  59. <img
  60. @click="
  61. $emit('CHANGE_TABLE_ROW', {
  62. type: 1,
  63. tableIndex,
  64. rowIndex: scope.$index,
  65. groupId: scope.row.groupId
  66. })
  67. "
  68. src="../../../images/add-new-rule-sub.png"
  69. alt=""
  70. />
  71. </div>
  72. <div
  73. class="btn_div"
  74. v-if="imposeRestrictionsRemoveOnGroup(scope.row.groupId)"
  75. >
  76. <img
  77. @click="
  78. $emit('CHANGE_TABLE_ROW', {
  79. type: -1,
  80. tableIndex,
  81. groupId: scope.row.groupId
  82. })
  83. "
  84. src="../../../images/add-new-rule-del.png"
  85. alt=""
  86. />
  87. </div>
  88. </div>
  89. </template>
  90. </el-table-column>
  91. <el-table-column prop="groupId" label="问题" width="70px">
  92. <template slot-scope="scope">
  93. <div class="btn_box">
  94. <div class="btn_div">
  95. <img
  96. @click="
  97. $emit('CHANGE_TABLE_ROW', {
  98. type: 2,
  99. tableIndex,
  100. rowIndex: scope.$index,
  101. groupId: scope.row.groupId,
  102. issueId: scope.row.issueId
  103. })
  104. "
  105. src="../../../images/add-new-rule-sub.png"
  106. alt=""
  107. />
  108. </div>
  109. <div
  110. class="btn_div"
  111. v-if="
  112. imposeRestrictionsRemoveOnIssue({
  113. groupId: scope.row.groupId,
  114. issueId: scope.row.issueId
  115. })
  116. "
  117. >
  118. <img
  119. @click="
  120. $emit('CHANGE_TABLE_ROW', {
  121. type: -2,
  122. tableIndex,
  123. issueId: scope.row.issueId
  124. })
  125. "
  126. src="../../../images/add-new-rule-del.png"
  127. alt=""
  128. />
  129. </div>
  130. </div>
  131. </template>
  132. </el-table-column>
  133. <el-table-column prop="two_selectType" label="选择" width="70px">
  134. <template slot-scope="scope">
  135. <div class="table_select_btn">
  136. <el-button
  137. @click="handleSelectTypeBtn(scope.$index, 1)"
  138. :style="scope.row.two_selectType != 1 ? { color: '#999' } : ''"
  139. type="text"
  140. >单选</el-button
  141. >
  142. <el-button
  143. :style="scope.row.two_selectType != 2 ? { color: '#999' } : ''"
  144. @click="handleSelectTypeBtn(scope.$index, 2)"
  145. type="text"
  146. >多选</el-button
  147. >
  148. </div>
  149. </template>
  150. </el-table-column>
  151. <el-table-column
  152. prop="two_content"
  153. label="问题内容"
  154. :width="tableResultType === 1 ? '140px' : ''"
  155. >
  156. <template slot-scope="scope">
  157. <el-form-item>
  158. <el-input
  159. type="text"
  160. v-model="scope.row.two_content"
  161. clearable
  162. placeholder="请输入"
  163. ></el-input>
  164. </el-form-item>
  165. </template>
  166. </el-table-column>
  167. <el-table-column
  168. prop="two_factor"
  169. label="系数"
  170. width="70px"
  171. v-if="tableResultType === 2"
  172. >
  173. <template slot-scope="scope">
  174. <el-form-item>
  175. <el-input
  176. type="text"
  177. v-model="scope.row.two_factor"
  178. clearable
  179. placeholder="请输入"
  180. ></el-input>
  181. </el-form-item>
  182. </template>
  183. </el-table-column>
  184. <el-table-column
  185. prop="two_constant"
  186. label="常数"
  187. width="70px"
  188. v-if="tableResultType === 2"
  189. >
  190. <template slot-scope="scope">
  191. <el-form-item>
  192. <el-input
  193. type="text"
  194. v-model="scope.row.two_constant"
  195. clearable
  196. placeholder="请输入"
  197. ></el-input>
  198. </el-form-item>
  199. </template>
  200. </el-table-column>
  201. <el-table-column prop="issueId" label="选项个数" width="80px">
  202. <template slot-scope="scope">
  203. <div>
  204. {{ optionLength(scope.row.issueId) }}
  205. </div>
  206. </template>
  207. </el-table-column>
  208. <el-table-column prop="orderNo" label="选项" width="70px">
  209. <template slot-scope="scope">
  210. <div class="btn_box">
  211. <div class="btn_div">
  212. <img
  213. @click="
  214. $emit('CHANGE_TABLE_ROW', {
  215. type: 3,
  216. tableIndex,
  217. rowIndex: scope.$index,
  218. groupId: scope.row.groupId,
  219. issueId: scope.row.issueId
  220. })
  221. "
  222. src="../../../images/add-new-rule-sub.png"
  223. alt=""
  224. />
  225. </div>
  226. <div
  227. class="btn_div"
  228. v-if="imposeRestrictionsRemoveOnOption(scope.row.issueId)"
  229. >
  230. <img
  231. @click="
  232. $emit('CHANGE_TABLE_ROW', {
  233. type: -3,
  234. tableIndex,
  235. rowIndex: scope.$index
  236. })
  237. "
  238. src="../../../images/add-new-rule-del.png"
  239. alt=""
  240. />
  241. </div>
  242. </div>
  243. </template>
  244. </el-table-column>
  245. <el-table-column
  246. prop="content"
  247. label="选项内容"
  248. :width="tableResultType === 1 ? '140px' : ''"
  249. >
  250. <template slot-scope="scope">
  251. <el-form-item>
  252. <el-input
  253. type="text"
  254. v-model="scope.row.content"
  255. clearable
  256. placeholder="请输入"
  257. ></el-input>
  258. </el-form-item>
  259. </template>
  260. </el-table-column>
  261. <el-table-column
  262. prop="content"
  263. label="分值"
  264. v-if="tableResultType === 2"
  265. >
  266. <template slot-scope="scope">
  267. <el-form-item>
  268. <el-input
  269. type="text"
  270. v-model="scope.row.content"
  271. clearable
  272. placeholder="请输入"
  273. ></el-input>
  274. </el-form-item>
  275. </template>
  276. </el-table-column>
  277. <el-table-column
  278. prop="result"
  279. label="结果"
  280. width="100px"
  281. v-if="tableResultType === 1"
  282. >
  283. <template slot-scope="scope">
  284. <el-form-item>
  285. <el-input
  286. type="text"
  287. v-model="scope.row.result"
  288. clearable
  289. placeholder="请输入"
  290. ></el-input>
  291. </el-form-item>
  292. </template>
  293. </el-table-column>
  294. <el-table-column
  295. prop="pushInfo"
  296. label="建议"
  297. v-if="tableResultType === 1"
  298. >
  299. </el-table-column>
  300. </el-table>
  301. </div>
  302. </div>
  303. </template>
  304. <script>
  305. export default {
  306. name: "scaleTable",
  307. data() {
  308. return {
  309. tableShow: true
  310. };
  311. },
  312. methods: {
  313. addGroup() {},
  314. // 单选、多选
  315. handleSelectTypeBtn(index, type) {
  316. this.$emit(
  317. "CHANGE_FORM_DATA",
  318. this.tableIndex,
  319. index,
  320. "two_selectType",
  321. type
  322. );
  323. },
  324. // 计算表格选项内容的值
  325. optionLength(val) {
  326. console.log("console.log();", val);
  327. if (val || val === 0) {
  328. const accordWithThis = this.tableData.filter(
  329. (item) => item.issueId === val
  330. );
  331. return accordWithThis.length ? accordWithThis.length : 0;
  332. }
  333. },
  334. // 表格合并单元格逻辑
  335. getSpanArr(data) {
  336. this.spanArr = [];
  337. this.spanArr1 = [];
  338. for (var i = 0; i < data.length; i++) {
  339. if (i === 0) {
  340. // 如果是第一条记录(即索引是0的时候),向数组中加入1
  341. /** *
  342. * 例子:
  343. * name:1
  344. * name:1
  345. * name:2
  346. * name:2
  347. * 最终结果:spanArr = [2,0,2,0]
  348. */
  349. this.spanArr.push(1);
  350. this.pos = 0;
  351. this.spanArr1.push(1);
  352. this.pos1 = 0;
  353. } else {
  354. if (data[i].groupId === data[i - 1].groupId) {
  355. // 如果remark相等就累加,并且push 0
  356. this.spanArr[this.pos] += 1;
  357. this.spanArr.push(0);
  358. } else {
  359. // 不相等push 1,并且pos 要换成当前下标
  360. this.spanArr.push(1);
  361. this.pos = i;
  362. }
  363. if (data[i].issueId === data[i - 1].issueId) {
  364. // 如果remark相等就累加,并且push 0
  365. this.spanArr1[this.pos1] += 1;
  366. this.spanArr1.push(0);
  367. } else {
  368. // 不相等push 1,并且pos 要换成当前下标
  369. this.spanArr1.push(1);
  370. this.pos1 = i;
  371. }
  372. }
  373. }
  374. },
  375. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  376. if (columnIndex == 0) {
  377. const _row = this.spanArr[rowIndex];
  378. const _col = _row > 0 ? 1 : 0;
  379. return {
  380. rowspan: _row,
  381. colspan: _col
  382. };
  383. } else if (
  384. column.label == "问题" ||
  385. column.label == "选择" ||
  386. column.label == "问题内容" ||
  387. column.label == "系数" ||
  388. column.label == "常数" ||
  389. column.label == "选项个数"
  390. ) {
  391. const _row = this.spanArr1[rowIndex];
  392. const _col = _row > 0 ? 1 : 0;
  393. return {
  394. rowspan: _row,
  395. colspan: _col
  396. };
  397. }
  398. },
  399. // 删除组限制:剩余一组时隐藏删除按钮
  400. imposeRestrictionsRemoveOnGroup(groupId) {
  401. // 查找有没有其他组存在 如果为0的话则不存在
  402. const otherGroupLength = this.tableData.filter(
  403. (item) => item.groupId !== groupId
  404. ).length;
  405. return otherGroupLength;
  406. },
  407. // 删除问题限制:该组剩余一个问题时隐藏删除按钮
  408. imposeRestrictionsRemoveOnIssue(val) {
  409. const { groupId, issueId } = val;
  410. // 获取该组所有的issus
  411. const thisGroups = this.tableData.filter(
  412. (item) => item.groupId === groupId
  413. );
  414. // 查找该组有没有其他问题 otherIssueLength为0表示只有当前一个问题存在 所以隐藏删除按钮
  415. const otherIssueLength = thisGroups.filter(
  416. (item) => item.issueId !== issueId
  417. ).length;
  418. console.log(thisGroups, otherIssueLength);
  419. if (otherIssueLength) {
  420. return true;
  421. }
  422. return false;
  423. },
  424. // 删除选项限制:该问题剩余一个选项时隐藏删除按钮
  425. imposeRestrictionsRemoveOnOption(issueId) {
  426. // 查找同组的问题,如果只有一个问题则隐藏删除按钮
  427. const thisOptionByissueIdLenth = this.tableData.filter(
  428. (item) => item.issueId === issueId
  429. ).length;
  430. if (thisOptionByissueIdLenth > 1) {
  431. return true;
  432. }
  433. return false;
  434. }
  435. },
  436. created() {
  437. this.getSpanArr(this.tableData);
  438. },
  439. watch: {
  440. tableData: {
  441. handler() {
  442. this.getSpanArr(this.tableData);
  443. },
  444. deep: true
  445. },
  446. tableResultType() {
  447. // this.$nextTick(() => {
  448. // console.log(this.$refs[`scaleTableRef[${this.tableIndex}]`]);
  449. // console.log(
  450. // document.getElementById(`scaleTableRef[${this.tableIndex}]`).offsetHeight
  451. // );
  452. // });
  453. // this.$refs[`scaleTableRef[${this.tableIndex}]`].doLayout();
  454. this.tableShow = false;
  455. setTimeout(() => {
  456. // this.$refs[`scaleTableRef[${this.tableIndex}]`].doLayout();
  457. this.tableShow = true;
  458. });
  459. }
  460. },
  461. props: {
  462. tableData: {
  463. default: []
  464. },
  465. tableIndex: {
  466. required: true
  467. },
  468. tableResultType: {
  469. required: true,
  470. default: 1
  471. }
  472. }
  473. };
  474. </script>