首页 > 其他分享 >vue简单的表格增删改查

vue简单的表格增删改查

时间:2023-02-14 12:01:08浏览次数:50  
标签:vue return name 改查 item let 增删 border id

<iframe allowfullscreen="true" data-mediaembed="bilibili" src="https://player.bilibili.com/player.html?aid=92450219"></iframe>

bandicam 2020-02-28 19-59-01-717

html

<style>
			.table {
				border: 1px solid rgb(149, 140, 140);
				width: 100%;
			}

			.table thead tr th {
				border: 1px solid;
				padding-right: 20%;
				padding: 10px 5px;
				text-align: left;
			}

			.table tbody tr td {
				border: 1px solid;
				padding: 12px 7px;
				box-sizing: border-box;
			}

			.header {
				width: 100%;
				height: 80px;
				box-sizing: border-box;
				border: 1px solid #ccc;
			}

			.header span {
				display: block;
				height: 30px;
				background-color: rgba(0, 0, 255, 0.347);
				font-weight: bold;
				padding: 5px 5px;
			}

			.header input {
				height: 30px;
				max-width: 200px;
				border-radius: 5px;
				border: 1px solid #ccc;
				margin-top: 2px;
				outline: none;
			}

			.add {
				border: none;
				width: 80px;
				height: 30px;
				outline: none;
			}

			.light {
				background-color: rgb(62, 109, 250);
			}

			.errTest {
				border: 1px solid red !important;
			}
		</style>
	</head>

	<body>
		<div id="app"></div>
		<script>
			let App = {
				template: `<div id='app'>
        <div class='header'>
          <span>品牌查询</span>
          <strong>Id&nbsp;&nbsp;</strong><input type='text' v-model='id' @blur='test' :class='{errTest:isErr}' />
          <strong>Name&nbsp;&nbsp;</strong><input type='text' v-model='name' @keyup.enter='add'/>
          <button class='add' @click='add' :class="[this.id != ''? light:'']" :disabled="isDisable">添加</button>
          <input type='text' placeholder=" 查询" v-model='query' />
        </div>
      <table class="table" cellspacing="0">
        <colgroup span="1" width="50px"></colgroup>
        <colgroup span="1" width="180px"></colgroup>
        <colgroup span="2" width="100px"></colgroup>
        <thead>
          <tr>
            <th>Id</th>  
            <th>Name</th>  
            <th>Ctime</th>  
            <th>Operation</th>  
          </tr>  
        </thead>
        <tbody>
          <tr v-for='(item,index) in search(query)' :key='index' >
            <td>{{ item.id }}</td>  
            <td @dblclick='changeName(index)'><input @blur='getName'  v-model='newName'  @input='setName(index)' :style="{position:'absolute'}" type='text' v-show = 'index == currentIndex' /><span>{{ item.name }}</span></td>  
            <td>{{ item.time | dateFormat(true) }}</td>  
            <td><a href='JavaScript:void(0);' @click='remove(item.id)'>删除</a></td> 
          </tr> 
        </tbody>
      </table>
      </div>`,
				data() {
					return {
						id: '',
						name: '',
						list: [{
							id: 1,
							name: '奔驰',
							time: new Date(),
						}, {
							id: 2,
							name: '宝马',
							time: new Date(),
						}],
						light: 'light',
						isErr: false,
						isDisable: false,
						query: '',
						v: '',
						currentIndex: -1,
						change: '',
						newName: '',
					}
				},
				methods: {
					add() {
						if (this.name != '' && this.id != '' && this.isErr == false) {
							let item = {
								id: this.id,
								name: this.name,
								time: new Date()
							};
							this.list.push(item);
							this.id = this.name = '';
						} else {
							console.log("not");
						}
					},
					remove(v) {
						let index = this.list.findIndex(item => {
							if (item.id == v) {
								return true;
							}
						});
						this.list.splice(index, 1);
					},
					test() {
						let index = this.list.findIndex(item => {
							if (item.id == this.id) {
								return true;
							}
						});
						if (index == -1) {
							this.isErr = false;
							return;
						} else {
							this.isErr = true;
						}
					},
					search(query) {
						return this.list.filter(item => {
							//   if(item.name.indexOf(query) != -1){
							//ES6新提供的方法 includes('要查询的字符串')查到返回true,否则false
							if (item.name.includes(query)) {
								return item;
							}
						})
					},
					changeName(index) {
						this.currentIndex = index;
					},
					setName(index) {
						this.list[index].name = this.newName;
					},
					getName() {
						this.currentIndex = -1;
					}

				}
			}
			//日期过滤器
			Vue.filter('dateFormat', function(msg, parrten = false) {
				let t = new Date(msg);
				let y = t.getFullYear();
				let m = (t.getMonth() + 1).toString().padStart(2,'0');
				let d = t.getDate().toString().padStart(2,'0');
			
				if (parrten) {
					let hh = t.getHours().toString().padStart(2,'0');
					let mm = t.getMinutes().toString().padStart(2,'0');
					let ss = t.getSeconds().toString().padStart(2,'0');
					return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
				} else {
					return `${y}-${m}-${d}`;
				}
			})
			let vm = new Vue({
				el: "#app",
				components: {
					App
				},
				template: `<App></App>`
			})
		</script>
	</body>

标签:vue,return,name,改查,item,let,增删,border,id
From: https://blog.51cto.com/u_15964288/6056541

相关文章

  • VueCLI商城项目纪实day.3
    home页的图片区顶部滚动条:MUI的tab-top-webview-main组件设个组件需要用到mui的js文件引入//MUI的js文件,解决顶部滑块无法滑动问题 importmuifrom'../../lib/MUI......
  • Vue实现表格的增删改查
    bandicam2020-03-0109-41-46-926html<body> <divid="app"> <modelref='mod'v-show=isShow@addlist="addItem"@editlist='editDate'@cancle='cancle......
  • Vue props中的default值正确写法
    先看一个警告[Vuewarn]:Invaliddefaultvalueforprop"content":PropswithtypeObject/Arraymustuseafactoryfunctiontoreturnthedefaultvalue.//错......
  • VUE 项目大文件上传下载解决方案
    ​ 一、基本介绍 1,什么是WebUploader?WebUploader是由百度公司团队开发的一个以HTML5为主,FLASH为辅的现代文件上传组件。官网地址:http://fex.baidu.com/webuploa......
  • VsCode运行VUE3出现错误
        1.首先查看vscode版本2.安装最新稳定的nodejs可以在注册表查看一些有效或者无效的路径  可以cmd查看node-v  和 npm-v3.如果出现一些提示vite......
  • vue原理:diff、模板编译、渲染过程等
    一、虚拟DOM:因为DOM操作非常消耗性能,在操作DOM时,会出现DOM的回流(Reflow:元素大小或者位置发生改变)与重绘(元素样式的改变)使DOM重新渲染。现在的框架Vue和React很少直接操作......
  • vue 锚点定位和置顶
    锚点定位//vue中使用//标题<divclass="tabs"v-for="(item,index)intitAll":key="index":class="{actives:isactive===index}"@click="ta......
  • Vue3 hash and history mode
    今天在修改hash模式为history模式的时候,发现页面在重新刷新后直接显示404了。多方排查,发现vue.config.js中有配置publicPath:'./',此配置在hash模式下是成功的,但是当......
  • vue中兄弟组件传值event bus被多次触发以及踩过的坑
    传值方式:兄弟组件传值的时候,使用的是this.$bus.$emit触发事件和this.$emit.on监听事件。业务bug:点击按钮切换兄弟组件,再切换的过程$eventBus出发了多次。原因:事件订......
  • vue基础
     vue是一套用于构建用户界面的渐进式(vue可以自底向上逐层的应用;由一个轻量小巧的核心库到可以引入各式各样的vue插件)JavaScript框架。 vue的特点:1.采用组件化模式......