graceNvueSelectTags.vue 3.9 KB

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