dataChecked.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <view class="phone-radio-group data-check-radio-group">
  3. <!-- 技能块展示 -->
  4. <view v-for="item in displayedList" :key="item.id" class="phone-radio-item"
  5. :class="{ radioActive: isSelected(item.id) }" @click="toggleSelect(item)">
  6. {{ item.name }}
  7. </view>
  8. <!-- 添加按钮 -->
  9. <view v-if="showAddButton" class="radio-add-btn" @click="handleAdd">
  10. + 添加
  11. </view>
  12. <!-- 展开/收起按钮 -->
  13. <view class="radio-btn-box label-radio-btn-box">
  14. <view v-if="showExpandButton" :class="[{ 'radio-btn': true }, showAll ? 'collapsed-btn' : 'expanded-btn']"
  15. @click="toggleExpand">
  16. {{ showAll ? '收起' : `展开` }}
  17. </view>
  18. </view>
  19. <!-- <view v-if="showExpandButton" class="expand-button" @click="toggleExpand">
  20. {{ showAll ? '收起' : `展开剩余${remainingCount}项` }}
  21. </view> -->
  22. <!-- 添加弹窗 -->
  23. <uni-popup ref="addPopup" type="dialog" :animation="false" :is-mask-click="false">
  24. <view class="phone-common-dialog">
  25. <view class="common-body-box">
  26. <view class="common-title">新增技能</view>
  27. <view class="common-input-box">
  28. <input v-model="newName" placeholder="请输入技能名称" class="common-input-margin" />
  29. </view>
  30. </view>
  31. <view class="common-btn-box">
  32. <view class="not-confirm-btn" @click="cancelAdd">取消</view>
  33. <view class="confirm-btn" @click="confirmAdd">确认</view>
  34. </view>
  35. </view>
  36. </uni-popup>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. name: 'SkillSelector',
  42. props: {
  43. // 技能列表(必须包含id和name字段)
  44. list: {
  45. type: Array,
  46. required: true,
  47. default: () => []
  48. },
  49. // 默认显示数量
  50. defaultCount: {
  51. type: Number,
  52. default: 10
  53. },
  54. // 是否显示添加功能
  55. showAdd: {
  56. type: Boolean,
  57. default: true
  58. },
  59. // 新增模式选择
  60. mode: {
  61. type: String,
  62. default: 'multiple', // 'single' | 'multiple'
  63. validator: (value) => ['single', 'multiple'].includes(value)
  64. },
  65. // 外部传入的已选id(单选时为单个id,多选时为id数组)
  66. selectedIds: {
  67. type: [Array, Number,String],
  68. default: () => []
  69. },
  70. selectedNames: {
  71. type: String,
  72. default: ''
  73. }
  74. },
  75. data() {
  76. return {
  77. showAll: false,
  78. selectedData: [],
  79. newName: ''
  80. };
  81. },
  82. computed: {
  83. displayedList() {
  84. return this.showAll ? this.list : this.list.slice(0, this.defaultCount);
  85. },
  86. remainingCount() {
  87. return this.list.length - this.defaultCount;
  88. },
  89. showExpandButton() {
  90. return this.list.length > this.defaultCount;
  91. },
  92. showAddButton() {
  93. if (!this.showAdd) return false;
  94. return this.showAll || this.list.length < this.defaultCount;
  95. }
  96. },
  97. watch: {
  98. // 监听外部传入的selectedIds变化,更新selectedData
  99. selectedIds: {
  100. handler(newVal) {
  101. if (this.mode === 'single') {
  102. // 单选模式
  103. const selectedItem = this.list.find(item => item.id == newVal);
  104. this.selectedData = selectedItem ? [selectedItem] : [];
  105. } else {
  106. // 多选模式
  107. this.selectedData = this.list.filter(item => newVal.includes(item.id));
  108. }
  109. }
  110. },
  111. // 监听外部传入的selectedNames变化,更新selectedData
  112. selectedNames: {
  113. immediate: true,
  114. handler(newVal) {
  115. if (!newVal) {
  116. this.selectedData = [];
  117. return;
  118. }
  119. // 将字符串解析为名称数组
  120. const names = newVal.split(',').map(name => name.trim());
  121. // 根据名称从list中找到对应的数据
  122. this.selectedData = this.list.filter(item => names.includes(item.name));
  123. }
  124. }
  125. },
  126. methods: {
  127. isSelected(id) {
  128. return this.selectedData.some(item => item.id === id);
  129. },
  130. toggleSelect(data) {
  131. if (this.mode == 'single') {
  132. this.handleSingleSelect(data);
  133. } else {
  134. this.handleMultipleSelect(data);
  135. }
  136. this.$emit('change', this.selectedData);
  137. },
  138. // 单选处理逻辑
  139. handleSingleSelect(data) {
  140. const isSelected = this.selectedData.some(item => item.id == data.id);
  141. if (isSelected) {
  142. // 取消选择
  143. this.selectedData = [];
  144. } else {
  145. // 替换选择
  146. this.selectedData = [data];
  147. }
  148. },
  149. // 多选处理逻辑
  150. handleMultipleSelect(data) {
  151. const index = this.selectedData.findIndex(item => item.id == data.id);
  152. if (index === -1) {
  153. this.selectedData.push(data);
  154. } else {
  155. this.selectedData.splice(index, 1);
  156. }
  157. },
  158. toggleExpand() {
  159. this.showAll = !this.showAll;
  160. },
  161. handleAdd() {
  162. this.$refs.addPopup.open();
  163. },
  164. confirmAdd() {
  165. if (!this.newName.trim()) {
  166. uni.showToast({
  167. title: '请输入技能名称',
  168. icon: 'none'
  169. });
  170. return;
  171. }
  172. const newObject = {
  173. id: this.list.length,
  174. name: this.newName
  175. };
  176. console.log('list', this.list);
  177. console.log('newObject', newObject);
  178. this.$emit('add', newObject);
  179. this.newName = '';
  180. this.$refs.addPopup.close();
  181. },
  182. cancelAdd() {
  183. this.newName = '';
  184. this.$refs.addPopup.close();
  185. }
  186. }
  187. };
  188. </script>
  189. <style scoped>
  190. .add-block {
  191. background: transparent;
  192. border: 1px dashed #007aff;
  193. color: #007aff;
  194. }
  195. .expand-button {
  196. margin-top: 30rpx;
  197. color: #007aff;
  198. font-size: 28rpx;
  199. text-align: center;
  200. }
  201. .add-popup {
  202. padding: 40rpx;
  203. background: #fff;
  204. border-radius: 16rpx;
  205. }
  206. .add-input {
  207. width: 100%;
  208. padding: 20rpx;
  209. border: 1px solid #eee;
  210. border-radius: 8rpx;
  211. margin-bottom: 40rpx;
  212. }
  213. .button-group {
  214. display: flex;
  215. gap: 20rpx;
  216. }
  217. .confirm-button {
  218. flex: 1;
  219. background: #007aff;
  220. color: #fff;
  221. }
  222. .cancel-button {
  223. flex: 1;
  224. background: #ff3b30;
  225. color: #fff;
  226. }
  227. </style>