MoreText.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <span class="more-text">
  3. <el-popover
  4. v-if="moreContent"
  5. placement="top"
  6. width="300"
  7. trigger="hover"
  8. :content="moreContent"
  9. >
  10. <span slot="reference">{{ showContent }}...</span>
  11. </el-popover>
  12. <span v-else>{{ showContent }}</span>
  13. </span>
  14. </template>
  15. <script>
  16. import { objTypeOf } from "@/plugins/utils";
  17. export default {
  18. name: "more-text",
  19. props: {
  20. data: {
  21. type: [Array, String],
  22. default: null,
  23. },
  24. showCount: {
  25. type: Number,
  26. default: 1,
  27. },
  28. },
  29. data() {
  30. return {
  31. showContent: "",
  32. moreContent: "",
  33. };
  34. },
  35. watch: {
  36. data(val) {
  37. this.initData();
  38. },
  39. },
  40. created() {
  41. this.initData();
  42. },
  43. methods: {
  44. initData() {
  45. if (!this.data) return;
  46. const type = objTypeOf(this.data);
  47. if (type === "array") {
  48. this.showContent = this.data.slice(0, this.showCount).join(",");
  49. this.moreContent = "";
  50. if (this.data.length > this.showCount) {
  51. this.moreContent = this.data.join(",");
  52. }
  53. } else if (type === "string") {
  54. this.showContent = this.data.substring(0, this.showCount);
  55. this.moreContent = "";
  56. if (this.data.length > this.showCount) {
  57. this.moreContent = this.data;
  58. }
  59. }
  60. },
  61. },
  62. };
  63. </script>