dataChecked.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <view class="form-radio-group">
  3. <!-- 技能块展示 -->
  4. <view v-for="item in displayedList" :key="item.id" class="form-radio-item"
  5. :class="{ genderActive: 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']" @click="toggleExpand">
  15. {{ showAll ? '收起' : `展开` }}
  16. </view>
  17. </view>
  18. <!-- <view v-if="showExpandButton" class="expand-button" @click="toggleExpand">
  19. {{ showAll ? '收起' : `展开剩余${remainingCount}项` }}
  20. </view> -->
  21. <!-- 添加弹窗 -->
  22. <uni-popup ref="addPopup" type="dialog">
  23. <view class="add-popup">
  24. <input v-model="newName" placeholder="请输入技能名称" class="add-input" />
  25. <view class="button-group">
  26. <button @click="confirmAdd" class="confirm-button">确认</button>
  27. <button @click="cancelAdd" class="cancel-button">取消</button>
  28. </view>
  29. </view>
  30. </uni-popup>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. name: 'SkillSelector',
  36. props: {
  37. // 技能列表(必须包含id和name字段)
  38. list: {
  39. type: Array,
  40. required: true,
  41. default: () => []
  42. },
  43. // 默认显示数量
  44. defaultCount: {
  45. type: Number,
  46. default: 10
  47. },
  48. // 是否显示添加功能
  49. showAdd: {
  50. type: Boolean,
  51. default: true
  52. },
  53. // 新增模式选择
  54. mode: {
  55. type: String,
  56. default: 'multiple', // 'single' | 'multiple'
  57. validator: (value) => ['single', 'multiple'].includes(value)
  58. }
  59. },
  60. data() {
  61. return {
  62. showAll: false,
  63. selectedData: [],
  64. newName: ''
  65. };
  66. },
  67. computed: {
  68. displayedList() {
  69. return this.showAll ? this.list : this.list.slice(0, this.defaultCount);
  70. },
  71. remainingCount() {
  72. return this.list.length - this.defaultCount;
  73. },
  74. showExpandButton() {
  75. return this.list.length > this.defaultCount;
  76. },
  77. showAddButton() {
  78. if (!this.showAdd) return false;
  79. return this.showAll || this.list.length < this.defaultCount;
  80. }
  81. },
  82. methods: {
  83. isSelected(id) {
  84. return this.selectedData.some(item => item.id === id);
  85. },
  86. // toggleSelect(data) {
  87. // const index = this.selectedData.indexOf(data);
  88. // console.log('index', index);
  89. // if (index == -1) {
  90. // this.selectedData.push(data);
  91. // } else {
  92. // this.selectedData.splice(index, 1);
  93. // }
  94. // console.log('this.selectedData', this.selectedData);
  95. // this.$emit('change', this.selectedData);
  96. // },
  97. toggleSelect(data) {
  98. if (this.mode == 'single') {
  99. this.handleSingleSelect(data);
  100. } else {
  101. this.handleMultipleSelect(data);
  102. }
  103. this.$emit('change', this.selectedData);
  104. },
  105. // 单选处理逻辑
  106. handleSingleSelect(data) {
  107. const isSelected = this.selectedData.some(item => item.id == data.id);
  108. if (isSelected) {
  109. // 取消选择
  110. this.selectedData = [];
  111. } else {
  112. // 替换选择
  113. this.selectedData = [data];
  114. }
  115. },
  116. // 多选处理逻辑
  117. handleMultipleSelect(data) {
  118. const index = this.selectedData.findIndex(item => item.id == data.id);
  119. if (index === -1) {
  120. this.selectedData.push(data);
  121. } else {
  122. this.selectedData.splice(index, 1);
  123. }
  124. },
  125. toggleExpand() {
  126. this.showAll = !this.showAll;
  127. },
  128. handleAdd() {
  129. this.$refs.addPopup.open();
  130. },
  131. confirmAdd() {
  132. if (!this.newName.trim()) {
  133. uni.showToast({
  134. title: '请输入技能名称',
  135. icon: 'none'
  136. });
  137. return;
  138. }
  139. const newObject = {
  140. id: this.list.length, // 使用时间戳作为临时ID
  141. name: this.newName
  142. };
  143. console.log('list', this.list);
  144. console.log('newObject', newObject);
  145. this.$emit('add', newObject);
  146. this.newName = '';
  147. this.$refs.addPopup.close();
  148. },
  149. cancelAdd() {
  150. this.newName = '';
  151. this.$refs.addPopup.close();
  152. }
  153. }
  154. };
  155. </script>
  156. <style scoped>
  157. .add-block {
  158. background: transparent;
  159. border: 1px dashed #007aff;
  160. color: #007aff;
  161. }
  162. .expand-button {
  163. margin-top: 30rpx;
  164. color: #007aff;
  165. font-size: 28rpx;
  166. text-align: center;
  167. }
  168. .add-popup {
  169. padding: 40rpx;
  170. background: #fff;
  171. border-radius: 16rpx;
  172. }
  173. .add-input {
  174. width: 100%;
  175. padding: 20rpx;
  176. border: 1px solid #eee;
  177. border-radius: 8rpx;
  178. margin-bottom: 40rpx;
  179. }
  180. .button-group {
  181. display: flex;
  182. gap: 20rpx;
  183. }
  184. .confirm-button {
  185. flex: 1;
  186. background: #007aff;
  187. color: #fff;
  188. }
  189. .cancel-button {
  190. flex: 1;
  191. background: #ff3b30;
  192. color: #fff;
  193. }
  194. </style>