①先写后端代码。
一、先写服务(service)层的接口(interface)层
BrandService.java 的内容为:
package com.pinyougou.sellergoods.service;
import java.util.List;
import com.pinyougou.pojo.TbBrand;
import entity.PageAndSize;
import entity.PageResult;
/**
* 品牌接口
* @author Administrator
*
*/
public interface BrandService {
public List<TbBrand> findAll();
/**
* 品牌分页
* @param pageAndSize
* @return
*/
public PageResult findPage(PageAndSize pageAndSize);
/**
* 增加
* @param brand
*/
public void add(TbBrand brand);
/**
* 根据ID查询实体
* @param id
* @return
*/
public TbBrand findOne(Long id);
/**
* 修改
* @param brand
*/
public void update(TbBrand brand);
/**
* 删除
* @param ids
*/
public void delete(List<Long> ids);
}
二、再写服务(service)层的实现(implements)层
BrandServiceImpl.java 的内容为:
package com.pinyougou.sellergoods.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
import entity.PageAndSize;
import entity.PageResult;
@Service
public class BrandServiceImpl implements BrandService {
@Autowired
private TbBrandMapper brandMapper;
@Override
public List<TbBrand> findAll() {
//返回全部列表
return brandMapper.selectByExample(null);
}
@Override
public PageResult findPage(PageAndSize pageAndSize) {
PageHelper.startPage(pageAndSize.getPageNum(), pageAndSize.getSizeNum());
Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(null);
return new PageResult(page.getTotal(),page.getResult());
}
@Override
public void add(TbBrand brand) {
brandMapper.insert(brand);
}
@Override
public TbBrand findOne(Long id) {
return brandMapper.selectByPrimaryKey(id);
}
@Override
public void update(TbBrand brand) {
brandMapper.updateByPrimaryKey(brand);
}
@Override
public void delete(List<Long> ids) {
for(Long id:ids) {
brandMapper.deleteByPrimaryKey(id);
}
}
}
三、写控制(controller)层代码即web层代码。
BrandController.java 的内容为:
package com.pinyougou.manager.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
import entity.IdResult;
import entity.PageAndSize;
import entity.PageResult;
import entity.Result;
@RestController
@RequestMapping("brand")
public class BrandController {
@Reference
private BrandService brandService;
@RequestMapping("/findAll")
public List<TbBrand> findAll(){
return brandService.findAll();
}
@RequestMapping("/findPage")
public PageResult findPage(@RequestBody PageAndSize pageAndSize) {
return brandService.findPage(pageAndSize);
}
@RequestMapping("/add")
public Result add(@RequestBody TbBrand brand) {
try {
brandService.add(brand);
return new Result(true,"增加成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"增加失败");
}
}
@RequestMapping("findOne")
public TbBrand findOne(@RequestBody IdResult idResult) {
return brandService.findOne(idResult.getId());
}
@RequestMapping("/update")
public Result update(@RequestBody TbBrand brand) {
try {
brandService.update(brand);
return new Result(true,"修改成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"修改失败");
}
}
@RequestMapping("/delete")
public Result delete(@RequestBody List<IdResult> idResults) {
List<Long> ids = new ArrayList<Long>();
int i=0;
if(idResults.size()>0) {
for(IdResult idResult:idResults) {
ids.add(idResult.getId());
i++;
}
}
try {
brandService.delete(ids);
return new Result(true,"删除成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"删除失败");
}
}
}
②再写前端代码。
一、先写更新勾选CheckBox复选框的ID的代码
<tbody>
<tr ng-repeat="entity in list">
<td><input type="checkbox" id="{{entity.id}}"
ng-click="updateSelectIds($event,entity.id)" /></td>
<td>{{entity.id}}</td>
<td>{{entity.name}}</td>
<td>{{entity.firstChar}}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" ng-click="findOne($event)">修改</button>
</td>
</tr>
</tbody>
$scope.selectIds = [];//用户勾选的ID集合
$scope.boxes = [];//保存复选框被选的状态,true(勾选)或者false(不勾选)
$scope.updateSelectIds = function($event,id){
if($event.target.checked){
$scope.selectIds.push(id);//push向集合添加元素
}else{
var index = $scope.selectIds.indexOf(id);//查找id值在集合中的位置
if(index != -1){
$scope.selectIds.splice(index,1);//从某个位置移掉几个元素
}
}
$scope.saveStatus();
}
//保存CheckBox复选框的true和false的状态
$scope.saveStatus = function(){
for(var j=0;j<$scope.list.length;j++){
for(var i=0;i< $scope.selectIds.length;i++){
//从第0位查找id为$scope.list[j].id的值,是否有这个id在$scope.selectIds里
if($scope.selectIds.indexOf($scope.list[j].id,0)!=-1){
$scope.boxes[$scope.list[j].id] = true;
}else{
$scope.boxes[$scope.list[j].id]= false;
}
}
}
}
二、再写删除按钮的代码
<div class="pull-left">
<div class="form-group form-inline">
<div class="btn-group">
<button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ng-click="entity={}" ><i class="fa fa-file-o"></i> 新建</button>
<button type="button" class="btn btn-default" title="删除" ng-click="dele()"><i class="fa fa-trash-o"></i> 删除</button>
<button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
</div>
</div>
</div>
$scope.dele = function(){
$scope.entity04=[];
for(var i=0;i<$scope.selectIds.length;i++){
$scope.entity04.push({id: $scope.selectIds[i]}) ;
}
$http.post("../brand/delete.do",$scope.entity04).success(
function(response){
for(var i=0;i<$scope.selectIds.length;i++){
//查找id值在集合中的位置
var index = $scope.selectIds.indexOf($scope.selectIds[i]);
if(index != -1){
$scope.selectIds.splice(index,1);//从某个位置移掉几个元素
}
}
if(response.success){
$scope.reloadList();
}else{
alert(response.message);
}
}
);
}
三、再写页面渲染完成后,复选框的显示,true为被勾选,false为不勾选。
<tbody>
<tr ng-repeat="entity in list" on-finish-render-filters>
<td><input type="checkbox" id="{{entity.id}}"
ng-click="updateSelectIds($event,entity.id)" /></td>
<td>{{entity.id}}</td>
<td>{{entity.name}}</td>
<td>{{entity.firstChar}}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" ng-click="findOne($event)">修改</button>
</td>
</tr>
</tbody>
on-finish-render-filters表示等待这个渲染完成以后,执行以下代码。
只为部分代码
<script type="text/javascript">
var app = angular.module('pinyougou',['pagination']);//在js里可以用单引也可以用双引
app.directive('onFinishRenderFilters', function ($timeout) {
return {
restrict: 'A',
link: function(scope, element, attr) {
if (scope.$last === true) {
$timeout(function() {
scope.$emit('ngRepeatFinished');
});
}
}
};
});
app.controller('brandController',function($scope,$http){
$scope.$on('ngRepeatFinished', function(){
//可执行DOM操作
for(var i=0;i< $scope.selectIds.length;i++){
for(var j=0;j<$scope.list.length;j++){
if( $scope.list[j].id == $scope.selectIds[i]){
//在渲染 完成后,对复选框的 显示进行更新,false表示不勾选,true表示勾选
document.getElementById(String($scope.list[j].id)).checked = $scope.boxes[$scope.list[j].id];
}
}}
});
//查询品牌列表数据
$scope.findAll = function(){
//之前的访问json的路径是http://localhost:9101/brand/findAll.do
//则现在,在brand.html文件../返回根目录,webapp根目录,再寻找/bran
最后:brand.html文件的内容为:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>品牌管理</title>
<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
<link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
<link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
<link rel="stylesheet" href="../css/style.css">
<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../plugins/angularjs/angular.min.js"></script>
<!-- 分页组件开始 -->
<script type="text/javascript" src="../plugins/angularjs/pagination.js"></script>
<link rel="stylesheet" href="../plugins/angularjs/pagination.css">
<!-- 分页组件结束 -->
<script type="text/javascript">
var app = angular.module('pinyougou',['pagination']);//在js里可以用单引也可以用双引
app.directive('onFinishRenderFilters', function ($timeout) {
return {
restrict: 'A',
link: function(scope, element, attr) {
if (scope.$last === true) {
$timeout(function() {
scope.$emit('ngRepeatFinished');
});
}
}
};
});
app.controller('brandController',function($scope,$http){
$scope.$on('ngRepeatFinished', function(){
//可执行DOM操作
for(var i=0;i< $scope.selectIds.length;i++){
for(var j=0;j<$scope.list.length;j++){
if( $scope.list[j].id == $scope.selectIds[i]){
//在渲染 完成后,对复选框的 显示进行更新,false表示不勾选,true表示勾选
document.getElementById(String($scope.list[j].id)).checked = $scope.boxes[$scope.list[j].id];
}
}}
});
//查询品牌列表数据
$scope.findAll = function(){
//之前的访问json的路径是http://localhost:9101/brand/findAll.do
//则现在,在brand.html文件../返回根目录,webapp根目录,再寻找/brand/findAll.do
$http.get('../brand/findAll.do').success(
function(response){
$scope.list = response;
}
);
}
$scope.paginationConf = {
//当前页
currentPage : 1,
//总记录数
totalItems : 10,
//每页记录数
itemsPerPage : 10,
//分页选项,下拉菜单
perPageOptions : [ 10, 20, 30, 40, 50 ],
//当页码重新变更后自动触发的方法
onChange : function() {
$scope.reloadList();
}};
//这个前端的方法名称findPage不一定要和后端的findPage方法名称一样,可以随意取
//只不过去一样的名称好辨别一些。
//分页
$scope.findPage = function(){
$scope.entity02 = {
pageNum: $scope.paginationConf.currentPage,
sizeNum: $scope.paginationConf.itemsPerPage
};
$http.post('../brand/findPage.do',$scope.entity02).success(
function(response){
//显示当前一页的[{},{},...{}]内容
$scope.list = response.rows;
//更改更新总记录数,更改此变量值,前端分页控件
//自动获得值
$scope.paginationConf.totalItems = response.total;
}
);
}
$scope.reloadList = function(){
$scope.findPage();
}
//保存
$scope.save = function(){
var methodName = 'add';
if($scope.entity.id != null){
methodName = 'update';
}
$http.post('../brand/'+methodName+'.do',$scope.entity).success(
function(response){
if(response.success){
$scope.reloadList();//刷新
}else{
alert(response.message);
}
}
);
}
$scope.findOne = function($event){
$scope.entity03= {id: $event.target.parentNode.parentNode.children[1].innerText}
$http.post("../brand/findOne.do",$scope.entity03).success(
function(response){
$scope.entity = response;
}
);
}
$scope.selectIds = [];//用户勾选的ID集合
$scope.boxes = [];//保存复选框被选的状态,true(勾选)或者false(不勾选)
$scope.updateSelectIds = function($event,id){
if($event.target.checked){
$scope.selectIds.push(id);//push向集合添加元素
}else{
var index = $scope.selectIds.indexOf(id);//查找id值在集合中的位置
if(index != -1){
$scope.selectIds.splice(index,1);//从某个位置移掉几个元素
}
}
$scope.saveStatus();
}
//保存CheckBox复选框的true和false的状态
$scope.saveStatus = function(){
for(var j=0;j<$scope.list.length;j++){
for(var i=0;i< $scope.selectIds.length;i++){
//从第0位查找id为$scope.list[j].id的值,是否有这个id在$scope.selectIds里
if($scope.selectIds.indexOf($scope.list[j].id,0)!=-1){
$scope.boxes[$scope.list[j].id] = true;
}else{
$scope.boxes[$scope.list[j].id]= false;
}
}
}
}
$scope.dele = function(){
$scope.entity04=[];
for(var i=0;i<$scope.selectIds.length;i++){
$scope.entity04.push({id: $scope.selectIds[i]}) ;
}
$http.post("../brand/delete.do",$scope.entity04).success(
function(response){
for(var i=0;i<$scope.selectIds.length;i++){
//查找id值在集合中的位置
var index = $scope.selectIds.indexOf($scope.selectIds[i]);
if(index != -1){
$scope.selectIds.splice(index,1);//从某个位置移掉几个元素
}
}
if(response.success){
$scope.reloadList();
}else{
alert(response.message);
}
}
);
}
});
</script>
</head>
<body class="hold-transition skin-red sidebar-mini" ng-app="pinyougou" ng-controller="brandController" >
<!-- .box-body -->
<div class="box-header with-border">
<h3 class="box-title">品牌管理</h3>
</div>
<div class="box-body">
<!-- 数据表格 -->
<div class="table-box">
<!--工具栏-->
<div class="pull-left">
<div class="form-group form-inline">
<div class="btn-group">
<button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ng-click="entity={}" ><i class="fa fa-file-o"></i> 新建</button>
<button type="button" class="btn btn-default" title="删除" ng-click="dele()"><i class="fa fa-trash-o"></i> 删除</button>
<button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
</div>
</div>
</div>
<div class="box-tools pull-right">
<div class="has-feedback">
</div>
</div>
<!--工具栏/-->
<!--数据列表-->
<table id="dataList" class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right:0px">
<input id="selall" type="checkbox" class="icheckbox_square-blue">
</th>
<th class="sorting_asc">品牌ID</th>
<th class="sorting">品牌名称</th>
<th class="sorting">品牌首字母</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="entity in list" on-finish-render-filters>
<td><input type="checkbox" id="{{entity.id}}"
ng-click="updateSelectIds($event,entity.id)" /></td>
<td>{{entity.id}}</td>
<td>{{entity.name}}</td>
<td>{{entity.firstChar}}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" ng-click="findOne($event)">修改</button>
</td>
</tr>
</tbody>
</table>
<!--数据列表/-->
</div>
<!-- 数据表格 /-->
<tm-pagination conf="paginationConf" ></tm-pagination>
</div>
<!-- /.box-body -->
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" >
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">品牌编辑</h3>
</div>
<div class="modal-body">
<table class="table table-bordered table-striped" width="800px">
<tr>
<td>品牌名称</td>
<td><input class="form-control" placeholder="品牌名称" ng-model="entity.name" > </td>
</tr>
<tr>
<td>首字母</td>
<td><input class="form-control" placeholder="首字母" ng-model="entity.firstChar"> </td>
</tr>
</table>
</div>
<div class="modal-footer">
<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" ng-click="save()">保存</button>
<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
</div>
</div>
</div>
</div>
</body>
</html>