①规格管理---增加规格、新增规格---增加行。
一、要想实现这个功能,就要新增一个方法。addTableRow(增加表格行)方法。
把这个表格,绑定一个集合类型(或者叫数组类型)的变量。点击增加时,实际上是增加集合(数组)的元素。
整个要提交的东西是entity,形式如下:
{specification:{ id: ,specName:" "},specificationOptionList:[ { } , { } , { } ...... ]}
//增加规格选项行
$scope.addTableRow = function(){
$scope.entity.specificationOptionList.push({});
}
二、在specification.html文件的表格绑定该$scope.entity.specificationOptionList变量
三个tr,删除两个,留下一个。再循环留下的一个。
其中ng-repeat="pojo in entity.specificationOptionList"的pojo应该和规格选项一致,即pojo项目里面的包com.pinyougou.pojo里面的TbSpecificationOption.java类里面的保持一致。而enity应该和TbSpecification.java类保持一致。
所以有了entity.id 和 entity.specName。
所以有了pojo.id、pojo.optionName、pojo.specId 和 pojo.orders。
<!-- 规格选项 -->
<div class="btn-group">
<button type="button" class="btn btn-default" title="新建"
ng-click="addTableRow()"><i class="fa fa-file-o"></i> 新增规格选项</button>
</div>
<table class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="sorting">规格选项</th>
<th class="sorting">排序</th>
<th class="sorting">操作</th>
</thead>
<tbody>
<tr ng-repeat="pojo in entity.specificationOptionList">
<td>
<input class="form-control" placeholder="规格选项" ng-model="pojo.optionName">
</td>
<td>
<input class="form-control" placeholder="排序" ng-model="pojo.orders">
</td>
<td>
<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
</td>
</tr>
</tbody>
</table>
只是这样,点击新增按钮运行的话,会报错。
说明specificationOptionList为空null
如果这样定义:
又会报错:
所以不能这么$scope.entity.specificationOptionList=[ ];写。
也可以不写全entity。因为specification不是一个集合(数组[ ])
在后面$scope.entity.specification这个值可以自动绑定的。所以这个地方entity可以不全写。
记住:如果是集合(数组[ ]),必须先初始化[ ] ,如果不是集合(数组[ ]),而是{ },就不用初始化。
三、几个重要文件的内容
specificationController.js的内容为:
//控制层
/**
app.controller('specificationController' ,function($scope,$controller ,specificationService){
$controller('baseController',{$scope:$scope});//继承
//读取列表数据绑定到表单中
$scope.findAll=function(){
specificationService.findAll().success(
function(response){
$scope.list=response;
}
);
}
//分页
$scope.findPage=function(page,rows){
specificationService.findPage(page,rows).success(
function(response){
$scope.list=response.rows;
$scope.paginationConf.totalItems=response.total;//更新总记录数
}
);
}
//查询实体
$scope.findOne=function(id){
specificationService.findOne(id).success(
function(response){
$scope.entity= response;
}
);
}
//保存
$scope.save=function(){
var serviceObject;//服务层对象
if($scope.entity.id!=null){//如果有ID
serviceObject=specificationService.update( $scope.entity ); //修改
}else{
serviceObject=specificationService.add( $scope.entity );//增加
}
serviceObject.success(
function(response){
if(response.success){
//重新查询
$scope.reloadList();//重新加载
}else{
alert(response.message);
}
}
);
}
//批量删除
$scope.dele=function(){
//获取选中的复选框
specificationService.dele( $scope.selectIds ).success(
function(response){
if(response.success){
$scope.reloadList();//刷新列表
}
}
);
}
$scope.searchEntity={};//定义搜索对象
//搜索
$scope.search=function(page,rows){
specificationService.search(page,rows,$scope.searchEntity).success(
function(response){
$scope.list=response.rows;
$scope.paginationConf.totalItems=response.total;//更新总记录数
}
);
}
});
**/
app.controller('specificationController',function($scope,$controller,specificationService){
$controller('baseController',{$scope:$scope});//控制器的继承
/**
$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
specificationService.findAll().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
};
specificationService.findPage($scope.entity02).success(
function(response){
//显示当前一页的[{},{},...{}]内容
$scope.list = response.rows;
//更改更新总记录数,更改此变量值,前端分页控件
//自动获得值
$scope.paginationConf.totalItems = response.total;
}
);
}
/**
$scope.reloadList = function(){
//$scope.findPage();
$scope.search();//否则又会还原成全部的数据呈现
}
**/
//保存
$scope.save = function(){
var object = null;
if($scope.entity.id != null){
object = specificationService.update($scope.entity);
}else{
object = specificationService.add($scope.entity);
}
object.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}
specificationService.findOne($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]}) ;
}
specificationService.dele($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);
}
}
);
}
/**
$scope.searchEntity = {};
**/
//条件查询
$scope.search = function(){
$scope.entity05 = {
pageNum: $scope.paginationConf.currentPage,
sizeNum: $scope.paginationConf.itemsPerPage,
brand: $scope.searchEntity
};
specificationService.search($scope.entity05).success(
function(response) {
$scope.list = response.rows;
$scope.paginationConf.totalItems = response.total;
}
)
}
//不能这么写,因为entity本身为null值空值。$scope.entity.specificationOptionList=[];
//可以写全entity
//$scope.entity={specification:{},specificationOptionList:[]};
//也可以不写全entity,也不会报错。因为specification不是集合(数组[])
$scope.entity={specificationOptionList:[]};
//增加规格选项行
$scope.addTableRow = function(){
$scope.entity.specificationOptionList.push({});
}
});
specification.html的内容为:
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<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" src="../js/base_pagination.js"></script>
<script type="text/javascript" src="../js/render_finish.js"></script>
<script type="text/javascript" src="../js/service/specificationService.js"></script>
<script type="text/javascript" src="../js/controller/baseController.js"></script>
<script type="text/javascript" src="../js/controller/specificationController.js"></script>
<!-- 删除以下内容
<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为模块对象,控制器有控制器的名字,服务也要有服务的名字。
//和控制器一样也是有function并且可以接受参数和内容。
//品牌服务
app.service("brandService",function($http){
//不能用$scope,要用this,这是他本身。
this.findAll = function(){
//.success后面的内容,$scope.list = response;给了视图
//与视图交互,又与后端交互
//.success后面的内容是控制层的内容。
//所以只写前面