index.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <confirm-dialog
  3. ref="confirmDialg"
  4. :size="sizeClassName"
  5. v-bind="$attrs"
  6. @confirm="onConfirm"
  7. >
  8. <template #default>
  9. <schema-table
  10. ref="table"
  11. :schema="tableSchema"
  12. :row-key="rowKey"
  13. :current-row-key="selectedKey"
  14. highlight-current-row
  15. @row-click="onClickRow"
  16. >
  17. <template
  18. v-if="hasHeader"
  19. #header="scope"
  20. >
  21. <slot
  22. name="header"
  23. :item="selectedRow"
  24. v-bind="scope"
  25. />
  26. </template>
  27. </schema-table>
  28. </template>
  29. </confirm-dialog>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'RadioTableDialog',
  34. props: {
  35. schema: {
  36. type: Object,
  37. required: true
  38. },
  39. size: {
  40. type: String,
  41. default: 'medium'
  42. },
  43. message: {
  44. type: String,
  45. default: '请选择内容'
  46. },
  47. rowKey: {
  48. type: String,
  49. default: 'id'
  50. }
  51. },
  52. data () {
  53. return {
  54. selectedRow: null
  55. }
  56. },
  57. computed: {
  58. hasHeader () {
  59. return !!this.$scopedSlots.header
  60. },
  61. selectedKey () {
  62. return this.selectedRow?.[this.rowKey]
  63. },
  64. tableSchema () {
  65. const { cols, ...data } = this.schema
  66. return {
  67. ...data,
  68. cols: [
  69. { render: (data, h) => h(
  70. 'span',
  71. { staticClass: `el-radio__input ${data[this.rowKey] === this.selectedKey ? 'is-checked' : ''}` },
  72. [h('span', { staticClass: 'el-radio__inner' })]
  73. ), width: 60, align: 'center' },
  74. ...cols
  75. ]
  76. }
  77. },
  78. sizeClassName () {
  79. return `table ${this.size} fixed`
  80. }
  81. },
  82. methods: {
  83. show (current) {
  84. this.selectedRow = current
  85. this.$refs.confirmDialg.show()
  86. },
  87. onConfirm (done) {
  88. if (!this.selectedKey) {
  89. this.$message({
  90. type: 'warning',
  91. message: this.message
  92. })
  93. return
  94. }
  95. this.$emit('confirm', {
  96. value: this.selectedRow,
  97. done
  98. })
  99. },
  100. onClickRow (row) {
  101. this.selectedRow = row
  102. }
  103. }
  104. }
  105. </script>