摘要:添加操作news/doAdd操作add.html $.post(url,data,function(data){})public function doAdd(){ // 获取数据 $data = Request::param
添加操作news/doAdd操作
add.html $.post(url,data,function(data){})
public function doAdd(){
// 获取数据
$data = Request::param();
halt($data);
// 加入发布的时间
$data['time'] = time();
// 加入发布新闻的管理员名称
$data['username'] = Session::get('username');
// 将新闻标题独立出来
$title = $data['title'];
// 将新闻标题作为条件来查询数据
$news = NewsModel::where('title', "$title")->find();
// 验证新闻标题是否重复
if ($news == true) {
// 重复并返回提示信息
return ['res' => 0, 'msg' => '新闻标题重复!'];
}
// 实例化模型
$new = new NewsModel();
// 存储验证
if ($new->save($data)) {
// 返回发布成功的信息
return ['res' => 1, 'msg' => '发布成功!'];
} else {
// 返回发布失败的信息
return ['res' => 0, 'msg' => '发布失败!'];
}
}
form.on('submit(add)', function (data) {
console.log(data);
//发异步,把数据提交给php
$.post('{:url(\'doAdd\')}', {
'title': $('#title').val(),
'desc': $('#desc').val(),
'content': editor.txt.html(),
}, function (data) {
if (data.res == 1) {
layer.alert(data.msg, {icon: 6}, function () {
// 获得frame索引
var index = parent.layer.getFrameIndex(window.name);
//关闭当前frame
parent.layer.close(index);
});
} else {
layer.alert(data.msg, {icon: 6}, function () {
// 获得frame索引
var index = parent.layer.getFrameIndex(window.name);
//关闭当前frame
parent.layer.close(index);
});
}
})
return false;
});
});修改操作
public function doEdit(){
//获取提交的数据
$data=Request::param();
$new=new NewsModel();
$res=$new->save([
'title'=>$data['title'],
'desc'=>$data['desc'],
'content'=>$data['content'],
'username'=>Session::get('username'),
'time'=>time(),
],['id'=>$data['id']]);
if($res){
return ['res'=>1,'msg'=>'修改成功'];
}else{
return ['res'=>0,'msg'=>'修改失败'];
}
} edit.html中的$.post(url,data,function(data){})
//监听提交
form.on('submit(add)', function(data){
console.log(data);
//发异步,把数据提交给php
$.post('{:url(\'doEdit\')}',{
'title':$('#title').val(),
'id':$('#id').val(),
'desc':$('#desc').val(),
'content':editor.txt.html(),
},function(data){
if(data.msg==1){
layer.alert(data.msg, {icon: 6},function () {
// 获得frame索引
var index = parent.layer.getFrameIndex(window.name);
//关闭当前frame
parent.layer.close(index);
});
}else{
layer.alert(data.msg, {icon: 6},function () {
// 获得frame索引
var index = parent.layer.getFrameIndex(window.name);
//关闭当前frame
parent.layer.close(index);
});
}
})
return false;
});删除操作
public function del(){
$newId=Request::param('id');
$new=new NewsModel();
if($new->destroy($newId)){
return ['res'=>1,'msg'=>'删除成功'];
}else{
return ['res'=>0,'msg'=>'删除失败'];
}
} function member_del(obj,id){
layer.confirm('确认要删除吗?',function(index){
//发异步删除数据
$.get('{:url(\'del\')}','id='+id,function(data){
if(data.res==1){
$(obj).parents("tr").remove();
layer.msg(data.msg,{icon:1,time:1000});
}
})
});
}展示操作
public function index(){
$news=new NewsModel();
$new=$news->order('id','desc')->paginate(8);
$this->assign('new',$new);
return $this->fetch();
} {volist name="new" id="news"}
<tr>
<td>{$news.id}</td>
<td>{$news.title}</td>
<td><img src="#"></td>
<td>{$news.desc}</td>
<td>{$news.username}</td>
<td>{$news.time|date="Y-m-d H:i:s"}</td>
<td class="td-manage">
<a title="查看" onclick="x_admin_show('编辑','{:url(\'News/edit\')}?id={$news.id}')" href="javascript:;">
<i class="layui-icon"></i>
</a>
<a title="删除" onclick="member_del(this,'{$news.id}')" href="javascript:;">
<i class="layui-icon"></i>
</a>
</td>
</tr>
{/volist}
批改老师:查无此人批改时间:2018-11-25 13:34:39
老师总结:代码写的不错,后台功能比较容易,都是些展示数据的。加油。