123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script src="./js/vue.js"></script>
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
- <style>
- .container {
- padding: 20px;
- }
- .navbar {
- margin-bottom: 20px;
- }
- .add-goods {
- width: 400px;
- }
- </style>
- </head>
- <body>
- <div id="app">
- <div class="container">
- <!-- 顶部导航 -->
- <nav class="navbar bg-primary" data-bs-theme="dark">
- <div class="container-fluid">
- <span class="navbar-brand mb-0 h1">商品列表管理</span>
- </div>
- </nav>
- <!-- 搜索框 -->
- <div class="row">
- <div class="col-2">
- <input v-model="searchInp" type="text" class="form-control" placeholder="请输入搜索名称">
- </div>
- <div class="col-10">
- <button @click="searchFun" type="button" class="btn btn-primary">搜索</button>
- </div>
- </div>
- <!-- 添加商品 -->
- <div class="add-goods input-group mt-3">
- <input v-model="goodsName" type="text" class="form-control" placeholder="商品名称" aria-label="Recipient's username"
- aria-describedby="button-addon2">
- <input v-model.number="goodsPrice" type="text" class="form-control" placeholder="商品价格" aria-label="Recipient's username"
- aria-describedby="button-addon2">
- <button @click="addGoods" class="btn btn-outline-secondary" type="button" id="button-addon2">添加商品</button>
- </div>
- <!-- 表格区域 -->
- <div class="table-content">
- <table class="table">
- <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 class="table-group-divider">
- <tr @click="checkLine(item.id)" :class="{'table-active':item.isCheck}"
- v-for="(item,index) in dataList" :key="item.id">
- <th scope="row">{{index+1}}</th>
- <td>
- <input type="checkbox" v-bind:checked="item.isCheck">
- </td>
- <td>{{item.name}}</td>
- <td>{{item.price}}</td>
- <td>
- <button @click.stop="delFun(item.id)" type="button" class="btn btn-primary btn-sm">删除</button>
- </td>
- </tr>
- <tr>
- <th colspan="3">总价</th>
- <td>{{sum}}</td>
- <td>
- <button @click="delCheck" type="button" class="btn btn-primary btn-sm">删除选中</button>
- </td>
- </tr>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- <script>
- let app = new Vue({
- el: "#app",
- data: {
- goodsName:"",
- goodsPrice:"",
- searchInp: "",
- dataList: [
- {
- id: 1001,
- name: "衣服",
- price: 100,
- isCheck: false
- },
- {
- id: 1002,
- name: "裤子",
- price: 200,
- isCheck: false
- },
- {
- id: 1003,
- name: "帽子",
- price: 50,
- isCheck: false
- },
- {
- id: 1004,
- name: "鞋",
- price: 300,
- isCheck: false
- }
- ]
- },
- methods: {
- // 添加商品
- addGoods(){
- if(this.goodsName && this.goodsPrice){
- let newId = 0;
- // this.dataList.forEach((val)=>{
- // if(val.id > newId){
- // newId = val.id;
- // }
- // })
- // Math.max(1,2,3,4)
- // console.log(newId);
- let idList = this.dataList.map((val)=>{
- return val.id;
- })
- // console.log(Math.max(...idList));
- newId = Math.max(...this.dataList.map(val => val.id))+1;
- console.log(newId);
- let newGoods = {
- id:newId,
- name:this.goodsName,
- price:this.goodsPrice,
- isCheck:false
- }
- this.dataList.push(newGoods);
- }else{
- alert("请输入商品名称和价格");
- }
- },
- // 单行选中
- checkLine(id) {
- this.dataList.map((val) => {
- if (val.id == id) {
- val.isCheck = !val.isCheck;
- }
- })
- },
- // 单行删除
- delFun(id){
- let newDataList = this.dataList.filter((val)=>{
- if(val.id != id){
- return true
- }
- })
- this.dataList = newDataList;
- },
- // 删除选中
- delCheck(){
- let newDataList = this.dataList.filter((val)=>{
- if(!val.isCheck){
- return true
- }
- })
- this.dataList = newDataList;
- },
- // 搜索
- searchFun(){
- let searchVal = this.searchInp;
- let newDataList = this.dataList.filter((val)=>{
- if(val.name.includes(searchVal)){
- return true
- }
- });
- this.dataList = newDataList;
- }
- },
- computed: {
- sum() {
- let sumVal = 0;
- this.dataList.forEach((val)=>{
- if(val.isCheck){
- sumVal += val.price;
- }
- });
- return sumVal
- }
- }
- })
- </script>
- </body>
- </html>
|