multiple-rate.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Author: simsir-lin
  3. * Github: https://github.com/simsir-lin
  4. * Email: 15986907592@163.com
  5. */
  6. Component({
  7. behaviors: [],
  8. properties: {
  9. rate: {
  10. type: String,
  11. value: '0'
  12. },
  13. icon: {
  14. type: String,
  15. value: 'star'
  16. },
  17. disabled: {
  18. type: String,
  19. value: false
  20. }
  21. },
  22. data: {
  23. starArr: []
  24. },
  25. attached: function () {
  26. this.getStarArr()
  27. },
  28. moved: function () {
  29. },
  30. detached: function () {
  31. },
  32. methods: {
  33. getStarArr: function () {
  34. let starArr = [];
  35. for (var i = 0; i < this.data.rate; i++) {
  36. starArr.push(this.data.icon);
  37. }
  38. for (let j = 0; j < 5 - this.data.rate; j++) {
  39. starArr.push(this.data.icon + '-o');
  40. }
  41. this.setData({
  42. starArr: starArr
  43. })
  44. },
  45. handleTap: function (e) {
  46. if (this.data.disabled) {
  47. return;
  48. }
  49. this.setData({
  50. rate: Number(e.currentTarget.dataset.index) + 1
  51. })
  52. this.triggerEvent('change', { value: Number(e.currentTarget.dataset.index) + 1 });
  53. this.getStarArr()
  54. }
  55. }
  56. })