Vue+Bootstrap实现todolist的小Demo

一、Html样式
<div class="container" id="box">
    <form role="form">
      <div class="form-group">
        <label for="username">username:</label>
        <!-- 用v-model指令将input内的值与实例data内的值绑定在一起 -->
        <input type="text" class="form-control" id="username" v-model="username" placeholder="Please enter your username">
      </div>
      <div class="form-group">
        <label for="password">password:</label>
        <input type="password" class="form-control" id="password" v-model="password" placeholder="please enter your password">
      </div>
      <!-- 用v-on指令绑定点击click事件 -->
      <button type="button" class="btn btn-primary" v-on:click="add">submit</button>
      <button type="reset" class="btn btn-danger">reset</button>
    </form>
    <hr>
    <table class="table table-bordered table-hover">
      <caption class="h3 text-center text-muted">用户信息表</caption>
      <tr class="text-danger">
        <th class="text-center">id</th>
        <th class="text-center">username</th>
        <th class="text-center">password</th>
        <th class="text-center">option</th>
      </tr>
      <tr class="text-center" v-for="(item,index) in myData">
        <!-- 用v-for指令循环渲染列表 index为索引值 -->
        <td>{{index+1}}</td>
        <td>{{item.name}}</td>
        <td>{{item.pwd}}</td>
        <td>
          <button class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal" v-on:click="nowIndex = index">delete</button>
        </td>
      </tr>
      <!--用v-show指令 根据条件返回的布尔值决定是否渲染  -->
      <tr v-show="myData.length != 0">
        <td colspan="4" class="text-right">
          <button class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal" v-on:click="{nowIndex = all,modalTitle = 'confirm delete all?'}"> delete all</button>
        </td>
      </tr>
      <tr v-show="myData.length == 0">
        <td colspan="4" class="text-center text-muted">
          <p>暂无数据...</p>
        </td>
      </tr>
    </table>

    <!-- 模态框 -->
    <div role="dialog" class="modal fade bs-example-modal-sm" id="myModal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
            <h4 class="modal-title">{{modalTitle}}</h4>
          </div>
          <div class="modal-body text-right">
            <button class="btn btn-primary btn-sm" data-dismiss="modal" v-on:click="remove(nowIndex)">yes</button>
            <button class="btn btn-danger btn-sm" data-dismiss="modal">no</button>
          </div>
        </div>
      </div>
    </div>
  </div>
二、JS部分
<link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css">
<script src="./libs/jquery-3.2.1.min.js"></script>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="./libs/vue.js"></script>
<script>
    window.onload = function(){
      new Vue({
        el:"#box",
        data:{
          myData:[],
          username:'',
          password:'',
          nowIndex:'',//当前用户的索引
          modalTitle:'confirm delete?'//模态框的title
        },
        methods:{
          add:function(){
            var name = this.username.trim();
            var pwd = this.password.trim();
            this.myData.push({
              name:name,
              pwd:pwd
            });
            this.username = '';
            this.password = '';
          },
          remove:function(index){
            if(index == 'all'){
              //删除所有
              this.myData = [];
            }else{
              //删除索引为index的那条信息
              this.myData.splice(index,1);
            }
          }
        }
      })
    };
  </script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容