只通过前端,实现信息的增删查 <script>
var vm = new Vue({
el:'#app',
data:{
//2.创建数据
persons:[
{name:"西门吹雪", age:"4", sex:"男", phone:"15078950533",birthday:"1985.9.20",nation:"汉族",type:"测试",hobby:" 吃饭 睡觉 打豆豆"},
{name:"常德帥", age:"6", sex:"男", phone:"15878950532",birthday:"1984.9.20",nation:"汉族",type:"测试",hobby:" 睡觉 打豆豆"},
{name:"尤幼倩", age:"7", sex:"男", phone:"15078950531",birthday:"1984.9.20",nation:"蒙古族",type:"测试",hobby:" 吃饭打 豆豆"},
{name:"陆小凤", age:"4", sex:"男", phone:"15078950530",birthday:"1984.9.20",nation:"汉族",type:"前端",hobby:" 吃饭 睡觉 打豆豆 旅游"},
{name:"香帅", age:"6", sex:"男", phone:"15878950539",birthday:"1984.9.20",nation:"汉族",type:"测试",hobby:" 打豆豆 旅游"},
{name:"tom猫", age:"7", sex:"男", phone:"15078950537",birthday:"1984.9.20",nation:"汉族",type:"大数据",hobby:" 睡觉 打豆豆 旅游"},
{name:"董得多", age:"5", sex:"男", phone:"15078950538",birthday:"1984.9.20",nation:"汉族",type:"测试",hobby:" 吃饭 旅游"}
],
persons_bak:[
{name:"西门吹雪", age:"4", sex:"男", phone:"15078950533",birthday:"1985.9.20",nation:"汉族",type:"测试",hobby:" 吃饭 睡觉 打豆豆"},
{name:"常德帥", age:"6", sex:"男", phone:"15878950532",birthday:"1984.9.20",nation:"汉族",type:"测试",hobby:" 睡觉 打豆豆"},
{name:"尤幼倩", age:"7", sex:"男", phone:"15078950531",birthday:"1984.9.20",nation:"蒙古族",type:"测试",hobby:" 吃饭打 豆豆"},
{name:"陆小凤", age:"4", sex:"男", phone:"15078950530",birthday:"1984.9.20",nation:"汉族",type:"前端",hobby:" 吃饭 睡觉 打豆豆 旅游"},
{name:"香帅", age:"6", sex:"男", phone:"15878950539",birthday:"1984.9.20",nation:"汉族",type:"测试",hobby:" 打豆豆 旅游"},
{name:"tom猫", age:"7", sex:"男", phone:"15078950537",birthday:"1984.9.20",nation:"汉族",type:"大数据",hobby:" 睡觉 打豆豆 旅游"},
{name:"董得多", age:"5", sex:"男", phone:"15078950538",birthday:"1984.9.20",nation:"汉族",type:"测试",hobby:" 吃饭 旅游"}
],
// 5 创建一条空的新对象
newStudent:{name:'', age:'', sex:'男', phone:'',birthday:'1995.11.3',nation:"汉族",type:"测试",hobby:''},
hobbies:[],
name:'',
type:'',
//newStudent:{name:'', age:'', sex:'男', phone:'',birthday:'',nation:'',hobby:'',type:''}
},
//4.实现创建新用户的逻辑代码
//逻辑:创建一个对象,存放到建立好的数组中,然后添加到展示栏即可
//先要获取到创建新用户的点击按钮进行监听
methods:{
//persions_bk=this.persons;
// 4.1 创建一条新记录
createNewStudent(){
// 8 验证输入框姓名,实现不能为空创建
if(this.newStudent.name === ''){
alert('学生姓名不能为空哦');
return; //一旦姓名为空,不再执行创建用户的操作
}
// 8.1 验证输入框年龄不能小于0,实现不能为空创建
if(this.newStudent.age <= 0){
alert('请输入正确的年龄');
return;
}
// 8.1 验证输入框手机号码不能为空,实现不能为空创建
if(this.newStudent.phone === ''){ //应该通过正则验证手机号码的格式,这里只是验证不能为空
alert('请输入正确的手机号码');
return;
}
alert('确定创建此学生的档案?');
//4.2拿到前面的数组,把新建的对象放进去,
// 4.2并且产生的时候应该是排列在数组的最前面,使用数组的.persons.unshift()方法实现这个功能
// 4.2往数组中创建一条新内容
for (let hobby in this.hobbies)
{
this.newStudent.hobby =' ' this.hobbies[hobby];
//console.log(this.hobbies[hobby]);
//console.log(hobby);
}
this.persons.unshift(this.newStudent);//4.2此时不管输入框是否存在有效值,点击button按钮都会不停的创建新用户
//4.2所以要设置如果没有值的时候不能继续插入新的值
//7.1清空数据,实现创建新用户之后把输入框的内容清空,但是此时依然可以创建空的数据
this.newStudent = {name:'', age:'', sex:'男', phone:'',birthday:'1995.11.3',nation:"汉族",type:"测试",hobby:''};
this.hobbies=[];
//this.newStudent={name:'', age:'', sex:'男', phone:'',birthday:'',nation:'',hobby:'',type:''}
this.persons_bak=JSON.parse(JSON.stringify(this.persons));
},
//9.点击删除按钮的时候删除一条学生信息记录
//实现原理:获取到数组,从数组中删除,
//想要从数组中删除一条数据需要一条索引
deleteStudentMsg(index){
alert('确定要删除此学生的档案?');
this.persons.splice(index,1);
this.persons_bak=JSON.parse(JSON.stringify(this.persons));
},
searchStudent()
{
if(this.name==''&&this.type=='')
{
return;
}
else
{
for(j=0;this.persons[j]!=null;j )
{
if(this.type=='')
{
if (this.persons[j].name != this.name)
{
this.persons.splice(j,1);
j--;
}
}
else if(this.name=='')
{
if (this.persons[j].type != this.type)
{
this.persons.splice(j,1);
j--;
}
}
else if(this.persons[j].type != this.type||this.persons[j].name != this.name)
{
this.persons.splice(j,1);
j--;
}
else
continue;
}
}
},
resetStudent()
{
this.persons=JSON.parse(JSON.stringify(this.persons_bak));
this.name='';
this.type='';
}
},
});
</script>
评论