dataChecked.vue 5.2 KB

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