dataChecked.vue 4.8 KB

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