dictDemo.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!--
  2. 字典组件使用示例
  3. 演示如何使用 useDict、DictTag 和 DictSelect
  4. -->
  5. <template>
  6. <div class="dict-demo-page">
  7. <n-card title="字典组件使用示例">
  8. <n-space vertical :size="24">
  9. <!-- 1. 字典数据加载 -->
  10. <n-card title="1. 字典数据加载(useDict)" size="small">
  11. <n-spin :show="loading">
  12. <n-space vertical>
  13. <div>
  14. <n-text strong>
  15. 加载的字典类型:
  16. </n-text>
  17. <n-text>sys_yes_no, sys_normal_disable</n-text>
  18. </div>
  19. <n-divider />
  20. <div>
  21. <n-text strong>
  22. sys_yes_no 字典数据:
  23. </n-text>
  24. <pre>{{ JSON.stringify(dict.sys_yes_no, null, 2) }}</pre>
  25. </div>
  26. <div>
  27. <n-text strong>
  28. sys_normal_disable 字典数据:
  29. </n-text>
  30. <pre>{{ JSON.stringify(dict.sys_normal_disable, null, 2) }}</pre>
  31. </div>
  32. <n-button @click="reload()">
  33. 重新加载字典
  34. </n-button>
  35. </n-space>
  36. </n-spin>
  37. </n-card>
  38. <!-- 2. 字典标签显示 -->
  39. <n-card title="2. 字典标签显示(DictTag)" size="small">
  40. <n-space vertical>
  41. <div>
  42. <n-text strong>
  43. 使用 options 属性:
  44. </n-text>
  45. <n-space>
  46. <DictTag :options="dict.sys_yes_no" value="Y" />
  47. <DictTag :options="dict.sys_yes_no" value="N" />
  48. </n-space>
  49. </div>
  50. <div>
  51. <n-text strong>
  52. 使用 dictType 属性:
  53. </n-text>
  54. <n-space>
  55. <DictTag dict-type="sys_yes_no" value="Y" />
  56. <DictTag dict-type="sys_yes_no" value="N" />
  57. </n-space>
  58. </div>
  59. <div>
  60. <n-text strong>
  61. 自定义类型:
  62. </n-text>
  63. <n-space>
  64. <DictTag :options="dict.sys_normal_disable" value="0" type="success" />
  65. <DictTag :options="dict.sys_normal_disable" value="1" type="error" />
  66. </n-space>
  67. </div>
  68. <div>
  69. <n-text strong>
  70. 所有标签类型:
  71. </n-text>
  72. <n-space>
  73. <DictTag :options="[{ label: '默认', value: '1', listClass: 'default' }]" value="1" />
  74. <DictTag :options="[{ label: '成功', value: '1', listClass: 'success' }]" value="1" />
  75. <DictTag :options="[{ label: '信息', value: '1', listClass: 'info' }]" value="1" />
  76. <DictTag :options="[{ label: '警告', value: '1', listClass: 'warning' }]" value="1" />
  77. <DictTag :options="[{ label: '错误', value: '1', listClass: 'error' }]" value="1" />
  78. </n-space>
  79. </div>
  80. <div>
  81. <n-text strong>
  82. 不同尺寸:
  83. </n-text>
  84. <n-space>
  85. <DictTag :options="dict.sys_yes_no" value="Y" size="small" />
  86. <DictTag :options="dict.sys_yes_no" value="Y" size="medium" />
  87. <DictTag :options="dict.sys_yes_no" value="Y" size="large" />
  88. </n-space>
  89. </div>
  90. <div>
  91. <n-text strong>
  92. 圆角和无边框:
  93. </n-text>
  94. <n-space>
  95. <DictTag :options="dict.sys_yes_no" value="Y" round />
  96. <DictTag :options="dict.sys_yes_no" value="Y" :bordered="false" />
  97. <DictTag :options="dict.sys_yes_no" value="Y" round :bordered="false" />
  98. </n-space>
  99. </div>
  100. </n-space>
  101. </n-card>
  102. <!-- 3. 字典选择器 -->
  103. <n-card title="3. 字典选择器(DictSelect)" size="small">
  104. <n-space vertical>
  105. <div>
  106. <n-text strong>
  107. 单选:
  108. </n-text>
  109. <DictSelect
  110. v-model:value="formData.yesNo"
  111. dict-type="sys_yes_no"
  112. placeholder="请选择是否"
  113. style="width: 200px"
  114. />
  115. <n-text>选中值:{{ formData.yesNo }}</n-text>
  116. </div>
  117. <div>
  118. <n-text strong>
  119. 多选:
  120. </n-text>
  121. <DictSelect
  122. v-model:value="formData.status"
  123. dict-type="sys_normal_disable"
  124. multiple
  125. placeholder="请选择状态"
  126. style="width: 300px"
  127. />
  128. <n-text>选中值:{{ formData.status }}</n-text>
  129. </div>
  130. <div>
  131. <n-text strong>
  132. 禁用状态:
  133. </n-text>
  134. <DictSelect
  135. v-model:value="formData.disabled"
  136. dict-type="sys_yes_no"
  137. disabled
  138. style="width: 200px"
  139. />
  140. </div>
  141. </n-space>
  142. </n-card>
  143. <!-- 4. 在表格中使用 -->
  144. <n-card title="4. 在表格中使用" size="small">
  145. <n-data-table
  146. :columns="tableColumns"
  147. :data="tableData"
  148. :pagination="false"
  149. />
  150. </n-card>
  151. <!-- 5. 辅助函数 -->
  152. <n-card title="5. 辅助函数" size="small">
  153. <n-space vertical>
  154. <div>
  155. <n-text strong>
  156. getLabel('sys_yes_no', 'Y'):
  157. </n-text>
  158. <n-text>{{ getLabel('sys_yes_no', 'Y') }}</n-text>
  159. </div>
  160. <div>
  161. <n-text strong>
  162. getDict('sys_yes_no', 'N'):
  163. </n-text>
  164. <pre>{{ JSON.stringify(getDict('sys_yes_no', 'N'), null, 2) }}</pre>
  165. </div>
  166. </n-space>
  167. </n-card>
  168. </n-space>
  169. </n-card>
  170. </div>
  171. </template>
  172. <script setup>
  173. import { h, ref } from 'vue'
  174. import DictSelect from '@/components/DictSelect.vue'
  175. import DictTag from '@/components/DictTag.vue'
  176. import { useDict } from '@/composables/useDict'
  177. defineOptions({ name: 'DictDemo' })
  178. // 加载字典
  179. const { dict, loading, reload, getLabel, getDict } = useDict('sys_yes_no', 'sys_normal_disable')
  180. // 表单数据
  181. const formData = ref({
  182. yesNo: '',
  183. status: [],
  184. disabled: 'Y',
  185. })
  186. // 表格数据
  187. const tableData = ref([
  188. { id: 1, name: '张三', isActive: 'Y', status: '0' },
  189. { id: 2, name: '李四', isActive: 'N', status: '1' },
  190. { id: 3, name: '王五', isActive: 'Y', status: '0' },
  191. ])
  192. // 表格列
  193. const tableColumns = [
  194. { key: 'id', title: 'ID', width: 80 },
  195. { key: 'name', title: '姓名', width: 120 },
  196. {
  197. key: 'isActive',
  198. title: '是否激活',
  199. width: 120,
  200. render: row => h(DictTag, { options: dict.value.sys_yes_no, value: row.isActive }),
  201. },
  202. {
  203. key: 'status',
  204. title: '状态',
  205. width: 120,
  206. render: row => h(DictTag, { options: dict.value.sys_normal_disable, value: row.status }),
  207. },
  208. ]
  209. </script>
  210. <style scoped>
  211. .dict-demo-page {
  212. padding: 16px;
  213. }
  214. pre {
  215. background: #f5f5f5;
  216. padding: 12px;
  217. border-radius: 4px;
  218. font-size: 12px;
  219. overflow-x: auto;
  220. }
  221. </style>