頁面同時傳多條數據到後臺並保存到數據庫

需求場景:須要同時提交多條數據到數據庫的應用場景,如保存購物訂單等java

思路:前臺js獲取須要提交數據,拼寫成json字符串傳入後臺,後臺解析保存數據庫。數據庫

實例:json

        function makeJson(){
              var rows = document.getElementById("Table").rows; //得到行數(包括thead)
              var colums = document.getElementById("Table").rows[0].cells.length; //得到列數
              console.log(rows);
              console.log(colums);
              console.log(rows[1].cells[2]);
                 var arr=[];  //json對象容器
                var obj={"id":"",      //建立數據對象
                                 "set_id":"",
                                 "islist":"",
                                 "isedit":"",
                                 "dict_type":"",
                                 "column_name":"",
                                 "java_type":"",
                                 "java_attribute_name":"",
                                 "isnull":"",
                                 "data_type":"",
                                 "isquery":"",
                                 "query_type":"",
                                 "isshowinlist":"",
                                 "ispk":"",
                                 "show_type":"",
                                 "column_comment":"",
                                 "sort":""
                                 };
                    for(var i=1;i<rows.length;i++){ //獲取表格數據,由於列數已經固定,因此直接獲取了,其實能夠經過循環遍從來的到
                        var obj = new Object();//這裏必定要new新的對象,要否則保存的都是同樣的數據;都是最後一行的數據
                        obj.column_name=$("#column_name"+i).val();
                        obj.column_comment=$("#column_comment"+i).val();
                        obj.data_type=$("#data_type"+i).val();
                        obj.java_type=$("#java_type"+i).val();
                        obj.java_attribute_name=$("#java_attribute_name"+i).val();
                        obj.ispk=$("#ispk"+i).val();
                        obj.isnull=$("#isnull"+i).val();
                        obj.isedit=$("#isedit"+i).val();
                        obj.islist=$("#islist"+i).val();
                        obj.isshowinlist=$("#isshowinlist"+i).val();
                        obj.isquery=$("#isquery"+i).val();
                        obj.query_type=$("#query_type"+i).val();
                        obj.show_type=$("#show_type"+i).val();
                        obj.dict_type=$("#dict_type"+i).val();
                        obj.sort=$("#sort"+i).val();
                         arr.push(obj);//向JSON數組添加JSON對象的方法;很關鍵
                    }
                      ///格式化數據
                     var jsonobj=JSON.stringify(arr);//這一行很關鍵
                     //將數據賦值經過按鈕提交到後臺
                     $("#jsonString").val(jsonobj);數組

                    

                }ssh

相關文章
相關標籤/搜索