DocGeneratorGrid.vue 13 KB

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