在选择属性阵列的打印元件 [英] Print Elements of Array in Select Attribute
问题描述
我有批量[250克,500克,1KG,2个,4个来自服务器端过来的数组。我想显示此数组中的JSP中选择属性。我怎样才能做到这一点使用angularjs?
内阵列的元件可以根据从服务器侧里的值而变化。
嗯,这是我认为是从服务器端获取完整的数组:
[{价格:40,名:苹果,PrimaryKey的:21701,量:[250克,500克,1KG,2片,4片]},{价格:0,名:香蕉,PrimaryKey的:21705,量:[250克,500克,王]},{价格:0,名:石榴 的PrimaryKey:21709,量:[250克,500克]},{价格:0,名:橙色,PrimaryKey的:21713,量:[250克,500克,1KG,2KG]}]
现在,我要的是显示在选择属性数量的一部分。
你要这样呢?
编辑:请记住,这更多的是一个黑客不是一个真正的解决方案。一个真正的解决办法是从服务器端应有的价值。如果您没有访问到服务器端,然后这应该去做就好了。
VAR应用= angular.module('app的',[]);\r
app.controller('myctrlR',函数($范围){\r
$ scope.SelectedItem = [];\r
$ scope.items = [{\r
价格:40,\r
名:苹果\r
的PrimaryKey:21701,\r
量:[250克,500克,1KG,2片,4片]\r
},{\r
价格:0,\r
名:香蕉,\r
的PrimaryKey:21705,\r
量:[250克,500克,王]\r
},{\r
价格:0,\r
名:石榴,\r
的PrimaryKey:21709,\r
量:[250克,500克]\r
},{\r
价格:0,\r
名:橙色,\r
的PrimaryKey:21713,\r
量:[250克,500克,1KG,2KG]\r
}]\r
\r
angular.forEach($ scope.items,功能(VAL,键){\r
val.quantity = val.quantity.replace('[','').replace(']','').split(,)\r
})\r
})
\r
< HTML NG-应用=app的>\r
&所述; SCRIPT SRC =https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js>\r
< / SCRIPT>\r
< DIV NG控制器=myctrlR>\r
< DIV NG重复=项中的项目的风格=保证金底:20像素; NG-的init =的SelectedItem [$指数] =''>\r
< DIV> {{item.name}} - 价格:$ {{item.price}}< / DIV>\r
<选择NG模型=的SelectedItem [$指数]NG选项=X用X在item.quantity轨道X>\r
<期权价值=>选择项目< /选项>\r
< /选择>\r
< DIV>选定的项目:其中;强> {{的SelectedItem [$指数]}}< / STRONG>\r
< / DIV>\r
< / DIV>\r
< / DIV>\r
\r
< / HTML>
\r
I am having an array of quantities [250 Gram, 500 Gram, 1KG, 2 pieces, 4 pieces] coming from server side. I want to display this array in jsp in select attribute. How can I do this using angularjs ?
The elements inside array may vary depending on the values coming from server side.
Well this is my complete array that am getting from serverside:
[{"price":40,"name":"Apple","primaryKey":21701,"quantity":"[250 Gram, 500 Gram, 1KG, 2 pieces, 4 pieces]"},{"price":0,"name":"Banana","primaryKey":21705,"quantity":"[250 Gram, 500 Gram, 1KG]"},{"price":0,"name":"Pomegranate","primaryKey":21709,"quantity":"[250 Gram, 500 Gram]"},{"price":0,"name":"Orange","primaryKey":21713,"quantity":"[250 Gram, 500 Gram, 1KG, 2KG]"}]
Now, what I want is to display the quantity part in select attribute.
Did you want something like this?
Edited: Please keep in mind that this is more of a hack than a real solution. A real solution would be to get the proper value from server side. If you don't have access to the server side then this should do just fine.
var app = angular.module('theApp', []);
app.controller('myctrlR', function($scope) {
$scope.SelectedItem = [];
$scope.items = [{
"price": 40,
"name": "Apple",
"primaryKey": 21701,
"quantity": "[250 Gram, 500 Gram, 1KG, 2 pieces, 4 pieces]"
}, {
"price": 0,
"name": "Banana",
"primaryKey": 21705,
"quantity": "[250 Gram, 500 Gram, 1KG]"
}, {
"price": 0,
"name": "Pomegranate",
"primaryKey": 21709,
"quantity": "[250 Gram, 500 Gram]"
}, {
"price": 0,
"name": "Orange",
"primaryKey": 21713,
"quantity": "[250 Gram, 500 Gram, 1KG, 2KG]"
}]
angular.forEach($scope.items, function(val, key) {
val.quantity = val.quantity.replace('[', '').replace(']', '').split(', ')
})
})
<html ng-app="theApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js">
</script>
<div ng-controller="myctrlR">
<div ng-repeat="item in items" style="margin-bottom:20px;" ng-init="SelectedItem[$index]=''">
<div>{{item.name}} - Price: ${{item.price}}</div>
<select ng-model="SelectedItem[$index]" ng-options="x for x in item.quantity track by x">
<option value="">Select Item</option>
</select>
<div>Selected Item is: <strong>{{SelectedItem[$index]}}</strong>
</div>
</div>
</div>
</html>
这篇关于在选择属性阵列的打印元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!