Java之品优购课程讲义_day06(6)

4.1保存选中规格选项

我们需要将用户选中的选项保存在 tb_goods_desc 表的 specification_items 字段中,定义 json

格式如下:

1

[{“attributeName”:”规格名称”,”attributeValue”:[“规格选项 1,“规格选项 2”.... ]        } , ....        ]

(1)在 baseController.js 增加代码

//从集合中按照 key 查询对象

 

$scope.searchObjectByKey=function(list,key,keyValue){

 

for(var  i=0;i<list.length;i++){

 

if(list[i][key]==keyValue){

 

 

 

}

 

}

 

 

 

}


2)在 goodsController.js 增加代码

$scope.entity={  goodsDesc:{itemImages:[],specificationItems:[]}    };

 

 

 

$scope.updateSpecAttribute=function($event,name,value){

 

var  object=  $scope.searchObjectByKey(

 

$scope.entity.goodsDesc.specificationItems  ,'attributeName',  name);

 

if(object!=null){ if($event.target.checked  ){

object.attributeValue.push(value);

 

}else{//取消勾选

object.attributeValue.splice(  object.attributeValue.indexOf(value  )  ,1);//移除选

项

 

//如果选项都取消了,将此条记录移除

 

if(object.attributeValue.length==0){

 

$scope.entity.goodsDesc.specificationItems.splice(

 

$scope.entity.goodsDesc.specificationItems.indexOf(object),1);

 

}

 

}

 

}else{

 

$scope.entity.goodsDesc.specificationItems.push(

 

{"attributeName":name,"attributeValue":[value]});

 

}

 

}


3)在 goods_edit.html 调用方法

 

1

<div  ng-repeat="pojo  in  specList">

01

02

03

04

05

06

07

08

09

10

11

12

<div  class="col-md-2  title">{{pojo.text}}</div>

 

<div  class="col-md-10  data">

 

<span  ng-repeat="option  in  pojo.options">

 

<input    type="checkbox"

ng-click="updateSpecAttribute($event,pojo.text,option.optionName)">{{option.optionN ame}}

</span>

</div>

 

</div>

为了方便测试,我们可以在页面上某个区域临时添加表达式,以便观测测试结果

1

{{entity.goodsDesc.specificationItems}}

猜你喜欢

转载自blog.csdn.net/czbkzmj/article/details/81983536