如何通过Java获取JS Grid值

我正在使用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获取所有数据(所有用户输入)?


繁星淼淼
浏览 192回答 1
1回答

慕斯王

在javascript结尾的字体中:function onSubmit(){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var items = $("#jsGrid").jsGrid("option", "data");//$("#jsGrid") must match <div id="jsGrid"></div>&nbsp; &nbsp; var json = JSON.stringify(items);&nbsp; &nbsp; document.form1.json.value=json;}<form name="form1"&nbsp; method="post" action="MyJavaBackend"><div id="jsGrid"></div><input type="hidden" name="json"&nbsp; ><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++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject=jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name=jsonObject.getString("Name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String age=jsonObject.getString("Age");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }....
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java