DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<input type="checkbox" id="cb1">
<label for="cb1">男label>
<hr>
<input type="checkbox" id="cb2">
<label for="cb2">女label>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>品牌列表案例title>
<link rel="stylesheet" href="./lib/bootstrap.css">
<link rel="stylesheet" href="./css/brandlist.css">
head>
<body>
<div id="app">
<div class="card">
<div class="card-header">添加品牌div>
<div class="card-body">
<form @submit.prevent="add">
<div class="form-row align-items-center">
<div class="col-auto">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text">品牌名称div>
div>
<input type="text" class="form-control" placeholder="请输入品牌名称" v-model.trim="brand">
div>
div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-2">添加button>
div>
div>
form>
div>
div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th scope="col">#th>
<th scope="col">品牌名称th>
<th scope="col">状态th>
<th scope="col">创建时间th>
<th scope="col">操作th>
tr>
thead>
<tbody>
<tr v-for="item in list" :key="item.id">
<td>{{item.id}}td>
<td>{{item.name}}td>
<td>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" :id="'cb' + item.id" v-model="item.status">
<label class="custom-control-label" :for="'cb' + item.id" v-if="item.status">已启用label>
<label class="custom-control-label" :for="'cb' + item.id" v-else>已禁用label>
div>
td>
<td>{{item.time}}td>
<td>
<a href="javascript:;" @click="remove(item.id)">删除a>
td>
tr>
tbody>
table>
div>
<script src="./lib/vue-2.6.12.js">script>
<script>
const vm = new Vue({
el: '#app',
data: {
// 品牌的列表数据
list: [
{id:1, name:'宝马', status:true, time:new Date()},
{id:2, name:'奔驰', status:false, time:new Date()},
{id:3, name:'奥迪', status:true, time:new Date()},
],
// 用户输入的品牌名称
brand: '',
//下一个可用的id
nextId: 4,
},
methods: {
// 点击链接,删除对应的品牌信息
remove(id){
console.log(id)
this.list = this.list.filter(item => item.id!==id)
},
// 阻止表单的默认提交行为之后,触发 add 方法
add(){
console.log(this.brand)
if(this.brand === ""){
alert('必须填写品牌名称')
return
}else{
//1. 先把要添加的品牌对象,整理出来
const obj = {
id: this.nextId,
name: this.brand,
status: true,
time: new Date()
}
//2. 往 this.list 数组中 push 步骤 1 中得到的对象
this.list.push(obj)
//3. 清空 this.brand
this.brand = ''
//4. 让 this.nextId 自增 +1
this.nextId++
}
}
},
})
script>
body>
html>