dataChecked.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // const index = this.selectedData.indexOf(data);
  132. // console.log('index', index);
  133. // if (index == -1) {
  134. // this.selectedData.push(data);
  135. // } else {
  136. // this.selectedData.splice(index, 1);
  137. // }
  138. // console.log('this.selectedData', this.selectedData);
  139. // this.$emit('change', this.selectedData);
  140. // },
  141. toggleSelect(data) {
  142. if (this.mode == 'single') {
  143. this.handleSingleSelect(data);
  144. } else {
  145. this.handleMultipleSelect(data);
  146. }
  147. this.$emit('change', this.selectedData);
  148. },
  149. // 单选处理逻辑
  150. handleSingleSelect(data) {
  151. const isSelected = this.selectedData.some(item => item.id == data.id);
  152. if (isSelected) {
  153. // 取消选择
  154. this.selectedData = [];
  155. } else {
  156. // 替换选择
  157. this.selectedData = [data];
  158. }
  159. },
  160. // 多选处理逻辑
  161. handleMultipleSelect(data) {
  162. const index = this.selectedData.findIndex(item => item.id == data.id);
  163. if (index === -1) {
  164. this.selectedData.push(data);
  165. } else {
  166. this.selectedData.splice(index, 1);
  167. }
  168. },
  169. toggleExpand() {
  170. this.showAll = !this.showAll;
  171. },
  172. handleAdd() {
  173. this.$refs.addPopup.open();
  174. },
  175. confirmAdd() {
  176. if (!this.newName.trim()) {
  177. uni.showToast({
  178. title: '请输入技能名称',
  179. icon: 'none'
  180. });
  181. return;
  182. }
  183. const newObject = {
  184. id: this.list.length,
  185. name: this.newName
  186. };
  187. console.log('list', this.list);
  188. console.log('newObject', newObject);
  189. this.$emit('add', newObject);
  190. this.newName = '';
  191. this.$refs.addPopup.close();
  192. },
  193. cancelAdd() {
  194. this.newName = '';
  195. this.$refs.addPopup.close();
  196. }
  197. }
  198. };
  199. </script>
  200. <style scoped>
  201. .add-block {
  202. background: transparent;
  203. border: 1px dashed #007aff;
  204. color: #007aff;
  205. }
  206. .expand-button {
  207. margin-top: 30rpx;
  208. color: #007aff;
  209. font-size: 28rpx;
  210. text-align: center;
  211. }
  212. .add-popup {
  213. padding: 40rpx;
  214. background: #fff;
  215. border-radius: 16rpx;
  216. }
  217. .add-input {
  218. width: 100%;
  219. padding: 20rpx;
  220. border: 1px solid #eee;
  221. border-radius: 8rpx;
  222. margin-bottom: 40rpx;
  223. }
  224. .button-group {
  225. display: flex;
  226. gap: 20rpx;
  227. }
  228. .confirm-button {
  229. flex: 1;
  230. background: #007aff;
  231. color: #fff;
  232. }
  233. .cancel-button {
  234. flex: 1;
  235. background: #ff3b30;
  236. color: #fff;
  237. }
  238. </style>