今天在开发中写了一个友情链接分享给大家方法
其实就是一个增删改查功能
那么直接不废话了直接代码,我这里没用用到模型,非常简单
首先创建一个Flink.php的控制器
//代码内容为
class Flink extends AdminController
{
//列表
public function index()
{
$flinkData=Db::name('flink')->paginate(10);//数据表名=data
return view('',[
'flinkData'=>$flinkData
]);
}
//添加数据
public function add(){
if(request()->isPost()){
$data=input('post.');
$res=Db::name('flink')->insert($data);
if($res){
return alert('操作完成-OJBK','index','6');
}else{
return alert('操作失败-不OJBK','index','5');
}
}else{
return view();
}
}
//修改数据
public function edit2(){
$id=input('id');
$flinkData=Db::name('flink')->find($id);
return view('',[
'flinkData'=>$flinkData
]);
}
//更新
public function update(){
$data=input('post.');
$res=Db::name('flink')->update($data);
if($res){
return alert('操作完成-OJBK','index','6');
}else{
return alert('操作失败-不OJBK','index','5');
}
}
//删除
/* public function del(){
$id=input('id');
$res=Db::name('flink')->delete($id);
if($res){
return alert('操作完成-OJBK','index','6');
}else{
return alert('操作失败-不OJBK','index','5');
}
}
*/
public function del(){
$id=input('id');
$res=Db::name('flink')->delete($id);
if($res){
return alert('操作完成-OJBK','index','6');
}else{
return alert('操作失败-不OJBK','index','5');
}
}
}
上面的代码我都有注释
接下来我们创建一个前端html页面来作为列表,我们就把他命名其为/flink/index.html
<div class="layui-fluid">
<div class="layui-row layui-col-space15">
<div class="layui-col-md12">
<div class="layui-card">
<div class="layui-card-body ">
</div>
<div class="layui-card-header">
<button class="btn btn-secondary dropdown-toggle" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
<button class="layui-btn" onclick="xadmin.open('添加链接','/admin.php/flink/add',800,600)"><i class="layui-icon"></i>添加</button>
</div>
<div class="layui-card-body layui-table-body layui-table-main">
<table class="layui-table layui-form">
<thead>
<tr>
<th>
<input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
</th>
<th>ID</th>
<th>名称</th>
<th>url</th>
<th> 操作</th>
</tr>
</thead>
<tbody>
{volist name="flinkData" id="vo"}
<tr>
<td>
<input type="checkbox" name="id" value="1" lay-skin="primary">
</td>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
<td>{$vo.url}</td>
<td class="td-manage">
<a title="编辑" onclick="xadmin.open('编辑','{:url('/flink/edit2',array('id'=>$vo.id))}','800','','600')" class="ml-5" style="text-decoration:none">
<i class="layui-icon"></i>
</a>
<a title="删除" href="javascript:;" onclick="link_del(this,'{$vo.id}')" style="text-decoration:none">
<i class="layui-icon"></i>
</a>
</td>
</tr>
{/volist}
接下载就是添加页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
友情链接
</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="shortcut icon" href="__PUBLICS__/favicon.ico" />
<link rel="stylesheet" href="__STATIC__/vendors/layui/css/layui.css" type="text/css">
<link rel="stylesheet" href="/static/css/font.css">
<link rel="stylesheet" href="/static/css/index.css">
<script src="./lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/js/index.js"></script>
</head>
<body>
<div class="layui-card">
<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
<div class="layui-tab-content">
<div class="layui-tab-item layui-show">
<form class="layui-form layui-form-pane" action="add" method="post">
<div class="layui-form-item">
<label class="layui-form-label">
<span class='x-red'>*</span>名称
</label>
<div class="layui-input-block">
<input type="text" name="name" autocomplete="off" placeholder="5个左右,8汉字以内,用英文,隔开" class="layui-input" lay-verify="required">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">
<span class='x-red'>*</span>URL
</label>
<div class="layui-input-block">
<input type="text" name="url" autocomplete="off" placeholder="输入url" class="layui-input" lay-verify="number">
</div>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" lay-submit="" lay-filter="*">
保存
</button>
</div>
</form>
<div style="height:100px;"></div>
</div>
</div>
</div>
</div>
<script>
layui.use(['element', 'layer', 'form'], function() {
$ = layui.jquery; //jquery
lement = layui.element(); //面包导航
layer = layui.layer; //弹出层
form = layui.form()
okLoading.close($);
//监听提交
form.on('submit(*)', function(data) {
var name = $("input[name=name]").val();
if (name == "") {
layer.msg("不能为空!", {
icon: 5,
time: 2000
});
return false;
}
var data = data.field;
$.ajax({
type: "post",
url: "add",
data: data,
dataType: "json",
success: function(data) {
if (data.status == 6) {
layer.msg(data.info, {
icon: 1,
time: 1000
});
setTimeout(function() {
window.parent.location.reload();
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
}, 1000);
return false;
} else {
layer.msg(data.info, {
icon: 5,
time: 2000
});
return false;
}
}
});
return false;
});
})
</script>
</body>
</html>
然后就是修改更新
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
友情链接
</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="shortcut icon" href="__PUBLICS__/favicon.ico" />
<link rel="stylesheet" href="__STATIC__/vendors/layui/css/layui.css" type="text/css">
<link rel="stylesheet" href="/static/css/font.css">
<link rel="stylesheet" href="/static/css/index.css">
<script src="./lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/js/index.js"></script>
</head>
<body>
<div class="layui-card">
<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
<div class="layui-tab-content">
<div class="layui-tab-item layui-show">
<form class="layui-form layui-form-pane" action="{:url('/flink/update')}" method="post">
<div class="layui-form-item">
<label class="layui-form-label">
<span class='x-red'>*</span>名称
</label>
<div class="layui-input-block">
<input type="text" name="name" autocomplete="off" value="{$flinkData.name}" placeholder="" class="layui-input" lay-verify="required">
<input type="hidden" class="input-texe" value="{$flinkData.id}" placeholder="" id="" name="id">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">
<span class='x-red'>*</span>URL
</label>
<div class="layui-input-block">
<input type="text" name="url" autocomplete="off" placeholder="输入url" value="{$flinkData.url}" class="layui-input" lay-verify="number">
</div>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" lay-submit="" lay-filter="*">
保存
</button>
</div>
</form>
<div style="height:100px;"></div>
</div>
</div>
</div>
</div>
<script>
</script>
</body>
</html>
非常简单的一个功能分享给大家,如需要网站,app,小程序,开发的可以点击定制开发栏目联系我哦!
我闪------
版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!