美文网首页
开发企业网站7 -- 新闻分类的修改和删除

开发企业网站7 -- 新闻分类的修改和删除

作者: 潘肚饿兵哥哥 | 来源:发表于2019-10-04 22:56 被阅读0次

\color{rgba(254, 67, 101, .8)}{修改}

<!-- 用前面封装好的show_window(在header.html页面),来显示弹窗 -->
<!-- 修改页面在嵌入的子页面 -->
<!-- 添加不需要带id,但是修改要,要确认改的是哪一个 -->
<a style="text-decoration:none" class="ml-5" onClick="show_window('修改分类',
'index.php?m=admin&c=news_category_add&id=<{$v.id}>')" href="javascript:;" title="编辑">
<i class="Hui-iconfont">&#xe6df;</i></a> 

\color{rgba(254, 67, 101, .8)}{修改功能需要ID,用来确认到底是哪一个}

\color{rgba(254, 67, 101, .8)}{点击编辑之后,出现修改页面}

image.png

\color{rgba(254, 67, 101, .8)}{在这个页面查看框架源代码,在搜索栏可以看到ID被带过来了}

image.png

\color{rgba(254, 67, 101, .8)}{然后在news\_category\_add.php页面处理获取到的ID}
\color{rgba(254, 67, 101, .8)}{处理逻辑:用if检测id值是否大于0}
\color{rgba(254, 67, 101, .8)}{如果大于0.表示有id,带着id就证明是修改,没有ID就是添加}
\color{rgba(254, 67, 101, .8)}{然后用数据库查询函数查出这个id}

<?php
    require_once('init.php');
    if( !empty($_POST)){
        //在这里接收html页面传过来的三个值
        $name = $_POST['name'];
        $is_show = $_POST['is_show'];
        //这个值有默认值,所以判断一下
        //传值就使用传过来的值,空值就使用默认值0
        $sort = !empty($_POST['sort'])?intval($_POST['sort']):0;
        
        //接收到上面出递过来的值之后,用sql语句将数据写入表中
        $sql = "insert into news_category(name, is_show, sort) values ('$name','$is_show', '$sort')";

        //使用封装好的操作函数库的查询函数调用它,将上面的数据库语言作为参数传给函数
        $ret = execute($sql);
        //因为是异步操作,所以查询完了之后不能直接跳转页面,需要进行判定
        //然后在news_category_add.html页面接收值
        if($ret !== false){
            echo 1;
        }else{
            echo 0;
        }

    }else{
        //修改功能:
        //通过GET方法获取news_category.html带过来的id(这个ID在搜索框中)
        $id = $_GET['id'];
        //逻辑:如果获取到的ID大于0,表示有id,那么就是修改
        //然后用数据库查询函数查出这个id
        //然后用smarty把这个查到的值传递给
        if($id > 0){
            $row = get_row("select * from news_category where id=$id limit 1");
        }
        $smarty->assign('row', $row);
        $smarty->display($tpl_name);
    }
    
?>

\color{rgba(254, 67, 101, .8)}{然后用smarty把这个查到的值分配给news\_category\_add.html}

<div class="row cl">
        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>名称:</label>
        <div class="formControls col-xs-8 col-sm-9">
            <input type="text" class="input-text" value="<{$row.name}>"  name="name">
        </div>
    </div>

\color{rgba(254, 67, 101, .8)}{同时排序也接收这个值}

<div class="row cl">
        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>排序:</label>
        <div class="formControls col-xs-8 col-sm-9">
            <input type="text" class="input-text" value="<{$row.sort}>" name="sort">
        </div>
    </div>

\color{rgba(254, 67, 101, .8)}{这样,点击编辑(修改)图标,就出来了}

image.png

\color{rgba(254, 67, 101, .8)}{之前的显示是checked默认显示的}
\color{rgba(254, 67, 101, .8)}{现在在news\_category\_add.html进行判断}

如果 is_show=1, 那么显示 checked
如果is_show =0,那么隐藏checked

<div class="row cl">
    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>显示:</label>
    <div class="formControls col-xs-8 col-sm-9 skin-minimal">
        <div class="radio-box">
            <input name="is_show" type="radio" id="show-1" value="1" <{if $row.is_show == 1}>checked <{/if}>>
            <label for="show-1">显示</label>
        </div>
        <div class="radio-box">
            <input type="radio" id="show-2" name="is_show" value="0" <{if $row.is_show == 0}>checked <{/if}>>
            <label for="show-2">隐藏</label>
        </div>
    </div>

\color{rgba(254, 67, 101, .8)}{此时显示或隐藏也已选中}

image.png

\color{rgba(254, 67, 101, .8)}{由于之前的sql语句还有一个查询的,所以是不知道现在提交的数据到底是增加还是修改}
\color{rgba(254, 67, 101, .8)}{在news\_category\_add.html中使用之前分配的id}
\color{rgba(254, 67, 101, .8)}{然后在news\_category\_add.php页面用post['id']接收}
\color{rgba(254, 67, 101, .8)}{然后用if判断}

<div class="row cl">
        <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input type="hidden" name="id" value="<{$row.id}>">
            <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
        </div>
    </div>

\color{rgba(254, 67, 101, .8)}{把之前的添加成功改成操作成功}

success: function(data){
                    if(data == 1){
                        //time:2000这个函数弹出的弹窗默认不会自动关闭
                        //设置时间就是自动关闭提示信息的时间
                        layer.msg('操作成功!',{icon:1,time:2000});

                        //视频中这里不显示弹窗提示,因为程序执行时间太短,所以需要加定时器
                        //但是自己的是显示的
                        //这个函数表示:前面是要做的事,后面设置时间为2秒
                        //这个函数表示2秒之后执行函数里的内容
                        //setTimeout(function(){},2000);
                        setTimeout(function(){
                        //如果添加成功,在提示成功之后,关闭当前窗口
                        //并刷新父级窗口,显示新添加信息
                        //逻辑:程序自身无法杀自身,只能通过其他程序
                        //所以这里要先找到他的父级,再刷新父级,再通过父级关闭它
                        //之所以刷新在中间,而不是先关闭再刷新
                        //是因为关闭了它自己就没有子级父级的概念了
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.location.reload();
                            parent.layer.close(index);
                        },2000);
                    }else{
                        layer.msg('操作失败!',{icon:2,time:2000});// 2000表示提示信息留存时间  icon是一个 X 字体图标
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    layer.msg('error!',{icon:1,time:1000});
                }

\color{rgba(254, 67, 101, .8)}{原本娱乐新闻是这样的:}

image.png

\color{rgba(254, 67, 101, .8)}{进行修改后,证明修改成功了}

image.png

\color{rgba(254, 67, 101, .8)}{删除功能}
\color{rgba(254, 67, 101, .8)}{模板已经有一个article\_del方法了,只需要往里面填东西就可以了}

<a style="text-decoration:none" 
class="ml-5" onClick="article_del(this,'10001')" 
href="javascript:;" title="删除"><i class="Hui-iconfont">&#xe6e2;</i></a>
                    </td>
                </tr>
                <{/foreach}>
            </tbody>
        </table>
    </div>
</div>


<!--请在下方写此页面业务相关的脚本-->


<script type="text/javascript">

/*资讯-删除功能*/
function article_del(obj,id){
    layer.confirm('确认要删除吗?',function(index){
        $.ajax({
            type: 'POST',
            url: '',
            dataType: 'json',
            success: function(data){
                $(obj).parents("tr").remove();
                layer.msg('已删除!',{icon:1,time:1000});
            },
            error:function(data) {
                console.log(data.msg);
            },
        });     
    });
}

10001是id号

1.onClick="article_del(this,'<{$v.id}>')",id要修改成模板传过来的id
2.下面模板写好了删除方法,只需要传值和建立删除的页面就可以了
3.url就是新建的删除的页面url

\color{rgba(254, 67, 101, .8)}{改成这样,然后根据写好的url新建php文件}
\color{rgba(254, 67, 101, .8)}{删除不需要html页面,所以这里只有php文件}

<a style="text-decoration:none" class="ml-5" onClick="article_del(this,'<{$v.id}>')" href="javascript:;" title="删除"><i class="Hui-iconfont">&#xe6e2;</i></a>
                    </td>
                </tr>
                <{/foreach}>
            </tbody>
        </table>
    </div>
</div>


<!--请在下方写此页面业务相关的脚本-->


<script type="text/javascript">

/*资讯-删除功能*/
function article_del(obj,id){
    layer.confirm('确认要删除吗?',function(index){
        $.ajax({
            type: 'POST',
            url: 'index.php?m=admin&c=news_category_del',
            //data:('id', id)等价于下面的 data:(id, id)
            //这是一个键值对,前面的id是键,后面的是值
            data:(id, id),
            dataType: 'json',
            success: function(data){
                $(obj).parents("tr").remove();
                layer.msg('已删除!',{icon:1,time:1000});
            },
            error:function(data) {
                console.log(data.msg);
            },
        });     
    });
}

\color{rgba(254, 67, 101, .8)}{建立了news\_category\_del.php页面后,还是先包含初始化文件init.php}

<?php
//删除功能 -- 还是先包含初始化文件
require_once('init.php');

//news_category.html页面用的是POST方式发送信息,所以这里也用post接收
$id = $_POST['id'];
echo $id;
?>

\color{rgba(254, 67, 101, .8)}{此时在添加/删除页面,点击检查打开浏览器控制台}
\color{rgba(254, 67, 101, .8)}{然后点击删除,确定}

image.png

\color{rgba(254, 67, 101, .8)}{可以看到已经ID为2的一项已经被删掉了,而且2作为返回值也返回了}
\color{rgba(254, 67, 101, .8)}{这个值就在news\_category\_del.php页面}

$id = $_POST['id'];这里存的就是这个id

\color{rgba(254, 67, 101, .8)}{接下来写一个数据库删除语句删掉ID对应的项}
\color{rgba(254, 67, 101, .8)}{并且,用if进行判定到底成功了没有}
\color{rgba(254, 67, 101, .8)}{因为是异步操作,所以查询完了之后不能直接跳转页面,需要进行判定}

<?php
//删除功能 -- 还是先包含初始化文件
require_once('init.php');

//news_category.html页面用的是POST方式发送信息,所以这里也用post接收
$id = $_POST['id'];
//此时,被删除项的id已经拿到,接下来就是写一个sql语句,将他从数据库删掉
$sql = "delete from news_category where id='$id'";
//在删掉之后,用一个if判断它到底删除成功还是失败
 //使用封装好的操作函数库的查询函数调用它,将上面的数据库语言作为参数传给函数
 $ret = execute($sql);
 //因为是异步操作,所以查询完了之后不能直接跳转页面,需要进行判定
 //然后在news_category_add.html页面接收值
 if($ret !== false){
     echo 1;
 }else{
     echo 0;
 }

?>

\color{rgba(254, 67, 101, .8)}{然后,在news\_category.html页面中进行判定}
\color{rgba(254, 67, 101, .8)}{如果接收到1,输出成功,否则输出失败}

image.png

\color{rgba(254, 67, 101, .8)}{在删除成功之后,总数量的统计也要相应的减1}
\color{rgba(254, 67, 101, .8)}{先在news\_category.html加一个ID:total}

</span> <span class="r" id="total">共有数据:<{$count}> 条</span>

\color{rgba(254, 67, 101, .8)}{上面这样加是错的,total下面经过JS处理之后,不能显示,因为其中有文字}
\color{rgba(254, 67, 101, .8)}{给count加一个标签,直接把id给这个新加的标签}

</span> <span class="r" >共有数据:<strong id="total"> <{$count}> </strong>条</span>

\color{rgba(254, 67, 101, .8)}{然后在下面函数中改}
\color{rgba(254, 67, 101, .8)}{先拿到id}

var total = $.trim($("#total").text());

\color{rgba(254, 67, 101, .8)}{这里有一个问题,这个总量原本是一个数字}
\color{rgba(254, 67, 101, .8)}{但是经过js获取之后,会变成一个字符串}
\color{rgba(254, 67, 101, .8)}{用parseInt把他转成一个数字,然后如果成功就减1}

var total = parseInt($.trim($("#total").text()));
total = total - 1;

\color{rgba(254, 67, 101, .8)}{然后用text()把值重新赋回去}
\color{rgba(254, 67, 101, .8)}{这个text()如果给值就是赋值,不给就是获取值}

/*资讯-删除功能*/
function article_del(obj,id){
    layer.confirm('确认要删除吗?',function(index){
        $.ajax({
            type: 'POST',
            url: 'index.php?m=admin&c=news_category_del',
            //data:('id', id)等价于下面的 data:(id, id)
            //这是一个键值对,前面的id是键,后面的是值
            data:{id:id},
            dataType: 'json',
            success: function(data){
                if(data == 1){
                    //这个text不给值就是获取值,给值就是修改值
                    //trim  取消空白,以防出现空白问题以致出错
                    //这个总量原本是一个数字,但是经过js获取之后,会变成一个字符串
                    //用parseInt把他转成一个数字
                    var total = parseInt($.trim($("#total").text()));
                    total = total - 1;
                    $(obj).parents("tr").remove();
                    $("#total").text(total);
                    layer.msg('删除成功!',{icon:1,time:2000});
                }else{
                    layer.msg('已删除!',{icon:2,time:2000});
                }
            },
            error:function(data) {
                console.log(data.msg);
            },
        });     
    });
}
image.png

\color{rgba(254, 67, 101, .8)}{在添加的时候可以给显示和排序一个默认值}
\color{rgba(254, 67, 101, .8)}{这样在添加的时候,直接写名字就可以了,有需要才修改}

//修改功能:
        //通过GET方法获取news_category.html带过来的id(这个ID在搜索框中)
        $id = $_GET['id'];
        //逻辑:如果获取到的ID大于0,表示有id,那么就是修改
        //然后用数据库查询函数查出这个id
        //然后用smarty把这个查到的值传递给
        //else是做添加时默认选项功能
        //else后面是把嵌入的news_category_add.html页面的显示和排序的默认值0和1设置默认值
        //这样在添加的时候,就只需要填写名称了,有需要才填显示隐藏和排序
        if($id > 0){
            $row = get_row("select * from news_category where id=$id limit 1");
        }else{
            $row = ['is_show'=>1, 'sort'=>0];
        }

        $smarty->assign('row', $row);
        $smarty->display($tpl_name);
image.png

\color{rgba(254, 67, 101, .8)}{批量删除}

方法名在模板中已经有了:
news_category.html:
onclick="datadel()"
但这只是一个方法名还不能用,这个功能需要代码实现

\color{rgba(254, 67, 101, .8)}{在news\_category.html页面下方写一个函数datadel}
\color{rgba(254, 67, 101, .8)}{批量删除功能的逻辑和处理方式:}

删除功能先在news_category.html页面写一个函数
还是交给news_category_del.php页面处理
把要删除项的ID传到news_category_del.php页面去处理

\color{rgba(254, 67, 101, .8)}{单个删除上面已经写好了,在他的基础上进行修改}
\color{rgba(254, 67, 101, .8)}{要拿到ID,就先给每一项的复选框传入ID和name}

<td><input type="checkbox" value="<{$v.id}>" name="del[]"></td>

\color{rgba(254, 67, 101, .8)}{要批量删除不能直接弹出这样的提示框}

image.png

\color{rgba(254, 67, 101, .8)}{需要提示更具体的信息}

先定义一个变量id为空
然后需要知道用户到底点了哪几项:
下面这个函数的意思是选中名字为del[]的input标签,并且要含有checked属性(当勾选她的时候,他就含有checked,这个之前已经写了)
这个是一个函数,只不过它的函数名就是
然后遍历
$("input[name='del[]']:checked").each();

\color{rgba(254, 67, 101, .8)}{}
\color{rgba(254, 67, 101, .8)}{}
\color{rgba(254, 67, 101, .8)}{}
\color{rgba(254, 67, 101, .8)}{}



\color{rgba(53, 93, 129, .8)}{}
蓝色
\color{rgba(3, 101, 100, .8)}{}
绿:内容

\color{rgba(254, 67, 101, .8)}{}

红:标题


⇄ ⇅ ⇆ ⇇ ⇈ ⇉ ⇊ ⇋ ⇌

相关文章

网友评论

      本文标题:开发企业网站7 -- 新闻分类的修改和删除

      本文链接:https://www.haomeiwen.com/subject/qanwuctx.html