AddShouShuTable.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <script setup lang="ts">
  2. import {useVModel} from "@vueuse/core";
  3. import XEUtils from "xe-utils";
  4. import {nextTick, onMounted, Ref, ref} from 'vue'
  5. import XcComboGridV2 from "@/components/xiao-chan/combo-grid/XcComboGridV2.vue";
  6. import {windowSizeStore} from "@/utils/store-public";
  7. import {useCompRef} from "@/utils/useCompRef";
  8. import {ElTable} from "element-plus";
  9. // @ts-ignore
  10. import Sortable from 'sortablejs';
  11. const props = defineProps({
  12. data: Array,
  13. title: String,
  14. queryFunction: Function,
  15. queryData: Array
  16. })
  17. const emits = defineEmits(['update:data', 'update:queryData', 'changeList'])
  18. const modelData = useVModel(props, 'data', emits) as Ref<any[]>
  19. const queryDayaModel = useVModel(props, 'queryData', emits) as Ref<any[]>
  20. const temp = ref({})
  21. const findIndex = (arr: any[], name: string, value: string) => {
  22. return XEUtils.findIndexOf(arr, item => {
  23. return item[name] === value;
  24. })
  25. }
  26. const delClock = (index) => {
  27. modelData.value.splice(index, 1)
  28. emits('changeList')
  29. }
  30. const rowClick = (row) => {
  31. const index = findIndex(modelData.value, 'code', row.code)
  32. if (index > -1) {
  33. delClock(index);
  34. } else {
  35. modelData.value.push({
  36. code: row.code,
  37. name: row.name,
  38. opScale: row.opScale
  39. })
  40. }
  41. emits('changeList')
  42. }
  43. const tableRef = useCompRef(ElTable)
  44. const initSortable = () => {
  45. const el = tableRef.value.$el.querySelector('tbody')
  46. const opt = {
  47. animation: 200,
  48. handle: '.el-table__row',
  49. onEnd({newIndex, oldIndex}) {
  50. const currRow = modelData.value.splice(oldIndex, 1)[0]
  51. modelData.value.splice(newIndex, 0, currRow)
  52. emits('changeList')
  53. }
  54. }
  55. Sortable.create(el, opt)
  56. }
  57. onMounted(async () => {
  58. await nextTick()
  59. initSortable();
  60. })
  61. </script>
  62. <template>
  63. {{ props.title }}
  64. <br/>
  65. <el-table :data="props.data"
  66. row-key="code"
  67. ref="tableRef"
  68. :max-height="windowSizeStore.h / 1.2 - 20">
  69. <el-table-column :label="`${props.title}名称`" prop="name">
  70. <template #default="{row,$index}">
  71. <span class="ss_text" :class="$index ===0? 'main' : 'secondary'">
  72. {{ $index === 0 ? '主' : '次' }}
  73. </span>
  74. {{ row.name }}
  75. </template>
  76. </el-table-column>
  77. <el-table-column>
  78. <template #default="{$index}">
  79. <el-button type="danger" @click="delClock($index)">删除</el-button>
  80. </template>
  81. </el-table-column>
  82. </el-table>
  83. <XcComboGridV2 v-model="temp"
  84. code-name="aa"
  85. :row-click-hide="false"
  86. @rowClick="rowClick"
  87. v-model:data="queryDayaModel"
  88. :query-data-func="props.queryFunction"/>
  89. </template>
  90. <style>
  91. .ss_text {
  92. padding: 3px;
  93. border-radius: 5px;
  94. margin-right: 5px;
  95. &.main {
  96. background: red;
  97. }
  98. &.secondary {
  99. background: #8f8787;
  100. }
  101. }
  102. </style>