graceSelectTags.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view class="grace-select-tags">
  3. <view class="grace-select-tag"
  4. :style="{
  5. width : itemWidth, 'background' : item.checked ? selectedColor+' !important' : bgColor, fontSize:fontSize,
  6. height:height, lineHeight:height, borderRadius:borderRadius, color : item.checked ? fontActiveColor : fontColor
  7. }"
  8. @tap.stop="graceSelectChange(index)" v-for="(item, index) in tagsData" :key="index">{{item.name}}</view>
  9. </view>
  10. </template>
  11. <script>
  12. export default{
  13. props: {
  14. itemWidth : {type: String, default:"200rpx"},
  15. type : { type: String, default: ""},
  16. selectedColor : { type: String, default: "#3688FF"},
  17. fontSize : { type: String, default: "26rpx"},
  18. items : { type: Array, default : function(){return []}},
  19. datas : { type: Array, default : function(){return []}},
  20. bgColor : { type: String, default: "#F6F7F8"},
  21. height:{type: String, default:"68rpx"},
  22. borderRadius : {type: String, default:"10rpx"},
  23. fontColor:{type: String, default:"#323232"},
  24. fontActiveColor:{type: String, default:"#FFFFFF"}
  25. },
  26. created : function(){
  27. this.tagsData = this.items == null ? [] : this.items;
  28. this.initChange();
  29. },
  30. data() {
  31. return {
  32. tagsData : []
  33. }
  34. },
  35. watch:{
  36. items:function(val){
  37. this.tagsData = val;
  38. this.initChange();
  39. }
  40. },
  41. methods:{
  42. initChange : function () {
  43. if(this.type == 'radio'){
  44. var selectIndex = -1;
  45. var selectVal = '';
  46. for (var i = 0; i < this.tagsData.length; i++){
  47. if(this.tagsData[i].checked){
  48. selectIndex = i;
  49. selectVal = this.tagsData[i].value;
  50. }
  51. }
  52. //this.$emit("change", selectVal, this.datas, selectIndex);
  53. }else{
  54. var sedRes = [], indexs = [];
  55. for (var i = 0; i < this.tagsData.length; i++){
  56. if(this.tagsData[i].checked){
  57. sedRes.push(this.tagsData[i].value);
  58. indexs.push(i);
  59. }
  60. }
  61. //this.$emit("change", sedRes, this.datas, indexs);
  62. }
  63. },
  64. graceSelectChange : function(index){
  65. // 单选
  66. if(this.type == 'radio'){
  67. for (var i = 0; i < this.tagsData.length; i++){
  68. this.tagsData[i].checked = false;
  69. this.tagsData.splice(i,1,this.tagsData[i]);
  70. }
  71. this.tagsData[index].checked = true;
  72. this.tagsData.splice(index,1,this.tagsData[index]);
  73. this.$emit("change", this.tagsData[index].value, this.datas, index);
  74. }
  75. // 多选
  76. else{
  77. if(this.tagsData[index].checked){
  78. this.tagsData[index].checked = false;
  79. }else{
  80. this.tagsData[index].checked = true;
  81. }
  82. this.tagsData.splice(index,1,this.tagsData[index]);
  83. var sedRes = [], indexs = [];
  84. for (var i = 0; i < this.tagsData.length; i++){
  85. if(this.tagsData[i].checked){
  86. sedRes.push(this.tagsData[i].value);
  87. indexs.push(i);
  88. }
  89. }
  90. this.$emit("change", sedRes, this.datas, indexs);
  91. }
  92. },
  93. // 设置新的选项
  94. setItems : function(items){
  95. this.tagsData = items;
  96. this.initChange();
  97. },
  98. // 全不选
  99. clearSelected:function(){
  100. var newData = [];
  101. for(let i = 0; i < this.tagsData.length; i++){
  102. this.tagsData[i].checked = false;
  103. newData.push(this.tagsData[i]);
  104. }
  105. this.tagsData = newData;
  106. if(this.type == 'radio'){
  107. this.$emit("change", '', this.datas, -1);
  108. }else{
  109. this.$emit("change", [], this.datas, []);
  110. }
  111. },
  112. // 全选
  113. selectAll : function () {
  114. if(this.type == 'radio'){return ;}
  115. var newData = [],reDatas = [], reIndex = [];
  116. for(let i = 0; i < this.tagsData.length; i++){
  117. this.tagsData[i].checked = true;
  118. newData.push(this.tagsData[i]);
  119. reDatas.push(this.tagsData[i].value);
  120. reIndex.push(i);
  121. }
  122. this.tagsData = newData;
  123. this.$emit("change", reDatas, this.datas, reIndex);
  124. }
  125. }
  126. }
  127. </script>
  128. <style scoped>
  129. .grace-select-tags{display:flex; flex-direction:row; flex-wrap:wrap;}
  130. .grace-select-tag{height:68rpx; line-height:68rpx; overflow:hidden; box-sizing:border-box; padding-left:15rpx; padding-right:15rpx; text-align:center; margin:10rpx; font-size:24rpx; border-radius:8rpx;}
  131. </style>