为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过Java获取JS Grid值

如何通过Java获取JS Grid值

繁星淼淼 2021-04-27 09:33:48
我正在使用js网格(http://js-grid.com/docs/),当用户单击“提交”按钮时,我想获取所有js-grid内容,如下所示:<link type="text/css" rel="stylesheet" href="jsgrid.min.css" /><link type="text/css" rel="stylesheet" href="jsgrid-theme.min.css" /><script type="text/javascript" src="jquery-3.3.1.min.js"></script>    <script type="text/javascript" src="jsgrid.min.js"></script><form name="form1"  method="post" action="MyJavaBackend"><div id="jsGrid"></div><Input type="submit"></form><script>    var clients = [        { "Name": "Otto Clay", "Age": 25, "Country": 1, "Address": "Ap #897-1459 Quam Avenue", "Married": false },    ];    var countries = [        { Name: "", Id: 0 },        { Name: "United States", Id: 1 },        { Name: "Canada", Id: 2 },        { Name: "United Kingdom", Id: 3 }    ];    $("#jsGrid").jsGrid({        width: "100%",        height: "400px",        inserting: true,        editing: true,        sorting: true,        paging: true,        data: clients,        fields: [            { name: "Name", type: "text", width: 150, validate: "required" },            { name: "Age", type: "number", width: 50 },            { name: "Address", type: "text", width: 200 },            { name: "Country", type: "select", items: countries, valueField: "Id", textField: "Name" },            { name: "Married", type: "checkbox", title: "Is Married", sorting: false },            { type: "control" }        ]    });</script>但是我的后端文件:public ActionForward create(ActionMapping mapping, ActionForm form,            HttpServletRequest request,            HttpServletResponse response)    throws Exception {        String[] datas = request.getParameterValues("data");// **get null**        return mapping.findForward("create");    }我已经检查了以下文章:http: //zetcode.com/articles/jsgridservlet/但是它只能得到一行编辑过的记录。(我要获取所有表数据)在用户通过request.getParameterValues或request.getParameter或.....单击提交按钮后,如何从jsgrid获取所有数据(所有用户输入)?
查看完整描述

1 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

在javascript结尾的字体中:


function onSubmit(){    


    var items = $("#jsGrid").jsGrid("option", "data");//$("#jsGrid") must match <div id="jsGrid"></div>

    var json = JSON.stringify(items);

    document.form1.json.value=json;

}


<form name="form1"  method="post" action="MyJavaBackend">

<div id="jsGrid"></div>

<input type="hidden" name="json"  >

<Input type="button" onClick="onSubmit();">

</form>


在后端Java中:


import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;


.....

String json_data = request.getParameter("json");

JSONArray jsonArray = new JSONArray(json_data);

for(int i=0;i<jsonArray.length();i++) {

                    JSONObject jsonObject=jsonArray.getJSONObject(i);


                    String name=jsonObject.getString("Name");

                    String age=jsonObject.getString("Age");

                }

....


查看完整回答
反对 回复 2021-05-12
  • 1 回答
  • 0 关注
  • 191 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信