gui-select-list.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <view class="gui-select-list">
  3. <view v-for="(item, index) in dataIn"
  4. :key="index" class="gui-select-list-item gui-flex gui-rows gui-nowrap gui-align-items-center"
  5. :data-index="index" @tap.stop="choose">
  6. <text class="gui-select-list-icon gui-icons gui-block-text gui-select-list-ring gui-select-list-icon-l"
  7. v-if="checkedType == 'ring' && !item.checked">&#xe762;</text>
  8. <text class="gui-select-list-icon gui-icons gui-block-text gui-select-list-ring gui-select-list-icon-l gui-fade-in gui-select-list-current"
  9. v-if="checkedType == 'ring' && item.checked">&#xe685;</text>
  10. <image :src="item.img" class="gui-select-list-img" v-if="item.img" mode="widthFix"></image>
  11. <view class="gui-select-list-body gui-flex gui-rows gui-nowrap gui-align-items-center"
  12. :class="[isBorder?'gui-border-b' : '']" :style="{borderColor:borderColor}">
  13. <view class="gui-select-list-content">
  14. <text class="gui-block-text gui-select-list-title">{{item.title}}</text>
  15. <text class="gui-select-list-desc gui-block-text" v-if="item.desc">{{item.desc}}</text>
  16. </view>
  17. <text class="gui-icons gui-block-text gui-select-list-icon gui-select-list-current"
  18. :class="[item.checked ? 'gui-fade-in gui-select-list-current' : '']"
  19. v-if="item.checked && checkedType == 'right'">&#xe60f;</text>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default{
  26. name : "gui-select-list",
  27. props : {
  28. items : {type : Array, default : function(){return [];}},
  29. type : {type : String, default : "radio"},
  30. checkedType : {type : String, default : 'right'},
  31. isBorder : {type : Boolean, default : true},
  32. borderColor : {type : String, default : "#F6F6F6"},
  33. maxSize : {type : Number, default : 0}
  34. },
  35. data() {
  36. return {
  37. dataIn : []
  38. }
  39. },
  40. created : function(){
  41. this.dataIn = this.items;
  42. },
  43. watch : {
  44. items : function(val){ this.dataIn = val;}
  45. },
  46. methods : {
  47. // 获取选中数据的索引
  48. getSelectedIndex : function(){
  49. var tmpArr = [];
  50. this.dataIn.forEach((item, idx)=>{
  51. if(item.checked){
  52. tmpArr.push(idx);
  53. }
  54. });
  55. return tmpArr;
  56. },
  57. // 选择数据
  58. choose : function(e){
  59. var index = e.currentTarget.dataset.index;
  60. if(this.type == 'radio'){
  61. if(this.dataIn[index].checked){
  62. this.dataIn[index].checked = false;
  63. this.$emit('change', -1);
  64. }else{
  65. for(let i = 0; i < this.dataIn.length; i++){
  66. this.dataIn[i].checked = false;
  67. }
  68. this.dataIn[index].checked = true;
  69. this.$emit('change', index);
  70. }
  71. }else{
  72. if(this.dataIn[index].checked){
  73. this.dataIn[index].checked = false;
  74. }else{
  75. if(this.maxSize > 0){
  76. var size = 0;
  77. this.dataIn.forEach((item)=>{
  78. if(item.checked){size++;}
  79. });
  80. size++;
  81. if(size > this.maxSize){this.$emit('maxSed'); return ;}
  82. }
  83. this.dataIn[index].checked = true;
  84. }
  85. var sedArr = [];
  86. for(let i = 0; i < this.dataIn.length; i++){
  87. if(this.dataIn[i].checked){
  88. sedArr.push(i);
  89. }
  90. }
  91. this.$emit('change', sedArr);
  92. }
  93. }
  94. }
  95. }
  96. </script>
  97. <style scoped>
  98. .gui-select-list-item{font-size:0;}
  99. .gui-select-list-icon{margin-left:10rpx;}
  100. .gui-select-list-icon-l{margin-left:0; margin-right:10rpx;}
  101. .gui-select-list-body{width:100rpx; flex:1;}
  102. .gui-select-list-content{width:200rpx; flex:1; overflow:hidden; padding:25rpx 0;}
  103. </style>