PHP 实现上移下移置顶功能

1.数据库:



id主键int(11)  

sortint(11)  1排序

topint(1)  1是否置顶 1为不置顶,2为置顶

2.视图:

sortint(11)  1排序

<div class="layui-form news_list">
            <table class="layui-table" lay-filter="test" >
                <colgroup>
                    <col width="">
                    <col width="">
                    <col width="">
                    <col width="350">
                </colgroup>
                <thead>
                <tr>
                    <th>名称</th>
                    <th>图片</th>
                    <th>启用状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="listbox"></tbody>
            </table>
            </div>
        </div>
    </div>
@stop
@section("javascript")
    <script src="{{asseturl("js/beginbanner/index.js")}}"></script>

3.js代码:

sortint(11)  1排序

top:function () {
    var id = $(this).data("id");
    var state = $(this).data("state");
    var auditMsg = "";
    if(state==1){
        auditMsg = "您确认取消置顶吗?";
    }else{
        auditMsg = "您确认置顶吗?";
    }
    layer.confirm(auditMsg, {
        btn: ['确认', '取消'] //按钮
        , title: '<i class="layui-icon"></i> 确认提示'
        , icon: 0
    }, function () {
        $.ajax({
            url: adminurl + "/beginbanner/topstatus",
            data: {
                id: id,
                status:state,
                _token: _token,
            },
            type: "PUT",
            dataType: "json",
            beforeSend: function (request) {
                index = layer.load(2, {
                    shade: [0.1, '#FFF'] //0.1透明度的白色背景
                });
            },
            success: function (res) {
                layer.close(index);
                if (res.code == 1) {
                    layer.closeAll();
                    layer.msg(res.msg);
                    setTimeout(function () {
                        layer.closeAll();
                        getDataLists(location.hash.replace('#!page=',''), "", "");
                    }, 200)
                } else if (res.code == 1002) {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        location.href = adminurl + "/login";
                    }, 200)
                } else {
                    layer.msg(res.msg);
                    return false;
                }
            },
            error: function () {
                layer.close(index);
                layer.msg("网络请求错误,稍后重试!");
                return false;
            }
        });
    });
},
upmobile:function () {
    var thisObj = $(this).parent().parent();
    var getUp = thisObj.prev();
    if ($(getUp).has(".handle").size()==0) {
        layer.msg("已到最顶端");
        return false;
    }else{
        //当前操作ID
        var thisId = $(this).data("id");
        var thisSort = $(this).data("sort");
        //上级操作ID
        var upId = $(getUp).find("a.upmobile").data("id");
        var upSort = $(getUp).find("a.upmobile").data("sort");
        $.ajax({
            url: adminurl + "/beginbanner/move",
            data: {
                this_id: thisId,
                this_sort: thisSort,
                new_id: upId,
                new_sort: upSort,
                _token:_token
            },
            type: "put",
            dataType: "json",
            beforeSend: function (request) {
                index = layer.load(2, {
                    shade: [0.1, '#FFF'] //0.1透明度的白色背景
                });
            },
            success: function (res) {
                layer.close(index);
                if (res.code == 1) {
                    layer.closeAll();
                    $(thisObj).after(getUp);
                } else if (res.code == 1002) {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        location.href = adminurl + "/login";
                    }, 500)
                } else {
                    layer.msg(res.msg);
                    return false;
                }
            },
            error: function () {
                layer.close(index);
                layer.msg("网络请求错误,稍后重试!");
                return false;
            }
        });
    }
},
downmobile:function () {
    var thisObj = $(this).parent().parent();
    var getDown = thisObj.next();
    if ($(getDown).has(".handle").size() == 0) {
        layer.msg("已到最底端");
        return false;
    } else {
        //当前操作ID
        var thisId = $(this).data("id");
        var thisSort = $(this).data("sort");
        //上级操作ID
        var upId = $(getDown).find("a.upmobile").data("id");
        var upSort = $(getDown).find("a.upmobile").data("sort");
        $.ajax({
            url: adminurl + "/beginbanner/move",
            data: {
                this_id: upId,
                this_sort: thisSort,
                new_id: thisId,
                new_sort: upSort,
                _token: _token
            },
            type: "put",
            dataType: "json",
            beforeSend: function (request) {
                index = layer.load(2, {
                    shade: [0.1, '#FFF'] //0.1透明度的白色背景
                });
            },
            success: function (res) {
                layer.close(index);
                if (res.code == 1) {
                    layer.closeAll();
                    $(thisObj).before(getDown);
                } else if (res.code == 1002) {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        location.href = adminurl + "/login";
                    }, 500)
                } else {
                    layer.msg(res.msg);
                    return false;
                }
            },
            error: function () {
                layer.close(index);
                layer.msg("网络请求错误,稍后重试!");
                return false;
            }
        });
    }
}


sortint(11)  1排序

//数据格式化模板
function floatDataTpl(data, page) {
    layui.each(data, function (index, item) {
        innerData += "<tr>";
        innerData += '<td>' + item.bannername + '</td>';
        if(item.state == 1){
            innerData +='<td><img style="cursor: pointer" data-method="viewGoodsBigImg" class="handle" title="点击查看大图" height="30px" src="' + item.path + '"/></td>';
        }else{
            innerData +='<td><img style="cursor: pointer" data-method="viewGoodsBigImg"  title="点击查看大图" height="30px" src="' + item.path + '"/></td>>';
        }

        if(item.state==1){
                innerData += '<td>'+'<input type="checkbox" name="is_use" lay-filter="is_use" lay-skin="switch" lay-text="是|否" data-id="'+item.id+'" checked>' +  '</td>';
        }else if(item.state==2){
                innerData += '<td>'+'<input type="checkbox" name="is_use" lay-filter="is_use" lay-skin="switch"  lay-text="是|否" data-id="'+item.id+'">' +  '</td>';
        }
        innerData += '<td>';
        innerData += '<a  href= "' + adminurl + '/beginbanner/edit/' + item.id + '" class="layui-btn layui-btn-sm layui-btn handle" data-id="' + item.id + '" data-state="2"><i class="layui-icon"></i>编辑</a>' +
                     '<a data-method="delet" class="layui-btn layui-btn-sm layui-btn-danger handle" data-id="' + item.id + '" data-state="3">删除</a>';
        innerData += '<a href="javascript:;" data-method="upmobile" class="layui-btn layui-btn-sm layui-bg-cyan upmobile handle" data-id="' + item.id + '" data-sort="' + item.sort + '"><i class="layui-icon"></i>上移</a>';
        innerData += '<a href="javascript:;" data-method="downmobile" class="layui-btn layui-btn-sm layui-bg-cyan downmobile handle" data-id="' + item.id + '" data-sort="' + item.sort + '"><i class="layui-icon"></i>下移</a>';
        if(item.top == 1) {
            innerData += '<a data-method="top" class="layui-btn layui-btn-sm layui-btn handle" data-id="' + item.id + '" data-state="2">置顶</a>';
         }else if(item.top == 2) {
            innerData += '<a data-method="top" class="layui-btn layui-btn-sm layui-btn handle" data-id="' + item.id + '" data-state="1">取消置顶</a>';
        }
        innerData += '</td>';
        innerData +='</tr>';
    });
}


4.控制器层:


/**
 * @param Request $request更新置顶操作
 */
public function topStatus(Request $request)
{
    $this->actionLog("是否置顶操作");
    $result = $this->banner->topStatus($request);
    return result($result->msg, $result->code, $result->data);
}

/**
 * @param Request $request替换两个的排序
 */
public function move(Request $request)
{
    $id1 = intval($request->get('this_id'));
    $id2 = intval($request->get('new_id'));
    DB::beginTransaction();
    try {
        $sort1 = DB::table($this->table)->select('sort')->where('id', $id1)->first()->sort;
        $sort2 = DB::table($this->table)->select('sort')->where('id', $id2)->first()->sort;
        $ret1 = DB::table($this->table)->where('id', $id1)->update(['sort' => $sort2]);
        $ret2 = DB::table($this->table)->where('id', $id2)->update(['sort' => $sort1]);
        if (!($ret1 && $ret2)) {
            throw new \Exception('操作异常');
        }
        DB::commit();
    } catch (\Exception $e) {
        DB::rollBack();
        return result($e->getMessage());
    }
    return result('保存成功', 1);
}


5.model层:

sortint(11)  1排序

/**
 * @param Request $request是否置顶
 */
public function topStatus($request)
{
    $id = $request->get('id');
    $res = DB::table($this->table)->where('id', $id)->update(['top'=>$request->get('status')]);
    if($res){
        return returnData('操作成功', 1);
    }else{
        return returnData('操作失败');
    }
}


sortint(11)  1排序
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭