vue-評分/評價五角星組件開發

功能:

  1. 展示默認的分數,
  2. 點擊幾顆星星,展示幾分
<template>  
  <div class="form-content">    
    <div>
      <span class='name'>質量分<span class="required"></span></span>
      <div class="stars">
        <img v-for="(item,ydx) in qStars" :key="ydx" :src="item?yellow:gray" @click="onSetStars('q',ydx)">
        <span>{{formData.qscore}}分</span>
      </div>
    </div>
    <div>
      <span class='name'>效率分<span class="required"></span></span>
      <div class="stars">
        <img v-for="(item2,ydx2) in eStars" :key="ydx2" :src="item2?yellow:gray" @click="onSetStars('e',ydx2)">
        <span>{{formData.escore}}分</span>
      </div>
    </div>
  </div> 
</template>

<script>
export default {
  data() {
    return {
      qStars:[false,false,false,false,false],
      eStars:[false,false,false,false,false],
      yellow:require("../../../assets/images/emer/star-yellow.png"),
      gray:require("../../../assets/images/emer/star-gray.png"),
	  formData:{
		taskid:"",
        qscore: 0,
        escore:0
	  }
    }
  },
  created() {
    this.formData.qscore = this.$route.params.row.qscore;
    this.formData.escore = this.$route.params.row.escore;

    // 填充星星顯示與否的值
    if(this.formData.qscore){
      this.qStars.fill(true,0,this.formData.qscore);
    }else{
      this.qStars.fill(false);
    }
    if(this.formData.escore){
      this.eStars.fill(true,0,this.formData.escore);
    }else{
      this.eStars.fill(false);
    }

  },
  methods: {

     /**----------------------------按鈕操作--------------------------------------- */
    // 1 設置分數
    onSetStars(name,index){
      if(name.indexOf('q')>-1){
        this.formData.qscore=index+1;
        if(index==4){//最後一個
          this.qStars.fill(true);
        }else{
          this.qStars.fill(true,0,index+2);
          this.qStars.fill(false,index+1);
        }
        this.$forceUpdate();

      }else{
        this.formData.escore=index+1;
        if(index==4){//最後一個
          this.eStars.fill(true);
        }else{//其他
          this.eStars.fill(true,0,index+2);
          this.eStars.fill(false,index+1);
        }
        this.$forceUpdate();
      }
    },
    // 2.1 提交前驗證信息
    doSubmit(){
	    if(this.formData.qscore ===0){
			errStr("請設置質量分。");
			return;
        }
		if(this.formData.escore ===0){
			errStr("請設置效率分。");
			return;
		}
        this.doSubmitOK();
    },
    // 2.2 提交
	doSubmitOK(){
      this.blnShowLoad = true;
		axios({
			method:'post',
			url:'task/scoring',
			data:this.formData,
		}).then((res)=>{
			this.blnShowLoad = false;
			if(res.data.data.result){
                showAlert("評分成功!","提示",()=>{
				    this.$router.go(-1);
			    });
		    }else{
              errStr("評分失敗!");
            }
		}).catch(err =>{
			this.blnShowLoad = false;
			showErrMsg(err,"評分失敗!");
		})
	}
  }
}
</script>

<style lang="stylus" scoped>
  @import "~common/stylus/duty"
  @import "~common/stylus/form"
  .stars{
    >img{
      width:0.8rem;
      height:0.8rem;
      margin-right:0.1rem;
    }
  }
</style>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章