listOption.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <view class="list-wrap">
  3. <view
  4. class="item"
  5. v-for="(item, index) in list"
  6. :key="index"
  7. :class="isActive(item)"
  8. @click="change(item)"
  9. >
  10. {{ item[label] }}
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. model: {
  17. prop: 'active',
  18. event: 'change'
  19. },
  20. props: {
  21. list: {
  22. type: Array,
  23. default () {
  24. return []
  25. }
  26. },
  27. active: {
  28. type: [Number, String]
  29. },
  30. label: {
  31. type: String,
  32. default: 'label'
  33. },
  34. value: {
  35. type: String,
  36. default () {
  37. return 'value'
  38. }
  39. }
  40. },
  41. methods: {
  42. isActive (item) {
  43. if (this.active === item[this.value]) {
  44. return 'active'
  45. } else {
  46. return ''
  47. }
  48. },
  49. change (item) {
  50. this.$emit('change', item[this.value])
  51. }
  52. }
  53. }
  54. </script>
  55. <style lang="scss" scoped>
  56. $cols: 3; // 列数
  57. $marginWidth: 20rpx; // 间隔
  58. .list-wrap {
  59. padding: 0 30rpx;
  60. display: flex;
  61. flex-wrap: wrap;
  62. .item {
  63. display: flex;
  64. justify-content: center;
  65. align-items: center;
  66. font-size: 28rpx;
  67. color: #333333;
  68. border: 1px solid #f2f2f2;
  69. width: calc((100% - #{($cols - 1) * $marginWidth}) / #{$cols});
  70. margin-left: $marginWidth;
  71. margin-bottom: $marginWidth;
  72. box-sizing: border-box;
  73. height: 60rpx;
  74. }
  75. .item:nth-of-type(#{$cols}n + 1) {
  76. margin-left: 0;
  77. }
  78. & .active {
  79. border: 1px solid #4b7902;
  80. }
  81. }
  82. </style>