基于vue.js的增删改

<!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="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

</head>
<body>
  <div id="app">
    <div class="modal" v-show='isshow'>
      <div class="cotain">
        <div class="title"><span>编辑</span><span @click='isshow=false'>x</span></div>
        <div class="content">
          <input type="text" placeholder="标题" v-model='keywords'><br>
          <input type="text" placeholder="作者" v-model='tempItem.author'><br>
          <input type="date" v-model='tempItem.createtime'><br>
          <button @click=updateItem()>确认</button>
          <button @click='isshow=false'>取消</button>
        </div>
      </div>
    </div>
    <input type="text" placeholder="标题" v-model='keywords'><br>
    
    <input type="text" placeholder="标题" v-model='newItem.title'>
    <input type="text" placeholder="作者" v-model='newItem.author'>
    <input type="date" v-model='newItem.createtime'>
    <button @click='addItem'>确定</button>
    <table border="1" width='600'>
      <tr bgcolor='lightblue'>
        <th>序号</th>
        <th>标题</th>
        <th>作者</th>
        <th>日期</th>
        <th>操作</th>
      </tr>
      <tr v-for="(item,index) in fillist">
        <td>{{index+1}}</td>
        <td>{{item.title}}</td>
        <td>{{item.author}}</td>
        <td>{{item.createtime}}</td>
        <td>
          <a href="javascript:void(0);" @click=deleteItem(item)>删除</a>|
          <a href="javascript:void(0);" @click=editItem(item,index)>编辑</a>

        </td>
      </tr>
    </table>
  </div>
</body>
</html>
<!-- <script src="./js/vue.js"></script>  //js代码为vue2的js代码 -->
		<script>
			// 初始化vue实例
			new Vue({
				el: '#app',
				data: {
					keywords:'',
					isshow: false, //是否显示模态框
					editIndex: null, //正在编辑的行号
					newItem: { //新增的对象
						title: "",
						author: '',
						createtime: ''
					},
					tempItem: { //正在编辑的对象
						title: "",
						author: '',
						createtime: ''
					}, //新增的对象
					list: [{
							title: "奇酷",
							author: 'mdy',
							createtime: '2022-09-30'
						},
						{
							title: "考研",
							author: 'dyh',
							createtime: '2022-09-28'
						}
					]
				},
				methods: {
					//添加
					addItem() {
						// 向list新增item
						this.list.unshift({
							...this.newItem
						});
						//清空数据
						this.newItem = {
							title: "",
							author: '',
							createtime: ''
						}
					},
					//删除
					deleteItem(obj) {
						var index = this.list.indexOf(obj);
						if (confirm("确认删除吗?")) {
							this.list.splice(index, 1);
						}
 
					},
					//编辑渲染
					editItem(item, index) {
						// 1、显示模态框
						this.isshow = true;
						// 2、渲染数据
						this.tempItem = {
							...item
						};
						this.editIndex = index; //正在编辑的行
 
					},
					// 确认修改
					updateItem() {
						this.list[this.editIndex] = {
							...this.tempItem
						};
						//关闭模态框
						this.isshow=false;
						//清空
						this.tempItem = { //正在编辑的对象
							title: "",
							author: '',
							createtime: ''
						}
					}
				},
				computed:{
					//过滤出的list	
					fillist(){
						if(this.keywords.trim()==""){
							return this.list;
						}else{
							return this.list.filter(item=>{
								return item.title.includes(this.keywords);
							})
						}
						
					}
				}
			})
		</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值