本篇博客将要实现的效果,包括添加信息、修改信息、查询信息、删除信息、对信息排序/敏感字过滤!
与上一篇文章不同的是:优化了界面效果,实现敏感字过滤,bug修复
将要用到:HTML,CSS,AngularJS,jQuery
首先看一下主界面实现的代码(HTML文件):
<html ng-app="OrderApp">
<head>
<meta charset="UTF-8">
<title>订单管理title>
<script type="text/javascript" src="lib/jquery.1.12.4.js">script>
<script type="text/javascript" src="lib/angular-1.3.0.js">script>
<link type="text/css" href="css/style.css" rel="stylesheet"/>
<script type="text/javascript" src="js/Data.js">script>
<script type="text/javascript">
// 全选框/全不选框
$(function () {
$("input[name='check_all']").click(function () {
var checked = this.checked; // 获取 中checked属性的值
$("input[name='order_id[]']").each(function () {
this.checked = checked; // 依次设置每一个 中checked属性的值
});
});
});
// 定义angularJS的使用范围
var app = angular.module("OrderApp", []);
// 敏感词过滤
app.filter("sensitiveWord", function () {
return function (msg, flag) {
return msg.replace(flag, "*");
}
});
// 实现按钮与链接的功能
app.controller("OrderCtrl", function ($scope) {
// 获取到输入信息
$scope.data = data;
// 获取信息列表中的ID
var id = 12;
// 通过月份判断显示的信息
$scope.filterByMonth = function (order) {
// 判断‘是否有选择查询时间段的开始时间’条件是否成立,成立则向下继续判断
if ($scope.filter_begin_month == undefined || $scope.filter_begin_month == "") {
return true;
}
// 判断‘是否有选择查询时间段的结束时间’条件是否成立,成立则向下继续判断
if ($scope.filter_end_month == undefined || $scope.filter_end_month == "") {
return true;
}
// 获取到查询时间段的开始时间
var beginMonth = parseInt($scope.filter_begin_month);
// 获取到查询时间段的结束时间
var endMonth = parseInt($scope.filter_end_month);
// 判断‘开始时间’到‘结束时间’条件是否成立,成立则向下继续判断
if (beginMonth > endMonth) {
return true;
}
// 定义判断方法:没有规定时间段时,从信息列表‘第一条信息’到‘最后一条信息’为查询条件
var month = order.dtCreated.substr(0, order.dtCreated.indexOf("-"));
// 获取到符合条件的信息
month = parseInt(month);
// 将符合条件的信息输出到界面中
return (month >= beginMonth && month <= endMonth);
};
// 发货:点击‘发货’链接时,状态值改变为‘已发货’
$scope.deliver = function (id) {
for (var i in $scope.data) {
if ($scope.data[i].id == id) {
$scope.data[i].status = "已发货";
}
}
};
// 批量发货:在多选或全选状态下,点击‘批量发货’按钮时,选中的‘未发货’状态变为‘已发货’状态
$scope.batchDeliver = function () {
$("input[name='order_id[]']:checked").each(function () {
$scope.deliver(this.id);
});
};
// 删除:点击‘删除’链接时,将信息从界面中移除
$scope.remove = function (id) {
for (var i in $scope.data) {
if ($scope.data[i].id == id) {
$scope.data.splice(i, 1);
}
}
};
// 批量删除:在多选或全选状态下,点击‘批量删除’按钮时,选中的信息都会从列表中移除
$scope.batchRemove = function () {
$("input[name='order_id[]']:checked").each(function () {
$scope.remove(this.id);
});
};
// 在主界面打开时,隐藏‘新增订单表单’
$scope.isShowAddOrderForm = false;
// 点击‘新增订单’按钮时,显示新增订单表单
$scope.showAddOrderForm = function () {
$scope.isShowAddOrderForm = true;
};
// 创建一个空集合,用于接收‘新增订单’表单中的信息
$scope.errTips = [];
// 点击‘提交’按钮时,将‘新增订单表单’中的输入内容添加到信息列表中
$scope.submitAddOrderForm = function () {
// 获取到空集合
$scope.errTips = [];
// 为集合定义接收信息的标识符
$scope.goodsNameClassName = "";
$scope.userNameClassName = "";
$scope.phoneClassName = "";
$scope.priceClassName = "";
// 输入信息有误时,hasErr状态值为false
$scope.hasErr = false;
// 获取到输入的信息
var goodsName = $scope.goodsName == undefined ? "" : $scope.goodsName.trim();
var userName = $scope.userName == undefined ? "" : $scope.userName.trim();
var phone = $scope.phone == undefined ? "" : $scope.phone.trim();
var price = $scope.price == undefined ? "" : $scope.price.trim();
// 对输入的信息进行判断 - 输入无误时,hasErr状态值变为true
// 判断商品名
if (goodsName == "") {
$scope.errTips.push("商品名不能为空!");
$scope.goodsNameClassName = "formErr";
$scope.hasErr = true;
}
// 判断用户名
if (userName == "") {
$scope.errTips.push("用户名不能为空!");
$scope.userNameClassName = "formErr";
$scope.hasErr = true;
}
// 判断手机号
if (phone == "") {
$scope.errTips.push("手机号不能为空!");
$scope.phoneClassName = "formErr";
$scope.hasErr = true;
}
// 判断价格
if (price == "") {
$scope.errTips.push("价格不能为空!");
$scope.priceClassName = "formErr";
$scope.hasErr = true;
}
// 判断城市
if ($scope.city == undefined || $scope.city == "") {
$scope.errTips.push("请选择城市!");
$scope.hasErr = true;
}
// 如果商品名输入有误时,提示以下信息
if (goodsName.length < 6 || goodsName.length > 20) {
$scope.errTips.push("商品名必须是6-20个字符!");
$scope.goodsNameClassName = "formErr";
$scope.hasErr = true;
}
// 如果用户名输入有误时,提示以下信息
if (userName.length < 4 || userName.length > 16) {
$scope.errTips.push("用户名必须是4-16个字符!");
$scope.userNameClassName = "formErr";
$scope.hasErr = true;
}
// 如果手机号输入有误时,提示以下信息
if (!/^\d{11}$/.test(phone)) {
$scope.errTips.push("手机号格式不正确!");
$scope.phoneClassName = "formErr";
$scope.hasErr = true;
}
// 如果价格输入有误时,提示以下信息
price = parseFloat(price);
if (isNaN(price) || price <= 0) {
$scope.errTips.push("价格必须大于0!");
$scope.priceClassName = "formErr";
$scope.hasErr = true;
}
// 如果输入无误时,提示以下信息
if ($scope.hasErr) {
return; // 提示为空:不提示信息
}
// 将输入信息添加到信息列表中
$scope.data.push({
id: ++id,
goodsName: goodsName,
userName: userName,
phone: phone,
price: price,
city: $scope.city,
dtCreated: "10-25 10:00",
status: "待发货"
});
// 点击提交时,将输入的内容清空
$scope.goodsName = "";
$scope.userName = "";
$scope.phone = "";
$scope.price = "";
$scope.city = "";
$scope.isShowAddOrderForm = false;
};
// 点击‘修改’链接时,显示修改订单表单
$scope.edit = function (id) {
for (var i in $scope.data) {
if ($scope.data[i].id == id) {
$scope.edit_goodsName = $scope.data[i].goodsName;
$scope.edit_userName = $scope.data[i].userName;
$scope.edit_phone = $scope.data[i].phone;
$scope.edit_price = $scope.data[i].price;
$scope.edit_city = $scope.data[i].city;
$scope.edit_id = $scope.data[i].id;
}
}
$scope.isShowEditOrderForm = true;
};
// 提交修改订单表单
$scope.submitEditOrderForm = function () {
// 获取到空集合
$scope.errTips = [];
// 为集合定义接收信息的标识符
$scope.edit_userNameClassName = "";
$scope.edit_phoneClassName = "";
$scope.edit_priceClassName = "";
// 输入信息有误时,hasErr状态值为false
$scope.hasErr = false;
// 获取到输入的信息
var id = $scope.edit_id == undefined ? "" : parseInt($scope.edit_id);
var userName = $scope.edit_userName == undefined ? "" : $scope.edit_userName.trim();
var phone = $scope.edit_phone == undefined ? "" : $scope.edit_phone.trim();
var price = $scope.edit_price == undefined ? "" : $scope.edit_price;
// 对输入的信息进行判断 - 输入无误时,hasErr状态值变为true
// 判断用户名
if (userName == "") {
$scope.errTips.push("用户名不能为空!");
$scope.userNameClassName = "formErr";
$scope.hasErr = true;
}
// 判断手机号
if (phone == "") {
$scope.errTips.push("手机号不能为空!");
$scope.phoneClassName = "formErr";
$scope.hasErr = true;
}
// 判断价格
if (price == "") {
$scope.errTips.push("价格不能为空!");
$scope.priceClassName = "formErr";
$scope.hasErr = true;
}
// 判断城市
if ($scope.edit_city == undefined || $scope.edit_city == "") {
$scope.errTips.push("请选择城市!");
$scope.hasErr = true;
}
// 如果用户名输入有误时,提示以下信息
if (userName.length < 4 || userName.length > 16) {
$scope.errTips.push("用户名必须是4-16个字符!");
$scope.userNameClassName = "formErr";
$scope.hasErr = true;
}
// 如果手机号输入有误时,提示以下信息
if (!/^\d{11}$/.test(phone)) {
$scope.errTips.push("手机号格式不正确!");
$scope.phoneClassName = "formErr";
$scope.hasErr = true;
}
// 如果价格输入有误时,提示以下信息
price = parseFloat(price);
if (isNaN(price) || price <= 0) {
$scope.errTips.push("价格必须大于0!");
$scope.priceClassName = "formErr";
$scope.hasErr = true;
}
// 如果输入无误时,提示以下信息
if ($scope.hasErr) {
return; // 提示为空:不提示信息
}
// 将修改后的信息添加到信息列表中
for (var i in $scope.data) {
if ($scope.data[i].id == id) {
$scope.data[i].userName = userName;
$scope.data[i].phone = phone;
$scope.data[i].price = price;
$scope.data[i].city = $scope.edit_city;
}
}
// 点击提交时,将输入的内容清空
$scope.edit_goodsName = "";
$scope.edit_userName = "";
$scope.edit_phone = "";
$scope.edit_price = "";
$scope.edit_city = "";
$scope.isShowEditOrderForm = false;
};
});
script>
head>
<body ng-controller="OrderCtrl">
<div class="filter">
<input type="text" placeholder="用户名搜索" ng-model="filter_user_name"/>
<input type="text" placeholder="手机号搜索" ng-model="filter_phone"/>
<select class="choose_city" ng-model="filter_city">
<option value="">选择城市option>
<option value="北京">北京option>
<option value="上海">上海option>
<option value="天津">天津option>
<option value="重庆">重庆option>
select>
<select class="choose_status" ng-model="filter_status">
<option value="">选择状态option>
<option value="待发货">待发货option>
<option value="已发货">已发货option>
<option value="已收货">已收货option>
select>
<select class="choose_time_begin_month" ng-model="filter_begin_month">
<option value="">开始月份option>
<option value="1">01option>
<option value="2">02option>
<option value="3">03option>
<option value="4">04option>
<option value="5">05option>
<option value="6">06option>
<option value="7">07option>
<option value="8">08option>
<option value="9">09option>
<option value="10">10option>
<option value="11">11option>
<option value="12">12option>
select> -
<select class="choose_time_end_month" ng-model="filter_end_month">
<option value="">结束月份option>
<option value="1">01option>
<option value="2">02option>
<option value="3">03option>
<option value="4">04option>
<option value="5">05option>
<option value="6">06option>
<option value="7">07option>
<option value="8">08option>
<option value="9">09option>
<option value="10">10option>
<option value="11">11option>
<option value="12">12option>
select>
<select class="id_order" ng-model="id_order_type">
<option value="">ID排序option>
<option value="id">ID正序option>
<option value="-id">ID倒序option>
select>
div>
<div class="buttons">
<button class="add_btn" ng-click="showAddOrderForm();">新增订单button>
<button class="deliver_btn" ng-click="batchDeliver()">批量发货button>
<button class="remove_btn" ng-click="batchRemove()">批量删除button>
敏感字:米(商品名)-> 替换成 *
div>
<div class="list">
<table width="800px" cellspacing="0" rules="cols" border="1px">
<thead>
<tr>
<th width="4%">
<input type="checkbox" name="check_all"/>
th>
<th width="6%">IDth>
<th width="14%">商品名th>
<th width="8%">用户名th>
<th width="14%">手机号th>
<th width="10%">价格th>
<th width="8%">城市th>
<th width="12%">下单时间th>
<th width="8%">状态th>
<th width="14%">操作th>
tr>
thead>
<tbody>
<tr align="center"
ng-repeat="order in data | filter: {userName: filter_user_name} | filter: {phone: filter_phone} | filter: {city: filter_city} | filter: {status: filter_status} | filter: filterByMonth | orderBy: id_order_type">
<td>
<input type="checkbox" name="order_id[]" id="{{ order.id }}"/>
td>
<td>{{ order.id }}td>
<td>{{ order.goodsName | sensitiveWord: "米"}}td>
<td>{{ order.userName }}td>
<td>{{ order.phone }}td>
<td>{{ order.price | currency: "¥"}}td>
<td>{{ order.city }}td>
<td>{{ order.dtCreated }}td>
<td>
<span ng-show="order.status=='待发货'" ng-click="deliver(order.id)">
<a href="javascript: void(0);">发货a>
span>
<span ng-show="order.status=='已发货'">已发货span>
<span ng-show="order.status=='已收货'">已收货span>
td>
<td>
<a href="javascript: void(0);" ng-click="edit(order.id)">修改a>
<a href="javascript: void(0);" ng-click="remove(order.id)">删除a>
td>
tr>
tbody>
table>
div>
<div class="form" ng-show="isShowAddOrderForm">
<div>
<span class="label">新增订单span>
<span class="txt">span>
div>
<div>
<span class="label">商品名span>
<span class="txt">
<input type="text" placeholder="6-20个字符" ng-model="goodsName" ng-class="goodsNameClassName"/>
span>
div>
<div>
<span class="label">用户名span>
<span class="txt">
<input type="text" placeholder="4-16个字符" ng-model="userName" ng-class="userNameClassName"/>
span>
div>
<div>
<span class="label">手机号span>
<span class="txt"><input type="text" ng-model="phone" ng-class="phoneClassName"/>span>
div>
<div>
<span class="label">价格span>
<span class="txt"><input type="text" ng-model="price" ng-class="priceClassName"/>span>
div>
<div>
<span class="label">城市span>
<span class="txt">
<select ng-model="city">
<option value="">选择城市option>
<option value="北京">北京option>
<option value="上海">上海option>
<option value="天津">天津option>
<option value="重庆">重庆option>
select>
span>
div>
<div class="errTips" ng-show="hasErr">
<ul>
<li ng-repeat="msg in errTips">{{ msg }}li>
ul>
div>
<div>
<span class="label">span>
<span class="txt"><button ng-click="submitAddOrderForm()">提交button>span>
div>
<div style="clear: both">div>
div>
<div class="form" ng-show="isShowEditOrderForm">
<div>
<span class="label">修改订单span>
<span class="txt">span>
div>
<div>
<span class="label">商品名(只读)span>
<span class="txt">
<input type="text" placeholder="6-20个字符" ng-model="edit_goodsName" readonly/>
span>
div>
<div>
<span class="label">用户名span>
<span class="txt">
<input type="text" placeholder="4-16个字符" ng-model="edit_userName" ng-class="edit_userNameClassName"/>
span>
div>
<div>
<span class="label">手机号span>
<span class="txt"><input type="text" ng-model="edit_phone" ng-class="edit_phoneClassName"/>span>
div>
<div>
<span class="label">价格span>
<span class="txt"><input type="text" ng-model="edit_price" ng-class="edit_priceClassName"/>span>
div>
<div>
<span class="label">城市span>
<span class="txt">
<select ng-model="edit_city">
<option value="">选择城市option>
<option value="北京">北京option>
<option value="上海">上海option>
<option value="天津">天津option>
<option value="重庆">重庆option>
select>
span>
div>
<div class="errTips" ng-show="hasErr">
<ul>
<li ng-repeat="msg in errTips">{{ msg }}li>
ul>
div>
<div>
<span class="label">span>
<input type="hidden" ng-model="edit_id"/>
<span class="txt"><button ng-click="submitEditOrderForm()">提交button>span>
div>
<div style="clear: both">div>
div>
body>
html>
为了给信息列表实现数据,要获取到数据文件(Data.js):
var data = [
{
id: 1,
goodsName: "iPhone 8 Plus",
userName: "曹操",
phone: "15111111111",
price: 7588.00,
city: "北京",
dtCreated: "09-04 10:00",
status: "已发货"
},
{
id: 2,
goodsName: "华为 畅享6S",
userName: "刘备",
phone: "15222222222",
price: 899.00,
city: "天津",
dtCreated: "08-09 10:00",
status: "已发货"
},
{
id: 3,
goodsName: "努比亚Z17",
userName: "孙权",
phone: "15333333333",
price: 2099.00,
city: "上海",
dtCreated: "07-01 10:00",
status: "待发货"
},
{
id: 4,
goodsName: "三星 Galaxy S7",
userName: "司马懿",
phone: "15444444444",
price: 2999.00,
city: "北京",
dtCreated: "06-02 10:00",
status: "已发货"
},
{
id: 5,
goodsName: "魅蓝5S",
userName: "夏侯淳",
phone: "15555555555",
price: 999.00,
city: "北京",
dtCreated: "10-03 10:00",
status: "已发货"
},
{
id: 6,
goodsName: "三星 Galaxy Note8",
userName: "张辽",
phone: "15666666666",
price: 2599.00,
city: "北京",
dtCreated: "04-02 10:00",
status: "已收货"
},
{
id: 7,
goodsName: "OPPO R9sk",
userName: "关羽",
phone: "15777777777",
price: 4999.00,
city: "北京",
dtCreated: "03-09 10:00",
status: "已发货"
},
{
id: 8,
goodsName: "红米Note4X",
userName: "张飞",
phone: "15888888888",
price: 999.00,
city: "上海",
dtCreated: "05-18 10:00",
status: "已收货"
},
{
id: 9,
goodsName: "红米5A",
userName: "周瑜",
phone: "15999999999",
price: 599.00,
city: "重庆",
dtCreated: "06-16 10:00",
status: "待发货"
},
{
id: 10,
goodsName: "小米Mix2",
userName: "黄盖",
phone: "13111111111",
price: 3299.00,
city: "北京",
dtCreated: "03-15 10:00",
status: "待发货"
},
{
id: 11,
goodsName: "小米Note5",
userName: "黄忠",
phone: "13222222222",
price: 699.00,
city: "重庆",
dtCreated: "02-28 10:00",
status: "待发货"
},
{
id: 12,
goodsName: "VIVO X20",
userName: "赵云",
phone: "13333333333",
price: 2998.00,
city: "上海",
dtCreated: "08-22 10:00",
status: "已发货"
}
];
最后进行界面优化(style.css):
/*全局标识符{字体大小,外边框,内边框}*/
* {
font-size: 14px;
margin: 0;
padding: 0;
}
/*body标签标识符{内边框:上下 左右}*/
body {
padding: 16px 32px;
}
/*导航条{相对定位,宽,高,居中}*/
.filter {
position: relative;
width: 800px;
height: 40px;
margin: 0 auto;
}
/*导航条 -> input{宽,高,边框,圆角,左内边距} */
.filter input {
width: 152px;
height: 24px;
border: 1px solid #999;
border-radius: 4px;
padding-left: 8px;
}
/*导航条 -> ‘选择城市’下拉列表{宽,高,边框,圆角}*/
.filter select {
width: 86px;
height: 24px;
border: 1px solid #999;
border-radius: 4px;
}
/*功能按钮实现{宽,高,居中}*/
.buttons {
width: 800px;
height: 40px;
margin: 0 auto;
}
/*功能按钮实现 -> 按钮{宽,高,背景色,边框,圆角,颜色}*/
.buttons button {
width: 80px;
height: 24px;
background-color: green;
border: 0;
border-radius: 4px;
color: white;
}
/*功能按钮实现 -> ‘批量删除’按钮{背景色}*/
.buttons .remove_btn {
background-color: red;
}
/*表格信息实现{宽,居中}*/
.list {
width: 800px;
margin: 0 auto;
}
/*表格信息实现 -> 表格主体实现{高}*/
.list table tr {
height: 32px;
}
/*表格信息实现 -> 表格头部实现{背景色}*/
.list thead tr {
background-color: #777;
}
/*表格信息实现 -> 表格主体实现 -> 奇数行{背景色}*/
.list tbody tr:nth-child(odd) {
background-color: #ccc;
}
/*表格信息实现 -> 表格主体实现 -> 偶数行{背景色}*/
.list tbody tr:nth-child(even) {
background-color: #fff;
}
/*新增订单功能界面的按钮{宽,高,行高,背景色,边框,圆角,颜色,字体大小}*/
.list button {
width: 40px;
height: 18px;
line-height: 18px;
background-color: green;
border: 0;
border-radius: 4px;
color: white;
font-size: 8px;
}
/*新增订单功能界面{宽,边框,居中}*/
.form {
width: 460px;
border: 1px solid #999;
margin: 0 auto;
}
/*新增订单功能界面 -> 每一行的div样式{颜色}*/
.form div {
clear: both;
}
/*判断商品名有误时,边框颜色改变{红色}*/
.form .formErr {
border: 1px solid red;
}
/*新增订单功能界面提示字体样式{清除块级样式,浮动,宽,高,行高,字体下划线}*/
.form .label {
display: block;
float: left;
width: 80px;
height: 40px;
line-height: 40px;
text-align: end;
}
/*新增订单功能界面包含输入框的div样式{清除快级样式,浮动,宽,高,行高,左内边距}*/
.form .txt {
display: block;
float: left;
width: 340px;
height: 40px;
line-height: 40px;
padding-left: 16px;
}
/*新增订单功能界面输入框的样式{宽,高,边框,圆角,左内边距}*/
.form input {
width: 312px;
height: 24px;
border: 1px solid #999;
border-radius: 4px;
padding-left: 8px;
}
/*新增订单功能界面城市下拉列表样式{宽,高,边框,圆角}*/
.form select {
width: 86px;
height: 24px;
border: 1px solid #999;
border-radius: 4px;
}
/*新增订单功能界面提交按钮样式{宽,高,背景色,边框,圆角,颜色}*/
.form button {
width: 56px;
height: 24px;
background-color: green;
border: 0;
border-radius: 4px;
color: white;
}
/*‘输入信息有误时,将提示信息添加到msg模块中’msg模板样式{
宽,背景色,字体颜色,圆角,左外边框,上外边框,下外边框,内边框:上下 左右
}*/
.form .errTips {
width: 226px;
background-color: lightpink;
color: darkred;
border-radius: 4px;
margin-left: 96px;
margin-top: 6px;
margin-bottom: 4px;
padding: 16px 48px;
}
以上就是实现所有效果的全部代码了,希望对大家有所帮助!