slambb-picker-item.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view class="padding-sm">
  3. <view class="flex justify-between" style="position: relative;height: 100%; width: 100%;">
  4. <picker-view class="mpvue-picker-view " :value="leftValue" :style="[{'--multiple': rightVisible ? 1 : 2}]"
  5. :indicator-style="indicatorStyle" @change="bindChange('left',$event)">
  6. <picker-view-column>
  7. <view class="item" v-for="(item,index) in currentItemobj.pickerLeftList" :key="index">{{item}}</view>
  8. </picker-view-column>
  9. </picker-view>
  10. <picker-view class="mpvue-picker-view " :value="rightValue" :style="[{'--multiple': rightVisible ? 1 : 2}]" v-if="rightVisible"
  11. :indicator-style="indicatorStyle" @change="bindChange('right',$event)">
  12. <picker-view-column>
  13. <view class="item" v-for="(item,index) in currentItemobj.pickerRightList" :key="index">.{{item}}
  14. </view>
  15. </picker-view-column>
  16. </picker-view>
  17. <view v-if="currentItemobj.pickerUnit" class="picker-unit text-sm text-purple" style="z-index: 10;">{{currentItemobj.pickerUnit}}</view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. props: {
  24. leftList: Array,
  25. rightList: Array,
  26. showUnit: {
  27. type: String,
  28. default: ""
  29. },
  30. itemObj: Object,
  31. bDouble: true
  32. },
  33. data() {
  34. let LIndex = 0;
  35. let RIndex = 0;
  36. if (this.bDouble) {
  37. // console.log(this.itemObj.defaultValue);
  38. if(this.itemObj.defaultValue){
  39. var valueArr = this.itemObj.defaultValue.toString().split(".");
  40. for (let i = 0; i < this.itemObj.pickerLeftList.length; i++) {
  41. if (valueArr[0] == this.itemObj.pickerLeftList[i]) {
  42. LIndex = i;
  43. break;
  44. }
  45. }
  46. for (let i = 0; i < this.itemObj.pickerRightList.length; i++) {
  47. if (valueArr[1] == this.itemObj.pickerRightList[i]) {
  48. RIndex = i;
  49. break;
  50. }
  51. }
  52. }
  53. } else {
  54. for (let i = 0; i < this.itemObj.pickerLeftList.length; i++) {
  55. if (this.itemObj.defaultValue == this.itemObj.pickerLeftList[i]) {
  56. LIndex = i;
  57. break;
  58. }
  59. }
  60. }
  61. this.$emit("bindChangeItem", this.itemObj.defaultValue,LIndex);
  62. return {
  63. leftValue: [LIndex],
  64. rightValue: [RIndex],
  65. oldLeftValue: this.itemObj.pickerLeftList[LIndex],
  66. oldRightValue: this.itemObj.pickerRightList[RIndex],
  67. visible: true,
  68. rightVisible: this.bDouble,
  69. // indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`
  70. indicatorStyle: `height:40px;`,
  71. currentItemobj: this.itemObj
  72. }
  73. },
  74. watch: {
  75. bDouble(val) {
  76. // console.log('watch bDouble:', val);
  77. this.rightVisible = val;
  78. },
  79. itemObj(val) {
  80. // 重新赋值
  81. // console.log('watch itemObj:', val);
  82. this.currentItemobj = val;
  83. }
  84. },
  85. methods: {
  86. bindChange: function(direction, e) {
  87. const val = e.detail.value
  88. if (direction == "left") {
  89. this.oldLeftValue = this.itemObj.pickerLeftList[val[0]];
  90. } else {
  91. // console.log(this.itemObj.pickerRightList[val[0]]);
  92. this.oldRightValue = this.itemObj.pickerRightList[val[0]];
  93. }
  94. // console.log("==",this.oldLeftValue,"==",this.oldRightValue);
  95. if (this.rightVisible) {
  96. this.$emit("bindChangeItem", this.oldLeftValue + (this.oldRightValue / 10),val[0]);
  97. } else {
  98. this.$emit("bindChangeItem", this.oldLeftValue,val[0]);
  99. }
  100. },
  101. }
  102. }
  103. </script>
  104. <style>
  105. /* picker */
  106. .mpvue-picker-view {
  107. width: calc(50% * var(--multiple) - 5px);
  108. height: calc(300px / var(--multiple));
  109. border-radius: 20px;
  110. }
  111. .item {
  112. text-align: center;
  113. width: calc(100% - 0px);
  114. background-color: rgba(255, 255, 255, 1);
  115. height: 88upx;
  116. line-height: 88upx;
  117. text-overflow: ellipsis;
  118. white-space: nowrap;
  119. font-size: 40upx;
  120. /* border-radius: 5px; */
  121. /* margin-top: 5px; */
  122. }
  123. .picker-unit {
  124. position: absolute;
  125. top: 0;
  126. right: 30rpx;
  127. /* left: 0; */
  128. bottom: 0;
  129. margin: auto;
  130. display: flex;
  131. align-items: center;
  132. }
  133. </style>