统计
  • 建站日期:2019-12-01
  • 文章总数:2331 篇
  • 评论总数:2160 条
  • 分类总数:22 个
  • 最后更新:4月1日
文章 未分类

ant vue 项目入坑

程序员阿鑫
首页 未分类 正文


antvue项目入坑
-程序员阿鑫-带你一起秃头
-第1
张图片

antvue项目入坑 -程序员阿鑫-带你一起秃头 -第1 张图片

table 的列可以拖动

问题1 :demo 无法运行

1. 直接拷贝代码过来,发现传的参数有问题:

  1. <template>
  2. <a-table bordered :columns="columns" :components="components" :dataSource="data">
  3. <template v-slot:action>
  4. <a href="javascript:;">Delete</a>
  5. </template>
  6. </a-table>
  7. </template>
  8. <script>
  9. import Vue from 'vue';
  10. import VueDraggableResizable from 'vue-draggable-resizable';
  11. Vue.component('vue-draggable-resizable', VueDraggableResizable);
  12. const columns = [
  13. {
  14. title: 'Date',
  15. dataIndex: 'date',
  16. width: 200,
  17. },
  18. {
  19. title: 'Amount',
  20. dataIndex: 'amount',
  21. width: 100,
  22. },
  23. {
  24. title: 'Type',
  25. dataIndex: 'type',
  26. width: 100,
  27. },
  28. {
  29. title: 'Note',
  30. dataIndex: 'note',
  31. width: 100,
  32. },
  33. {
  34. title: 'Action',
  35. key: 'action',
  36. scopedSlots: { customRender: 'action' },
  37. },
  38. ];
  39. const data = [
  40. {
  41. key: 0,
  42. date: '2018-02-11',
  43. amount: 120,
  44. type: 'income',
  45. note: 'transfer',
  46. },
  47. {
  48. key: 1,
  49. date: '2018-03-11',
  50. amount: 243,
  51. type: 'income',
  52. note: 'transfer',
  53. },
  54. {
  55. key: 2,
  56. date: '2018-04-11',
  57. amount: 98,
  58. type: 'income',
  59. note: 'transfer',
  60. },
  61. ];
  62. const draggingMap = {};
  63. columns.forEach(col => {
  64. draggingMap[col.key] = col.width;
  65. });
  66. const draggingState = Vue.observable(draggingMap);
  67. const ResizeableTitle = (h, props, children) => {
  68. let thDom = null;
  69. const { key, ...restProps } = props;
  70. const col = columns.find(col => {
  71. const k = col.dataIndex || col.key;
  72. return k === key;
  73. });
  74. if (!col.width) {
  75. return <th {...restProps}>{children}</th>;
  76. }
  77. const onDrag = (x, y) => {
  78. draggingState[key] = 0;
  79. col.width = Math.max(x, 1);
  80. };
  81. const onDragstop = () => {
  82. draggingState[key] = thDom.getBoundingClientRect().width;
  83. };
  84. return (
  85. <th {...restProps} v-ant-ref={r => (thDom = r)} width={col.width} class="resize-table-th">
  86. {children}
  87. <vue-draggable-resizable
  88. key={col.key}
  89. class="table-draggable-handle"
  90. w={10}
  91. x={draggingState[key] || col.width}
  92. z={1}
  93. axis="x"
  94. draggable={true}
  95. resizable={false}
  96. onDragging={onDrag}
  97. onDragstop={onDragstop}
  98. ></vue-draggable-resizable>
  99. </th>
  100. );
  101. };
  102. export default {
  103. name: 'App',
  104. data() {
  105. this.components = {
  106. header: {
  107. cell: ResizeableTitle,
  108. },
  109. };
  110. return {
  111. data,
  112. columns,
  113. };
  114. },
  115. };
  116. </script>
  117. <style lang="less">
  118. .resize-table-th {
  119. position: relative;
  120. .table-draggable-handle {
  121. height: 100% !important;
  122. bottom: 0;
  123. left: auto !important;
  124. right: -5px;
  125. cursor: col-resize;
  126. touch-action: none;
  127. }
  128. }
  129. </style>

2. 将标黄的部分改为,代码如下:

  1. const ResizeableTitle = (h1) => {
  2. const props = h1.props
  3. const children = h1.children

问题二:应用到ant vue界面时,需要做如下修改:

1. 将下列代码挪到main.js

  1. import VueDraggableResizable from 'vue-draggable-resizable'
  2. Vue.component('vue-draggable-resizable', VueDraggableResizable)

 2. 将下列代码copy过去。

  1. import Vue from 'vue'
  2. // #region 从Table1 拷贝的东东
  3. const draggingMap = {}
  4. columns.forEach(col => {
  5. draggingMap[col.Id] = col.width
  6. })
  7. const draggingState = Vue.observable(draggingMap)
  8. const ResizeableTitle = (h1) => {
  9. const props = h1.props
  10. const children = h1.children
  11. let thDom = null
  12. const { key, ...restProps } = props
  13. const col = columns.find(col => {
  14. const k = col.dataIndex || col.Id
  15. return k === key
  16. })
  17. if ((!col) || (!col.width)) {
  18. return <th {...restProps}>{children}</th>
  19. }
  20. const onDrag = (x, y) => {
  21. draggingState[key] = 0
  22. col.width = Math.max(x, 1)
  23. }
  24. const onDragstop = () => {
  25. draggingState[key] = thDom.getBoundingClientRect().width
  26. }
  27. return (
  28. <th {...restProps} v-ant-ref={r => (thDom = r)} width={col.width} class="resize-table-th">
  29. {children}
  30. <vue-draggable-resizable
  31. key={col.rowKey}
  32. class="table-draggable-handle"
  33. w={10}
  34. x={draggingState[key] || col.width}
  35. z={1}
  36. axis="x"
  37. draggable={true}
  38. resizable={false}
  39. onDragging={onDrag}
  40. onDragstop={onDragstop}
  41. ></vue-draggable-resizable>
  42. </th>
  43. )
  44. }
  45. // #endregion

3. 拷贝样式

  1. <style lang="less">
  2. .resize-table-th {
  3. position: relative;
  4. .table-draggable-handle {
  5. height: 100% !important;
  6. bottom: 0;
  7. left: auto !important;
  8. right: -5px;
  9. cursor: col-resize;
  10. touch-action: none;
  11. }
  12. }
  13. </style>

4. 表格的width=100 必须为数字的

5.columns 操作列增加 key: 'action', Id: 'action'

const columns = [

...

{ title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }

]

const columns = [
...
  { title: '操作', dataIndex: 'action', key: 'action', Id: 'action', scopedSlots: { customRender: 'action' } }
]


6.<a-table>标签添加vue属性:components="components"

<a-table
ref="table"
:components="components"
:columns="columns"

7.在data中增加 this.components

  1. export default {
  2. components: {
  3. EditForm
  4. },
  5. mounted () {
  6. this.getDataList()
  7. },
  8. data () {
  9. this.components = {
  10. header: {
  11. cell: ResizeableTitle
  12. }
  13. }
  14. return {

 

版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!

-- 展开阅读全文 --
这篇文章最后更新于2021-6-18,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
前端知识点总结——H5新标签
« 上一篇
学编程太枯燥,整理11个边玩游戏边学编程网站,越学越上瘾
下一篇 »

发表评论


扫一扫,支付10
支付完成后,请等待5秒左右。手机请点此支付!