当前位置:   article > 正文

uniapp实现点击标签文本域中显示标签内容_uni标签

uni标签

先上一个效果图

 实现的效果有:

①.点击标签时,标签改变颜色并处于可删除状态

②.切换标签,文本域中出现标签的内容

③.点击标签右上角的删除可删掉标签,同时清除文本域中标签的内容

④.可输入内容,切换时不影响输入的内容

使用的是uniapp+vue3+uVieww-plus

代码:

  1. <template>
  2. <view class="mt32">
  3. <u--textarea v-model="textareaValue" placeholder="请输入未通过的原因!" height="200" count maxlength="30" ></u--textarea>
  4. <view class="mt32 flex flex-wrap justify-center align-items-center align-center">
  5. <view class="u-page__tag-item" v-for="(item, index) in radios" :key="index">
  6. <u-tag :show="item.show" shape="circle" :closable="item.closable" :bgColor="item.bgColor" :color="item.color" borderColor="#DCDCDC" :text="item.content" :plain="!item.checked" :name="index"
  7. @click="radioClick" @close="close(item)">
  8. </u-tag>
  9. </view>
  10. </view>
  11. <view class="flex justify-center align-items-center">
  12. <view class="mt32 tjBtn" @click = "submit">提交</view>
  13. </view>
  14. </view>
  15. </template>
  16. <script setup>
  17. import {
  18. reactive,
  19. ref,
  20. toRaw,
  21. onMounted,
  22. watch,
  23. computed
  24. } from 'vue';
  25. import {
  26. onLoad,onReady
  27. } from '@dcloudio/uni-app'
  28. // const textareaValue = ref('')
  29. const radios = ref([
  30. {
  31. checked: true,
  32. show:true,
  33. closable:false,
  34. bgColor: '#ffffff',
  35. color:'rgba(0, 0, 0, 0.90)',
  36. content:'商机对接1'
  37. },
  38. {
  39. checked: false,
  40. show:true,
  41. closable:false,
  42. bgColor: '#ffffff',
  43. color:'rgba(0, 0, 0, 0.90)',
  44. content:'商机对接2'
  45. },
  46. {
  47. checked: false,
  48. show:true,
  49. bgColor: '#ffffff',
  50. color:'rgba(0, 0, 0, 0.90)',
  51. content:'商机对接3'
  52. },
  53. {
  54. checked: false,
  55. show:true,
  56. closable:false,
  57. bgColor: '#ffffff',
  58. color:'rgba(0, 0, 0, 0.90)',
  59. content:'商机对接4'
  60. },
  61. {
  62. checked: false,
  63. show:true,
  64. closable:false,
  65. bgColor: '#ffffff',
  66. color:'rgba(0, 0, 0, 0.90)',
  67. content:'商机对接5'
  68. },
  69. ])
  70. const reason1 = ref('');
  71. const reason2 = ref('');
  72. const textareaValue = computed({
  73. get: () => reason1.value + reason2.value,
  74. set: (value) => {
  75. for (let i = 0; i < radios.value.length; i++) {
  76. if (value.includes(radios.value[i].content)) {
  77. reason1.value = radios.value[i].content;
  78. reason2.value = value.replace(radios.value[i].content, '');
  79. return;
  80. }
  81. }
  82. }
  83. })
  84. const radioClick = (name)=> {
  85. // console.log('radios.value',name)
  86. radios.value.map((item, index) => {
  87. if(index === name){
  88. item.checked = true
  89. item.bgColor = 'rgba(0, 82, 217, 0.70)'
  90. item.color = '#ffffff'
  91. item.closable = true
  92. reason1.value = item.content
  93. }else{
  94. item.checked = false
  95. item.bgColor = '#ffffff'
  96. item.color = 'rgba(0, 0, 0, 0.90)'
  97. item.closable = false
  98. }
  99. })
  100. }
  101. const close = (item)=>{
  102. console.log('关闭')
  103. item.show = false
  104. reason1.value = ''
  105. }
  106. const submit = ()=> {}
  107. </script>
  108. <style>
  109. page{
  110. background: #F5F5F5;
  111. }
  112. .u-page__tag-item{
  113. height: auto;
  114. margin:0 20rpx 20rpx 0;
  115. }
  116. .u-page__tag-item text {
  117. display: inline-block;
  118. padding: 18rpx 32rpx;
  119. }
  120. .tjBtn{
  121. background: #0052D9;
  122. color: #fff;
  123. border-radius: 6px;
  124. padding: 24rpx 118rpx;
  125. }
  126. </style>

 有些样式在全局定义的,自行设置样式。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/245478
推荐阅读
相关标签
  

闽ICP备14008679号