vue 純前端 增刪改查(排序)(返回頂部)

瞎做 記錄 還有很多不好的地方


微信截圖_20211201110751.png

初始數(shù)據(jù)data.js
數(shù)據(jù)格式 [ { arr: [ {} ] } ] 數(shù)組對(duì)象數(shù)組對(duì)象格式

const datalist = [
    {
        id: 1,
        arr: [
            {
                text: "七里香",
            },
            {
                text: "一路向北",
            },
            {
                text: "晴天",
            },
            {
                text: "花海",
            },
            {
                text: "等你下課",
            },
        ],
    },
    {
        id: 1,
        arr: [
            {
                text: "曹操",
            },
            {
                text: "關(guān)鍵詞",
            },
            {
                text: "江南",
            },
            {
                text: "修煉愛(ài)情",
            },
        ],
    },
    {
        id: 1,
        arr: [
            {
                a: 1,
                text: "光年之外",
            },
            {
                text: "泡沫",
            },
            {
                text: "喜歡你",
            },
            {
                text: "桃花諾"
            },
        ],
    },
];

export {
    datalist
}

html

<template>
  <div class="content">
    <!-- <inputs @click="gogo()"></inputs> -->
    <div class="add">
      <button @click="add()">添加音樂(lè)數(shù)據(jù)</button>
    </div>
    <div v-for="(item, index) in datalist" :key="index" class="big">
      <div class="addbtn" @click="additem(index + 1, item.arr)">+</div>
      <div class="downbtn" @click="downitem(index)">-</div>
      <h2>第{{ index + 1 }}組音樂(lè)數(shù)據(jù)</h2>
      <div v-for="(item1, index1) in item.arr" :key="index1" class="big_con">
        <input
          class="sort"
          v-model="item1.sorts"
          @blur="blurs(item, index, item1, index1)"
        />
        <span v-show="item1.flag"> {{ item1.text }}</span>
        <input class="texts" v-show="!item1.flag" v-model="item1.text" />
        <div>
          <button class="del" @click="del(index, index1)">刪除</button>
          <button v-show="item1.flag" class="edit" @click="edit(index, index1)">
            編輯
          </button>
          <button
            class="save"
            v-show="!item1.flag"
            @click="save(index, index1)"
          >
            完成
          </button>
        </div>
      </div>
    </div>
    <div class="back" v-show="backshow" @click="backtop()">返回</div>
  </div>
</template>

script


<script>
import { datalist } from "./data.js";
export default {
  data() {
    return {
      datalist,
      show: false,
      indnum: "",
      backshow: false,
    };
  },
  mounted() {
    window.addEventListener("scroll", this.scrollToTop);
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollToTop);
  },
  created() {
    // 遍歷數(shù)組push單個(gè)元素
    this.datalist.map((it) => {
      it.arr.map((item, index) => {
        it.arr[index].sorts = index + 1;
        it.arr[index].flag = true;
        return;
      });
    });
  },
  methods: {
    scrollToTop() {
      var scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop > 50 && (this.backshow = true);
      scrollTop <= 50 && (this.backshow = false);
    },
    // 返回頂部
    backtop() {
      document.body.scrollTop = document.documentElement.scrollTop = 0;
    },
    // 焦點(diǎn)失去進(jìn)行排序
    blurs(item, index, item1, index1) {
      this.datalist.map((mapit, mapin) => {
        if (index == mapin) {
          mapit.arr.sort((q, w) => {
            return q.sorts - w.sorts;
          });
        }
      });
    },
    // 刪除某一條
    del(ind, ind1) {
      this.datalist.map((item, index) => {
        if (ind == index) {
          item.arr.map((item1, index1) => {
            if (ind1 == index1) {
              item.arr.splice(ind1, 1);
            }
          });
          return;
        }
      });
    },
    caozuo(ind, ind1, flag) {
      this.datalist.forEach((item, index) => {
        if (ind == index) {
          item.arr.forEach((item1, index1) => {
            if (ind1 == index1) {
              if (!flag) {
                item1.flag = false;
              } else {
                item1.flag = true;
              }
            }
          });
        }
      });
      this.$forceUpdate();
    },
    edit(ind, ind1) {
      this.caozuo(ind, ind1, false);
    },
    save(ind, ind1) {
      this.caozuo(ind, ind1, true);
    },
    add() {
      this.datalist.push({
        arr: [],
      });
    },
    additem(index, arr) {
      arr.push({ sorts: arr.length + 1 });
    },
    downitem(index) {
      this.datalist.splice(index, 1);
    },
    gogo() {
      this.$router.push({ path: "/m/link" });
    },
  },
};
</script>

css

<style lang="less">
.back {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  text-align: center;
  line-height: 50px;
}
.aa {
  margin-top: 20px;
}
.text {
  margin: 0 auto;
  text-align: center;
  margin-top: 20px;
  line-height: 30px;
}
.content {
  // 解決ios滑動(dòng)不流暢
  -webkit-overflow-scrolling: touch;
  transition-duration: 300ms;
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
  padding-bottom: 50px;
  .add {
    width: 90%;
    margin: 0 auto;
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    > span {
      line-height: 35px;
      margin-left: 20px;
    }
    > button {
      width: 100px;
      height: 35px;
      color: #fff;
      background: royalblue;
      outline: none;
      border: 0;
      border-radius: 10px;
    }
  }
  .big {
    width: 85%;
    margin: 0 auto;
    border: 3px solid #ccc;
    border-radius: 8px;
    margin-top: 10px;
    padding: 20px 10px;

    > div {
      border-radius: 5px;
    }
    .addbtn {
      float: right;
      width: 30px;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
      background: #ccc;
      margin-left: 8px;
    }
    .downbtn {
      float: right;
      width: 30px;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
      color: #fff;
      background: rgb(241, 46, 46);
    }
    .big_con {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 50px;
      > input {
        border-radius: 8px;
        border: 0;
        outline: none;
        background: rgb(209, 208, 208);
        color: #333;
        height: 20px;
        line-height: 20px;
      }
      > .sort {
        width: 60px;
        text-align: center;
      }
      > .texts {
        width: 100px;
        text-align: center;
      }
      button {
        color: #fff;
        outline: none;
        padding: 3px 5px;
        border-radius: 5px;
        border: 0;
      }
      .del {
        background: rgb(241, 46, 46);
      }
      .edit {
        background: royalblue;
      }
      .save {
        background: rgb(2, 189, 2);
      }
    }
  }
}
.sel {
  outline: none;
  border: 0;
  width: 200px;
  height: 50px;
}
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容