ShowInList.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <div>{{ result }}</div>
  3. </template>
  4. <script lang="ts">
  5. import type { KeyValue } from "@imengyu/js-request-transform";
  6. import { defineComponent, type PropType } from "vue";
  7. export default defineComponent({
  8. name: "ShowInList",
  9. data() {
  10. return {
  11. result: ''
  12. }
  13. },
  14. props: {
  15. noMatchText: {
  16. type: String,
  17. default: '暂无',
  18. },
  19. useProp: {
  20. type: Boolean,
  21. default: true,
  22. },
  23. usePropName: {
  24. type: String,
  25. default: 'id',
  26. },
  27. usePropValue: {
  28. type: String,
  29. default: 'name',
  30. },
  31. list: {
  32. type: Object as PropType<Array<KeyValue>>,
  33. default: null,
  34. },
  35. value: {
  36. default: null,
  37. }
  38. },
  39. mounted: function() {
  40. this.loadText();
  41. },
  42. watch: {
  43. list() { this.loadText(); },
  44. value() { this.loadText(); }
  45. },
  46. methods: {
  47. loadText() {
  48. const list = this.list as Array<KeyValue>;
  49. if(list && this.value && list.length > 0){
  50. for(let i = 0, c = list.length; i < c; i++){
  51. if(this.useProp)
  52. if(list[i][this.usePropName as string] == this.value) {
  53. this.result = list[i][this.usePropValue as string] as string;
  54. return;
  55. }
  56. else
  57. if(list[i] == this.value) {
  58. this.result = list[i] as unknown as string;
  59. return;
  60. }
  61. }
  62. this.result = this.noMatchText as string;
  63. }else this.result = this.noMatchText as string;
  64. }
  65. }
  66. });
  67. </script>