一、列表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" type="text/css" href="./layui/css/layui.css" />
<script type="text/javascript" src="./layui/layui.js"></script>
</head>
<body>
<script type="text/html" id="toolbar">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
<button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
<button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
</div>
</script>
<table id="demo" lay-filter="test"></table>
<script>
layui.use("table", function () {
var table = layui.table;
table.render({
toolbar: "#toolbar",
elem: "#demo",
url: "http://admin.ouyangke.cn/index.php/api/Layui/city",
where: {
id: 0,
status: 1,
},
cols: [
[
{ type: "radio" },
{ field: "area_id", title: "ID", width: 120, sort: true },
{ field: "area_name", title: "城市名", unresize: true },
{ field: "area_ip_desc", title: "归属", align: "right" },
{ field: "first_pinyin", title: "拼音" },
{ field: "pinyin", title: "首字母", minWidth: 500, sort: true },
{ field: "status", title: "状态", style: "color:red;" },
],
],
page: true,
limit: 10,
limits: [10, 20, 30],
});
table.on("row(test)", function (obj) {
obj.tr.find("div.layui-unselect.layui-form-radio")[0].click(); //单选
});
table.on("toolbar(test)", function (obj) {
console.log(obj.event);
switch (obj.event) {
case "add":
layer.msg("添加");
break;
case "update":
layer.msg("编辑");
break;
case "delete":
layer.msg("删除");
break;
}
});
});
</script>
</body>
</html>
二、添加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" type="text/css" href="./layui/css/layui.css" />
<script type="text/javascript" src="./layui/layui.js"></script>
</head>
<body>
<script type="text/html" id="toolbar">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
<button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
<button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
</div>
</script>
<table id="demo" lay-filter="test"></table>
<script>
layui.use("table", function () {
var table = layui.table;
var $ = layui.jquery;
var form = layui.form;
table.render({
toolbar: "#toolbar",
elem: "#demo",
url: "http://admin.ouyangke.cn/index.php/api/Layui/city",
where: {
id: 0,
status: 1,
},
cols: [
[
{ type: "radio" },
{ field: "area_id", title: "ID", width: 120 },
{ field: "area_name", title: "城市名" },
{ field: "area_ip_desc", title: "归属", align: "right" },
{ field: "first_pinyin", title: "拼音" },
{ field: "pinyin", title: "首字母" },
{ field: "status", title: "状态", style: "color:red;" },
],
],
page: true,
limit: 10,
limits: [10, 20, 30],
});
table.on("toolbar(test)", function (obj) {
switch (obj.event) {
case "add":
layer.full(
layer.open({
title: "添加",
type: 2,
content: "./add.html",
maxmin: true,
btn: ["确定", "关闭"],
yes: function (index, layero) {},
})
);
break;
case "update":
layer.msg("编辑");
break;
case "delete":
layer.msg("删除");
break;
}
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" type="text/css" href="./layui/css/layui.css" />
<script type="text/javascript" src="./layui/layui.js"></script>
</head>
<body>
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">城市名</label>
<div class="layui-input-block">
<input
type="text"
class="layui-input"
id="area_name"
placeholder="请输入城市名"
/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">归属</label>
<div class="layui-input-block">
<input
type="text"
class="layui-input"
id="area_ip_desc"
placeholder="请输入归属"
/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">拼音</label>
<div class="layui-input-block">
<input
type="text"
class="layui-input"
id="first_pinyin"
placeholder="请输入拼音"
/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">首字母</label>
<div class="layui-input-block">
<input
type="text"
class="layui-input"
id="pinyin"
placeholder="请输入首字母"
/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-block">
<select id="status">
<option value="1" selected>开启</option>
<option value="0">关闭</option>
</select>
</div>
</div>
</form>
</body>
</html>
- 提交数据
getChildFrame 获取 iframe 页的 DOM
yes: function (index, layero) {
var body = layer.getChildFrame("body", index);
var data = {
area_name: body.find("#area_name").val(),
area_ip_desc: body.find("#area_ip_desc").val(),
first_pinyin: body.find("#first_pinyin").val(),
pinyin: body.find("#pinyin").val(),
status: body.find("#status").val(),
};
$.post(
"http://admin.ouyangke.cn/index.php/api/Layui/city_add",
data,
function (res) {
if (res.code > 0) {
layer.msg("添加失败", { icon: 2 });
} else {
layer.msg("添加成功", { icon: 1 });
table.render({
toolbar: "#toolbar",
elem: "#demo",
url: "http://admin.ouyangke.cn/index.php/api/Layui/city",
where: {
id: 0,
status: 1,
},
cols: [
[
{ type: "radio" },
{ field: "area_id", title: "ID", width: 120 },
{ field: "area_name", title: "城市名" },
{
field: "area_ip_desc",
title: "归属",
align: "right",
},
{ field: "first_pinyin", title: "拼音" },
{ field: "pinyin", title: "首字母" },
{
field: "status",
title: "状态",
style: "color:red;",
},
],
],
page: true,
limit: 10,
limits: [10, 20, 30],
});
layer.close(index);
}
},
"json"
);
},
三、修改
case "update":
var data = table.checkStatus(obj.config.id).data;
if (!data[0]) {
layer.msg("请选择一条数据", { icon: 2 });
return false;
}
layer.full(
layer.open({
title: "修改",
type: 2,
content: "./edit.html",
maxmin: true,
btn: ["确定", "关闭"],
yes: function (index, layero) {
var body = layer.getChildFrame("body", index);
console.log(body.find("#area_name").val());
console.log(body.find("#area_ip_desc").val());
console.log(body.find("#first_pinyin").val());
console.log(body.find("#pinyin").val());
console.log(body.find("#status").val());
},
})
);
break;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" type="text/css" href="./layui/css/layui.css" />
<script type="text/javascript" src="./layui/layui.js"></script>
</head>
<body>
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">城市名</label>
<div class="layui-input-block">
<input
type="text"
class="layui-input"
id="area_name"
placeholder="请输入城市名"
/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">归属</label>
<div class="layui-input-block">
<input
type="text"
class="layui-input"
id="area_ip_desc"
placeholder="请输入归属"
/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">拼音</label>
<div class="layui-input-block">
<input
type="text"
class="layui-input"
id="first_pinyin"
placeholder="请输入拼音"
/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">首字母</label>
<div class="layui-input-block">
<input
type="text"
class="layui-input"
id="pinyin"
placeholder="请输入首字母"
/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-block">
<select id="status"></select>
</div>
</div>
</form>
</body>
</html>
success: function (layero, index) {
var body = layer.getChildFrame("body", index);
body.find("#area_name").val(data[0].area_name);
body.find("#area_ip_desc").val(data[0].area_ip_desc);
body.find("#first_pinyin").val(data[0].first_pinyin);
body.find("#pinyin").val(data[0].pinyin);
if (data[0].status == 1) {
var select =
'<option value="1" selected>开启</option><option value="0">关闭</option>';
} else {
var select =
'<option value="1">开启</option><option value="0" selected>关闭</option>';
}
body.find("#status").html(select);
var iframeWindow = layero.find("iframe")[0].contentWindow;
iframeWindow.layui.form.render();
},
四、修改表格数据
cols: [
[
{ type: "radio" },
{ field: "area_id", title: "ID", width: 120 },
{ field: "area_name", title: "城市名" },
{ field: "area_ip_desc", title: "归属", align: "right" },
{ field: "first_pinyin", title: "拼音" },
{ field: "pinyin", title: "首字母" },
{
field: "status",
title: "状态",
templet: function (res) {
if (res.status == 1) {
return '<span style="color:green;">开启</span>';
} else {
return '<span style="color:red;">禁用</span>';
}
},
},
],
],