1. 程式人生 > >前臺遍歷陣列,後臺foreach迴圈新增

前臺遍歷陣列,後臺foreach迴圈新增

前臺實現將複選框的值儲存到陣列中,後臺用list進行傳值,mybatis中用foreach遍歷迴圈新增或更新.

前臺jsp程式碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jquery-easyui-1.5.1/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jquery-easyui-1.5.1/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jquery-easyui-1.5.1/demo.css">
<script type="text/javascript" src="<%=request.getContextPath() %>/jquery-easyui-1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<title>安馨之家</title>
<script>
$(function(){
	
	/* //載入資料庫的大禮包資訊
	$.ajax({
		
	}) */
	
	//表單提交
	$("#commit").click(function(){
		//alert("方法呼叫")
		//將資料收集到陣列中
		var codes = new Array();
		$("input[name='code']").each(function(){
			codes.push($(this).val());
		})
		var names = new Array();
		$("input[name='name']").each(function(){
			names.push($(this).val());
		})
		var counts = new Array();
		$("input[name='count']").each(function(){
			counts.push($(this).val());
		})
		var prices = new Array();
		$("input[name='price']").each(function(){
			prices.push($(this).val());
		})
		var reasons = new Array();
		$("input[name='reason']").each(function(){
			reasons.push($(this).val());
		})
		
		
		//將所有資料放入整個陣列
		var list = new Array();//存放物件的陣列
		for(var i=0;i<codes.length;i++){
			/* alert("codes[i]"+codes[i]); */
//過濾條件根據需求自行修改
			if(codes[i]!=""){ //為填資料為空字串,此處過濾條件開啟後,可實現只填入有用資料,不會將所有資料都儲存
				var prize = {};//注意:此處的物件是java實體類要有的物件
				prize.cus_code = $("#cus_code").val();
				prize.addr_code = $("#addr_code").val();
				prize.name = names[i];
				prize.count = counts[i];
				prize.price = prices[i];
				prize.reason = reasons[i];
				
				list.push(prize);
			}
			
		}
將陣列轉換為json格式
		var plist = JSON.stringify(list); 
		
		$.ajax({
			url:"<%=request.getContextPath() %>/advisory/makePrize.action",
			type:"post",
			dataType:"json",
			data:{
				"plist":plist
			},
			success:function(data){
				if(data){
					alert("大禮包錄入成功!!!");
					location.href="<%=request.getContextPath() %>/work/toWork.action?cus_code="+$("#cus_code").val();
					
				}else{
					alert("大禮包錄入失敗!!!");
				}
			}
		})
	})
	
	//清空表單
	$("#clear").click(function(){
		$('#prize').form('clear');
	})
	
	//返回
	$("#back").click(function(){
		location.href="<%=request.getContextPath() %>/work/toWork.action?cus_code="+$("#cus_code").val();
	})			
	
})
		
</script>
</head>
<body border="1" width="1000px" height="500px">
	<div>
			<form id="prize" method="post">
				<div>
					<table border="1" width="100%" height="500px">
						<tr align="center"  height="50px">
							<th colspan="5"><h4 align="center">適老化大禮包配置建議</h4></th>
						</tr>
						<tr align="center"  height="50px">
							<th>客戶地址資訊:</th>
							<th colspan="4">
								<h4 align="center"><span>${address }</span></h4>
								<input type="hidden" id="cus_code" value="${cus_code }">	
								<input type="hidden" id="addr_code" value="${addr_code }">	
							</th>
						</tr>
						<tr align="center"  height="50px">
							<th>序號</th>
							<th>用品名稱</th>
							<th>數量</th>
							<th>公司零售價</th>
							<th>配置理由</th>
						</tr>
						<tr>
							<th><input type="text" name="code"></th>
							<th><input type="text" name="name"></th>
							<th><input type="text" name="count"></th>
							<th><input type="text" name="price"></th>
							<th><input type="text" name="reason"></th>
						</tr>
						<tr>
							<th><input type="text" name="code"></th>
							<th><input type="text" name="name"></th>
							<th><input type="text" name="count"></th>
							<th><input type="text" name="price"></th>
							<th><input type="text" name="reason"></th>
						</tr>
						<tr>
							<th><input type="text" name="code"></th>
							<th><input type="text" name="name"></th>
							<th><input type="text" name="count"></th>
							<th><input type="text" name="price"></th>
							<th><input type="text" name="reason"></th>
						</tr>
						<tr>
							<th><input type="text" name="code"></th>
							<th><input type="text" name="name"></th>
							<th><input type="text" name="count"></th>
							<th><input type="text" name="price"></th>
							<th><input type="text" name="reason"></th>
						</tr>
						<tr>
							<th><input type="text" name="code"></th>
							<th><input type="text" name="name"></th>
							<th><input type="text" name="count"></th>
							<th><input type="text" name="price"></th>
							<th><input type="text" name="reason"></th>
						</tr>
						<tr>
							<th colspan="5" height="50px">大禮包總金額為:    <input type="text" id="subtotal">元</th>
						</tr>
						<tr>
							<th colspan="5" height="50px">
								<input type="button" value="儲存" id="commit">    
								<input type="button" value="返回" id="back">
								<input type="button" value="清空" id="clear">
							</th>
						</tr>
					</table>
				</div>
			</form>
	</div>
</body>
</html>

後臺java程式碼:
/**
	 * 功能:配置大禮包
	 * */
	@RequestMapping("makePrize")
	@ResponseBody
	public boolean makePrize(String plist){
//此處需要將格式在轉換回來
		ObjectMapper objectMapper = new ObjectMapper();

		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Prize.class);
		List<Prize> list = null;
		try {
			list = objectMapper.readValue(plist, javaType);
//此處開始,MVC層只需要傳list集合即可
			int i = makeAdvisoryService.addPrize(list);
			if(i>0){
				return true;
			}else{
				return false;
			}
		} catch (IOException e) {
			
			e.printStackTrace();
			return false;
		}
	}
mybatis.xml的對映檔案(具體的xml檔名自定義)
<insert id="addPrize" parameterType="java.util.List">
		insert into prize (cus_code,addr_code,name,count,price,reason) values
		<foreach collection="list" item="p" separator="," index="index">
		 (	#{p.cus_code},
			#{p.addr_code},
			#{p.name},
			#{p.count}, 
			#{p.price}, 
			#{p.reason} 
		)
		</foreach>
	</insert>



相關推薦

前臺陣列,後臺foreach迴圈新增

前臺實現將複選框的值儲存到陣列中,後臺用list進行傳值,mybatis中用foreach遍歷迴圈新增或更新. 前臺jsp程式碼: <%@ page language="java" contentType="text/html; charset=UTF-8"

smarty 陣列foreach,section例項

1、一維陣列 array ( "0" => 'home', '1' => 'who', '2'=> 'tank',

forEach和each陣列的使用方式,一般用於接收到的後臺資料

// foreach遍歷陣列 var arr = [{name:"limeng",email:"xfjylimeng"},{name:"hehe",email:"xfjylimeng"}]; arr.forEach(myfun) function myfun(element,in

for迴圈foreach迴圈陣列的區別

在遍歷陣列的時候,for迴圈能夠在遍歷過程中修改陣列中元素,而foreach不行 例如(在main方法中):int[] array=new int[3];                    

foreach迴圈陣列和集合

j=dk .5 版本之後java有一種更簡單的。foreach迴圈。 可以更加簡潔的遍歷資料和集合。此時無需獲得陣列的集合長度,無需更具索引來訪問陣列元素和集合元素。自動遍歷陣列和集合的每個元素‘ foreach迴圈語法格式: for(type  variableName:

es6 陣列例項的 entries() , keys() 和 values() ES6 提供三個新的方法 —— entries(),keys()和values() —— 用於遍歷陣列。它們都返回一個遍歷器物件,可以用for...of迴圈進行遍歷,唯一的區別是keys()是對鍵名的遍歷、values(

ES6 提供三個新的方法 —— entries(),keys()和values() —— 用於遍歷陣列。它們都返回一個遍歷器物件,可以用for...of迴圈進行遍歷,唯一的區別是keys()是對鍵名的遍歷、values()是對鍵值的遍歷,entries()是對鍵值對的遍歷

【Javascript】JS陣列的三種方法:map、forEach、filter

前言   近一段時間,因為專案原因,會經常在前端對陣列進行遍歷、處理,JS自帶的遍歷方法有很多種,往往不加留意,就可能導致知識混亂的現象,並且其中還存在一些坑。前端時間在ediary中總結了js原生自帶的常用的對陣列遍歷處理的方法,分別為:map、forEach、filter,在講解知識點的同時,會類比相識

foreach陣列

foreach遍歷一維陣列 <?php //PHP陣列遍歷:foreach //定義陣列 $arr=array(1,2,3,4,5,6,7,8,9,10); //foreach迴圈 foreach ($arr as $key => $v

js陣列迴圈陣列內所有元素的方法

在js中陣列遍歷最簡單的辦法就是使用for然後再利用arr.length長度作為for最大限度值即可解決了,下面我們來看看一些有用的例項 例,for(){}遍歷陣列 <script type="text/javascript">  <!-- var

每日一條js之陣列物件forEach陣列方法

方法: array.forEach(function(當前元素(必), 當前元素的索引值, 當前元素所屬的陣列物件), thisValue(如果這個引數為空, "undefined" 會傳遞給 "this" 值)) 陣列物件的forEach中有兩個引數第一個是回撥函式,

關於AJAX請求後臺資料,接收後臺返回的JSON資料,以及前臺json資料和append追加元素。

在這兩天的學習以及練習過程中,需要涉及到微信小程式的一些東西。在小程式中,是不能直接跳轉後臺邏輯來獲取資料,而是通過使用AJAX傳值和AJAX請求,來向後臺邏輯傳遞資訊。 1,比如說註冊,填寫資訊之後,通過AJAX傳遞給後臺,後臺處理返回是否註冊成功的JSON資料,前臺再加以分析是否註冊成功,

PHP foreach陣列之如何判斷當前值已經是陣列的最後一個

先給出foreach的兩種語法格式 1,foreach (array_expression as $value) statement 2,foreach (array_expression as $key => $value) statement 第一種格式

寫一個能陣列和物件的通用的forEach函式

寫一個通用的函式,可以遍歷物件和陣列的通用函式 var obj = { x: 100, y: 200, z: 300 } var arr = [1, 2, 3, 4, 5, 6] function foreach(

js物件、陣列、js陣列方法大全、區分map()和forEach()以及filter()、區分for...in...和for...of...

1、給物件新增屬性:使用 object.prop 或object['prop']  給物件新增屬性 let obj={}; obj.name="zhangsan"; obj["age"]=23; obj["class"]="語文"; console.log(obj

陣列元素(foreach

foreach 語法結構提供了遍歷陣列的簡單方式。 foreach 僅能夠應用於陣列和物件,如果嘗試應用於其他資料型別的變數,或者未初始化的變數將發出錯誤資訊。有兩種語法: <1>foreach (array_expression as $value

for迴圈陣列

js陣列隨機打亂輸出,直接上程式碼: <script> var arr = ["a","b","c"]; document.write("舊陣列:"+arr.join()); arr.sort(function() { return 0.5

js中foreach,for in,for of的使用及區別,物件,陣列使用的不一樣 JavaScript

1. for(var obj in objs){ ... } 語句 如果集合中的元素是物件可以直接對它進行操作。範例:for (var l in document.links){ alert(l.href); } 如果物件是多個屬性和值的形式話得這樣用:var o

封裝一個既能陣列又能物件的的forEach函式

function newforEach(obj,fn) { var key; if(obj instanceof Array){ obj.forEach(function(item,index){ fn(item,index); })

js for,while 正序降序迴圈陣列

var arr = ["小強","小紅","小明","小白"]; js for迴圈正序 for(var i = 0;i<arr.length;i++){  console.log("My name is " + arr[i] + "!"); }; for(var

js for迴圈 陣列 物件屬性

1、js for迴圈 遍歷物件屬性 var person = {fname:"John",lname:"Doe",age:25}; for (x in person) {    console.l