Implementing simple student information management based on VUE

Realize the following requirements:
Requirement 1: Lay out the page, prepare the initial data (your own handwritten data structure)
Requirement 2: When the input box has no value, give the user a prompt, and there must be values ​​before adding new data
Requirement 3: Add functions - think Good data structure and unified object key
requirement 4: Click the edit function and assign the value to the input box (do not operate the dom, data-driven page)
requirement 5: After the user makes modifications, click the same button - to achieve the page update effect after editing
requirement 6: Click Delete to delete this row of data

<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model="name" />
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model.number="age" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="sex">
        <option value="男"></option>
        <option value="女"></option>
      </select>
    </div>
    <div>
      <button @click="addFn">添加/修改</button>
    </div>
    <div>
      <table border="1" cellpadding="10" cellspacing="0">
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(obj, index) in arr" :key="obj.id">
          <td>{
    
    {
    
     index + 1 }}</td>
          <td>{
    
    {
    
     obj.name }}</td>
          <td>{
    
    {
    
     obj.age }}</td>
          <td>{
    
    {
    
     obj.gender }}</td>
          <td>
            <button @click="delFn(index)">删除</button>
            <button @click="editFn(index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
export default {
    
    
  data() {
    
    
    return {
    
    
      name: "",
      age: 0,
      sex: "",
      arr: [
        {
    
     id: 1, name: "Tom", age: 19, gender: "男" },
        {
    
     id: 2, name: "Jone", age: 21, gender: "女" },
      ],
      selectIndex: "",
      flag: false,
    };
  },
  methods: {
    
    
    addFn() {
    
    
      if (this.name.length === 0 || this.age.length === 0) {
    
    
        alert("内容不能为空");
        return;
      }
      // let id = this.arr.length > 0 ? this.arr[this.arr.length - 1].id + 1 : 1;

      if (!this.flag) {
    
    
        this.arr.push({
    
    
          // id: id,
          name: this.name,
          age: this.age,
          gender: this.sex,
        });
      } else {
    
    
        this.arr.splice(this.selectIndex, 1, {
    
    
          name: this.name,
          age: this.age,
          gender: this.sex,
        });
        this.flag = false;
      }

      this.name = "";
      this.age = 0;
      this.sex = "";
    },
    editFn(i) {
    
    
      this.name = this.arr[i].name;
      this.age = this.arr[i].age;
      this.sex = this.arr[i].gender;
      this.selectIndex = this.arr.findIndex((obj) => obj.id === i + 1);
      this.flag = true;
    },
    delFn(ind) {
    
    
      this.arr.splice(ind, 1);
    },
  },
};
</script>

The above code can achieve the above requirements, and the rendering is as follows.
Insert image description here
Note: The above vue code is based on a global module package @vue/cli (get the vue command) officially provided by Vue, thereby creating a scaffolding project and using the webpack packaging tool. Be careful to download the third-party package completely when using it.

Guess you like

Origin blog.csdn.net/qq_45093219/article/details/118389057