欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue实现form表单与table表格的数据关联功能示例

程序员文章站 2022-11-14 19:14:57
本文实例讲述了vue实现form表单与table表格的数据关联功能。分享给大家供大家参考,具体如下:

本文实例讲述了vue实现form表单与table表格的数据关联功能。分享给大家供大家参考,具体如下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport"
     content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>www.jb51.net vue form表单数据关联</title>
  <style>
    *{
      margin: 0;
      padding: 0;
      list-style: none;
    }
    input{
      margin-left: 50px ;
    }
    span{
      margin-left: 50px ;
    }
    select{
      margin-left: 50px ;
    }
    .create{
      margin-left: 150px ;
    }
  </style>
</head>
<body>
<form id="app">
  <fieldset>
    <legend>creat new person</legend>
    <span>name:</span><input type="text" v-model="text0">
    <br>
    <span>age:</span><input type="text" value="0" v-model="text1">
    <br>
    <span>sex:</span><select v-model="text2">
    <option>man</option>
    <option>woman</option>
    <option>....</option>
  </select>
    <br>
    <button class="create" @click="add">create</button>
  </fieldset>
  <table>
    <tr><td>name</td><td>age</td><td>sex</td><td>delete</td></tr>
    <tr v-for="x in person"><td>{{x.name}}</td><td>{{x.age}}</td><td>{{x.sex}}</td><td><button @click="fun">delete</button></td></tr>
  </table>
</form>
</body>
<script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
<script>
  const app=new vue({
    el:"#app",
    data:{
      text0:"",
      text1:"",
      text2:"",
      person:[{
        name:"jack",
        age:"20",
        sex:"man",
      },
        {
          name:"bill",
          age:"24",
          sex:"woman",
        },
      ]
    },
    methods: {
      add(){
          if (this.text0==""||this.text1==""){
            alert("name or age undefined")
          }else{
            this.person.push({
              name: this.text0,
              age: this.text1,
              sex: this.text2,
            });
          }
      },
      fun(){
        this.person.pop()
      }
    }
  })
</script>
</html>

运行效果如下图所示:

vue实现form表单与table表格的数据关联功能示例

希望本文所述对大家vue.js程序设计有所帮助。