All files / src/pages Singer.vue

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                                                                                                                                                                                                                                                                           
<template>
  <div class="singer">
    <div class="singer-header">
      <ul>
        <li
          v-for="(item, index) in singerStyle"
          :key="index"
          :class="{active: item.name === activeName}"
          @click="handleChangeView(item)">
          {{item.name}}
        </li>
      </ul>
    </div>
    <content-list :contentList="data"></content-list>
    <div class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        background
        layout="total, prev, pager, next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="albumDatas.length">
      </el-pagination>
    </div>
  </div>
</template>
 
<script>
import ContentList from '../components/ContentList'
import { singerStyle } from '../assets/data/singer'
 
export default {
  name: 'singer',
  components: {
    ContentList
  },
  data () {
    return {
      singerStyle: [], // 歌手导航栏类别
      activeName: '全部歌手',
      pageSize: 15, // 页数
      currentPage: 1, // 当前页
      albumDatas: []
    }
  },
  computed: {
    // 计算当前表格中的数据
    data () {
      return this.albumDatas.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
    }
  },
  created () {
    this.singerStyle = singerStyle
    this.getAllSinger()
  },
  methods: {
    // 获取当前页
    handleCurrentChange (val) {
      this.currentPage = val
    },
    handleChangeView (item) {
      this.activeName = item.name
      this.albumDatas = []
      if (item.name === '全部歌手') {
        this.getAllSinger()
      } else {
        this.getSingerSex(item.type)
      }
    },
    // 获取所有歌手
    getAllSinger () {
      this.$api.singerAPI.getAllSinger()
        .then(res => {
          this.currentPage = 1
          this.albumDatas = res.data
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 通过性别对歌手分类
    getSingerSex (sex) {
      this.$api.singerAPI.getSingerOfSex(sex)
        .then(res => {
          this.currentPage = 1
          this.albumDatas = res.data
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
 
<style scoped>
div, ul, li{
  box-sizing: border-box;
}
 
.singer {
  margin: 30px 10%;
  padding-bottom: 50px;
  background-color: #ffffff;
}
 
.singer-header {
  width: 100%;
  padding: 0 40px;
}
 
li {
  display: inline-block;
  line-height: 40px;
  margin: 40px 20px 15px 20px;
  font-size: 20px;
  font-weight: 400;
  color: #67757f;
  border-bottom: none;
  cursor: pointer;
}
 
.active {
  color: black;
  font-weight: 600;
  border-bottom: 4px solid black;
}
 
.pagination {
  display: flex;
  justify-content: center;
}
</style>