gui-select-list.vue 3.4 KB

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