dataChecked.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. selectedNames: {
  66. type: String,
  67. default: ''
  68. }
  69. },
  70. data() {
  71. return {
  72. showAll: false,
  73. selectedData: [],
  74. newName: ''
  75. };
  76. },
  77. computed: {
  78. displayedList() {
  79. return this.showAll ? this.list : this.list.slice(0, this.defaultCount);
  80. },
  81. remainingCount() {
  82. return this.list.length - this.defaultCount;
  83. },
  84. showExpandButton() {
  85. return this.list.length > this.defaultCount;
  86. },
  87. showAddButton() {
  88. if (!this.showAdd) return false;
  89. return this.showAll || this.list.length < this.defaultCount;
  90. }
  91. },
  92. watch: {
  93. // 监听外部传入的selectedIds变化,更新selectedData
  94. selectedIds: {
  95. handler(newVal) {
  96. if (this.mode === 'single') {
  97. // 单选模式
  98. const selectedItem = this.list.find(item => item.id == newVal);
  99. this.selectedData = selectedItem ? [selectedItem] : [];
  100. } else {
  101. // 多选模式
  102. this.selectedData = this.list.filter(item => newVal.includes(item.id));
  103. }
  104. }
  105. },
  106. // 监听外部传入的selectedNames变化,更新selectedData
  107. selectedNames: {
  108. immediate: true,
  109. handler(newVal) {
  110. if (!newVal) {
  111. this.selectedData = [];
  112. return;
  113. }
  114. // 将字符串解析为名称数组
  115. const names = newVal.split(',').map(name => name.trim());
  116. // 根据名称从list中找到对应的数据
  117. this.selectedData = this.list.filter(item => names.includes(item.name));
  118. }
  119. }
  120. },
  121. methods: {
  122. isSelected(id) {
  123. return this.selectedData.some(item => item.id === id);
  124. },
  125. // toggleSelect(data) {
  126. // const index = this.selectedData.indexOf(data);
  127. // console.log('index', index);
  128. // if (index == -1) {
  129. // this.selectedData.push(data);
  130. // } else {
  131. // this.selectedData.splice(index, 1);
  132. // }
  133. // console.log('this.selectedData', this.selectedData);
  134. // this.$emit('change', this.selectedData);
  135. // },
  136. toggleSelect(data) {
  137. if (this.mode == 'single') {
  138. this.handleSingleSelect(data);
  139. } else {
  140. this.handleMultipleSelect(data);
  141. }
  142. this.$emit('change', this.selectedData);
  143. },
  144. // 单选处理逻辑
  145. handleSingleSelect(data) {
  146. const isSelected = this.selectedData.some(item => item.id == data.id);
  147. if (isSelected) {
  148. // 取消选择
  149. this.selectedData = [];
  150. } else {
  151. // 替换选择
  152. this.selectedData = [data];
  153. }
  154. },
  155. // 多选处理逻辑
  156. handleMultipleSelect(data) {
  157. const index = this.selectedData.findIndex(item => item.id == data.id);
  158. if (index === -1) {
  159. this.selectedData.push(data);
  160. } else {
  161. this.selectedData.splice(index, 1);
  162. }
  163. },
  164. toggleExpand() {
  165. this.showAll = !this.showAll;
  166. },
  167. handleAdd() {
  168. this.$refs.addPopup.open();
  169. },
  170. confirmAdd() {
  171. if (!this.newName.trim()) {
  172. uni.showToast({
  173. title: '请输入技能名称',
  174. icon: 'none'
  175. });
  176. return;
  177. }
  178. const newObject = {
  179. id: this.list.length,
  180. name: this.newName
  181. };
  182. console.log('list', this.list);
  183. console.log('newObject', newObject);
  184. this.$emit('add', newObject);
  185. this.newName = '';
  186. this.$refs.addPopup.close();
  187. },
  188. cancelAdd() {
  189. this.newName = '';
  190. this.$refs.addPopup.close();
  191. }
  192. }
  193. };
  194. </script>
  195. <style scoped>
  196. .add-block {
  197. background: transparent;
  198. border: 1px dashed #007aff;
  199. color: #007aff;
  200. }
  201. .expand-button {
  202. margin-top: 30rpx;
  203. color: #007aff;
  204. font-size: 28rpx;
  205. text-align: center;
  206. }
  207. .add-popup {
  208. padding: 40rpx;
  209. background: #fff;
  210. border-radius: 16rpx;
  211. }
  212. .add-input {
  213. width: 100%;
  214. padding: 20rpx;
  215. border: 1px solid #eee;
  216. border-radius: 8rpx;
  217. margin-bottom: 40rpx;
  218. }
  219. .button-group {
  220. display: flex;
  221. gap: 20rpx;
  222. }
  223. .confirm-button {
  224. flex: 1;
  225. background: #007aff;
  226. color: #fff;
  227. }
  228. .cancel-button {
  229. flex: 1;
  230. background: #ff3b30;
  231. color: #fff;
  232. }
  233. </style>