DocGeneratorGrid.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <template>
  2. <div class="flex-container-1">
  3. <div class="flex-content-1 table-fix-head">
  4. <table
  5. class="fixed-table table-striped table-bordered"
  6. :width="tableWidth"
  7. height="40px"
  8. >
  9. <thead>
  10. <tr height="40px">
  11. <th width="50px" class="fixed-cell">
  12. <input
  13. autocomplete="off"
  14. :checked="allSelected"
  15. type="checkbox"
  16. @click="selectAll($event)"
  17. />
  18. </th>
  19. <th
  20. v-for="infoGridField in infoGridFields"
  21. v-show="infoGridField.isShow"
  22. :key="infoGridField.fieldName"
  23. :width="infoGridField.width + 'px'"
  24. @dragover="ondragover($event, infoGridField)"
  25. @click="onSort(infoGridField)"
  26. >
  27. <div
  28. :id="'infoGridFieldId_' + infoGridField.fieldName"
  29. class="rz-handle"
  30. draggable="true"
  31. @dragstart="ondragstart($event, infoGridField)"
  32. @drag="ondrag($event, infoGridField)"
  33. @dragend="ondragend($event, infoGridField)"
  34. />
  35. <div class="td-max">
  36. {{ infoGridField.name }}
  37. </div>
  38. </th>
  39. </tr>
  40. </thead>
  41. <tbody>
  42. <tr v-for="rowData in dataList" :key="rowData.id" height="40px">
  43. <td class="fixed-cell">
  44. <input
  45. v-model="rowData.checked"
  46. autocomplete="off"
  47. type="checkbox"
  48. :value="rowData.id"
  49. />
  50. </td>
  51. <td
  52. v-for="infoGridField in infoGridFields"
  53. :key="infoGridField.fieldName + '_' + rowData.id"
  54. >
  55. <input
  56. v-if="infoGridField.simpleDisplayType == 'TextEditor'"
  57. autocomplete="off"
  58. type="text"
  59. class="form-control"
  60. :value="getDisplayValue(rowData, infoGridField)"
  61. @keyup="valueChange($event, rowData, infoGridField)"
  62. />
  63. <input
  64. v-if="infoGridField.simpleDisplayType == 'NumberEditor'"
  65. autocomplete="off"
  66. type="number"
  67. class="form-control"
  68. :value="getDisplayValue(rowData, infoGridField)"
  69. @keyup="valueChange($event, rowData, infoGridField)"
  70. />
  71. <span
  72. v-if="
  73. infoGridField.simpleDisplayType == null ||
  74. infoGridField.simpleDisplayType == '' ||
  75. infoGridField.simpleDisplayType == 'NONE'
  76. "
  77. >
  78. {{
  79. rowData.data[infoGridField.fieldName] == undefined
  80. ? ""
  81. : rowData.data[infoGridField.fieldName].displayValue[0]
  82. }}
  83. </span>
  84. </td>
  85. </tr>
  86. </tbody>
  87. </table>
  88. </div>
  89. <div class="flex-footer-1" style="margin-top: 10px">
  90. <div class="pull-left">
  91. <span>
  92. {{ (pagination.current_page - 1) * pagination.per_page + 1 }}
  93. -
  94. {{ pagination.current_page * pagination.per_page }}
  95. 条,共计
  96. {{ pagination.total }}
  97. 条,每页
  98. </span>
  99. <PageSizeSelect @page-size-changed="gridSizeSelect" />
  100. <span>条</span>
  101. </div>
  102. <div class="pull-right">
  103. <Pagination :pagination="pagination" :callback="refreshSearch" />
  104. </div>
  105. </div>
  106. </div>
  107. </template>
  108. <script>
  109. import Common from '../../common/Common.js';
  110. import InfoUtil from './InfoUtil.js';
  111. import Pagination from '../../vue-bootstrap-pagination/src/vue-bootstrap-pagination.vue';
  112. import PageSizeSelect from '../../page-size-select/src/PageSizeSelect.vue';
  113. export default {
  114. components: {
  115. Pagination,
  116. PageSizeSelect,
  117. },
  118. props: {
  119. // 查询窗口编号
  120. infoWindowNo: {
  121. type: String,
  122. default: null,
  123. },
  124. // 表格字段
  125. infoGridFields: {
  126. type: Array,
  127. default: null,
  128. },
  129. // 渲染的数据
  130. infoWindowData: {
  131. type: Object,
  132. default: null,
  133. },
  134. // 方法:根据id判断数据是否被选中
  135. isSelectedById: {
  136. type: Function,
  137. default: null,
  138. },
  139. },
  140. emits: ['selectChanged', 'refreshSearch'],
  141. data: function () {
  142. return {
  143. dataList: [],
  144. pagination: {
  145. total: 0,
  146. per_page: Common.pageSize, // required
  147. current_page: 1, // required
  148. last_page: 10, // required
  149. },
  150. sortStyle: '',
  151. sortClause: '',
  152. };
  153. },
  154. computed: {
  155. /**
  156. * 自动计算表格的宽度
  157. * @author YangZhiJie 20210909
  158. */
  159. tableWidth: function () {
  160. var totalWidth = 50;
  161. if (this.infoGridFields !== undefined) {
  162. this.infoGridFields.forEach(function (infoGridField) {
  163. if (
  164. infoGridField.width !== undefined &&
  165. infoGridField.width !== null &&
  166. infoGridField.width !== ''
  167. ) {
  168. totalWidth += Number(infoGridField.width);
  169. }
  170. });
  171. }
  172. return totalWidth + 'px';
  173. },
  174. /**
  175. * 是否选择了全部的数据
  176. * @author YangZhiJie 20210909
  177. */
  178. allSelected: function () {
  179. var _self = this;
  180. if (
  181. this.dataList === undefined ||
  182. this.dataList === null ||
  183. this.dataList.length == 0
  184. ) {
  185. return false;
  186. }
  187. for (
  188. let index = 0, length = this.dataList.length;
  189. index < length;
  190. index++
  191. ) {
  192. if (this.dataList[index].checked === false) {
  193. return false;
  194. }
  195. }
  196. return true;
  197. },
  198. },
  199. watch: {
  200. infoWindowData: function (newValue, oldValue) {
  201. this.setDataList(newValue);
  202. },
  203. dataList: {
  204. handler: function (newValue, oldValue) {
  205. // 清除延迟执行
  206. if (
  207. this.selectChangedTimeout !== undefined &&
  208. this.selectChangedTimeout !== null
  209. ) {
  210. window.clearTimeout(this.selectChangedTimeout);
  211. }
  212. // 设置延迟执行
  213. this.selectChangedTimeout = setTimeout(() => {
  214. this.$emit('selectChanged', this.dataList);
  215. }, 500);
  216. },
  217. deep: true,
  218. },
  219. },
  220. methods: {
  221. /**
  222. * 重新设置分页
  223. * 供外部组件调用
  224. * @author YangZhiJie 20210909
  225. */
  226. resetPagination: function () {
  227. this.pagination.current_page = 1;
  228. this.pagination.last_page = 10;
  229. this.pagination.total = 0;
  230. },
  231. /**
  232. * 设置分页
  233. * @author YangZhiJie 20210909
  234. */
  235. setPagination: function (total, lastPage) {
  236. this.pagination.last_page = lastPage;
  237. this.pagination.total = total;
  238. },
  239. /**
  240. * 获取查询参数
  241. * 供外部组件调用
  242. * @author YangZhiJie 20210909
  243. */
  244. getQueryParam: function () {
  245. return {
  246. infoWindowNo: this.infoWindowNo,
  247. start: (this.pagination.current_page - 1) * this.pagination.per_page,
  248. length: this.pagination.per_page,
  249. sortClause: this.sortClause,
  250. };
  251. },
  252. /**
  253. * 页码数量改变
  254. * @author YangZhiJie 20210909
  255. */
  256. gridSizeSelect: function (newPageSize) {
  257. this.pagination.per_page = newPageSize;
  258. this.pagination.current_page = 1;
  259. },
  260. /**
  261. * 列开始拖动
  262. * @author YangZhiJie 20210909
  263. */
  264. ondragstart: function (event, gridFieldItem) {
  265. var _self = this;
  266. _self.startX = event.pageX;
  267. _self.startWidth = Number(gridFieldItem.width);
  268. event.dataTransfer.setDragImage(event.target, 0, 20);
  269. event.dataTransfer.effectAllowed = 'move';
  270. },
  271. /**
  272. * 列拖动
  273. * @author YangZhiJie 20210909
  274. */
  275. ondrag: function (event, gridFieldItem) {
  276. var _self = this;
  277. gridFieldItem.width = _self.startWidth + (event.pageX - _self.startX);
  278. },
  279. /**
  280. * 列结束拖动
  281. * @author YangZhiJie 20210909
  282. */
  283. ondragend: function (event, gridFieldItem, index) {
  284. var _self = this;
  285. let newWidth = _self.startWidth + (event.pageX - _self.startX);
  286. if (newWidth < 50) {
  287. newWidth = 50;
  288. }
  289. gridFieldItem.width = newWidth;
  290. //gridFieldItem.width = gridFieldItemClone.width;
  291. InfoUtil.saveInfoGridFields(_self.infoWindowDto.no, _self.infoGridFields);
  292. },
  293. ondragover: function (event, gridFieldItem) {
  294. event.preventDefault();
  295. event.dataTransfer.dropEffect = 'move';
  296. },
  297. /**
  298. * 排序
  299. * @author YangZhiJie 20210909
  300. */
  301. onSort: function (infoGridField) {
  302. var _self = this;
  303. var fieldName = null;
  304. if (
  305. infoGridField.sortFieldName != undefined &&
  306. infoGridField.sortFieldName != ''
  307. ) {
  308. fieldName = infoGridField.sortFieldName;
  309. } else {
  310. fieldName = infoGridField.fieldName;
  311. }
  312. _self.sortClause = fieldName + _self.sortStyle;
  313. this.$emit('refreshSearch');
  314. _self.sortStyle = _self.sortStyle === ' ASC' ? ' DESC' : ' ASC';
  315. },
  316. /**
  317. * 发送查询请求
  318. * @author YangZhiJie 20210909
  319. */
  320. refreshSearch: function () {
  321. this.$emit('refreshSearch');
  322. },
  323. /**
  324. * 冻结表头
  325. * @author YangZhiJie 20210909
  326. */
  327. fixedTableHeader: function () {
  328. let _self = this;
  329. _self.$nextTick(function () {
  330. var $th = $('.table-fix-head').find('thead');
  331. var $fixedCell = $('.table-fix-head').find('.fixed-cell');
  332. $('.table-fix-head').on('scroll', function () {
  333. $th.css('transform', 'translateY(' + this.scrollTop + 'px)');
  334. $fixedCell.css('transform', 'translateX(' + this.scrollLeft + 'px)');
  335. });
  336. });
  337. },
  338. /**
  339. * 表格中填写的值发生了改变
  340. * @author YangZhiJie 20210909
  341. */
  342. valueChange: function (event, rowData, infoGridField) {
  343. var _self = this;
  344. var value = event.target.value;
  345. const fieldName = infoGridField.fieldName;
  346. if (rowData.data[fieldName] == undefined) {
  347. var tempFieldValue = {
  348. displayValue: [value],
  349. };
  350. rowData.data[fieldName] = tempFieldValue;
  351. } else {
  352. rowData.data[fieldName].displayValue[0] = value;
  353. }
  354. rowData.checked = true;
  355. },
  356. /**
  357. * 获取显示的值
  358. * @author YangZhiJie 20210909
  359. */
  360. getDisplayValue(rowData, infoGridField) {
  361. let fieldValue = rowData.data[infoGridField.fieldName];
  362. if (fieldValue === undefined || fieldValue === null) {
  363. return '';
  364. } else {
  365. return fieldValue.displayValue[0];
  366. }
  367. },
  368. /**
  369. * 选择所有/取消选择的数据
  370. * @author YangZhiJie 20210909
  371. */
  372. selectAll: function (event) {
  373. var isChecked = event.currentTarget.checked;
  374. if (
  375. this.dataList === undefined ||
  376. this.dataList === null ||
  377. this.dataList.length == 0
  378. ) {
  379. return;
  380. }
  381. for (
  382. let index = 0, length = this.dataList.length;
  383. index < length;
  384. index++
  385. ) {
  386. this.dataList[index].checked = isChecked;
  387. }
  388. },
  389. /**
  390. * 设置dataList数据
  391. * @author YangZhiJie 20210908
  392. */
  393. setDataList: function (dataList) {
  394. let _self = this;
  395. if (dataList === null || dataList === undefined) {
  396. this.dataList.splice(0, this.dataList.length);
  397. return;
  398. }
  399. if (!Array.isArray(dataList)) {
  400. console.error('参数异常,参数不是数组类型。');
  401. console.error(dataList);
  402. return;
  403. }
  404. const tempDataList = JSON.parse(JSON.stringify(dataList));
  405. for (
  406. let index = 0, length = tempDataList.length;
  407. index < length;
  408. index++
  409. ) {
  410. if (this.isSelectedById != null) {
  411. tempDataList[index].checked = this.isSelectedById(
  412. tempDataList[index].id,
  413. );
  414. } else {
  415. tempDataList[index].checked = false;
  416. }
  417. }
  418. this.dataList = tempDataList;
  419. this.$nextTick(function () {
  420. _self.fixedTableHeader();
  421. });
  422. },
  423. },
  424. };
  425. </script>
  426. <style scoped>
  427. .flex-container-1 {
  428. display: flex;
  429. flex-direction: column;
  430. width: 100%;
  431. height: calc(100% - 10px);
  432. }
  433. .flex-content-1 {
  434. flex: 1;
  435. overflow: auto;
  436. width: 100%;
  437. margin-top: 5px;
  438. }
  439. .flex-footer-1 {
  440. height: 35px;
  441. /*放大缩小比例为0 */
  442. flex: 0 0 35px;
  443. }
  444. </style>
  445. <style scoped>
  446. .fixed-table {
  447. table-layout: fixed;
  448. }
  449. table.fixed-table th {
  450. position: relative;
  451. min-width: 10px;
  452. }
  453. table.fixed-table tr td:first-child,
  454. table.fixed-table tr th:first-child {
  455. text-align: center;
  456. }
  457. table.fixed-table th,
  458. table.fixed-table td {
  459. text-align: left;
  460. overflow: hidden;
  461. white-space: nowrap;
  462. text-overflow: ellipsis;
  463. padding: 0.1em;
  464. border-right: 1px solid rgba(0, 0, 0, 0.05);
  465. background-color: white;
  466. }
  467. table.fixed-table th .rz-handle {
  468. width: 10px;
  469. height: 100%;
  470. position: absolute;
  471. top: 0;
  472. right: 0;
  473. background: repeating-linear-gradient(
  474. 45deg,
  475. transparent,
  476. transparent 2px,
  477. rgba(0, 0, 0, 0.05) 2px,
  478. rgba(0, 0, 0, 0.05) 4px
  479. );
  480. cursor: ew-resize !important;
  481. }
  482. table.fixed-table th .rz-handle.rz-handle-active {
  483. border-right: 2px solid #000;
  484. transform: scaleX(100);
  485. background: rgba(0, 0, 0, 0.05) 2px;
  486. }
  487. .rz-handle:hover {
  488. background: rgba(0, 0, 0, 0.2) 4px;
  489. }
  490. </style>