以表格格式显示JSON数据,每行对应一个提交按钮。

huangapple go评论73阅读模式
英文:

Display JSON data in table format with a submit button corresponding to each row

问题

/* 如何将JSON数据显示在JSP页面的表格中,每行都有一个提交按钮。单击提交按钮时,行数据应发送到一个servlet。

我已经使用Ajax从Servlet获取了JSON数据。

以下是从Servlet获取JSON数据的Ajax调用 */

function updateprofile(){
    $.ajax({
        url: 'abc/ConnectionInfo',
        type: 'GET',
        data: {},
        dataType: 'json'
    }).done(function(data){
        renderProfiles(data);
    }).fail(function(){
        toaster.error("错误...");
    });
}

function renderProfiles(data){
    // 如何实现这个方法以表格格式显示所有数据,每行都有一个按钮。单击按钮时,它应发送配置文件ID到Servlet的POST方法。
}
英文:

/* How do I Display json data into a table in a JSP Page with a submit button in each row. On clicking submit button, Row data should be sent to a servlet.

I have fetched json data from a Servlet using Ajax.

Below is my ajax call to get JSON data from Servlet */

function updateprofile(){
$.ajax({
url : 'abc/ConnectionInfo',
type: 'GET',
data: {},
data type: 'json'
}).done(function (data){
     renderProfiles(data);
}).fail(function (){
toaster.error("Eror...");
});
}

function 
renderProfiles(data){

//How can I implement this 
method to display all the 
data in table format with a 
button corresponding to 
each row.  And on clicking 
of the button it should 
send a profile ID to 
Servlet Post method.
}

答案1

得分: 0

你可以使用$.foreach循环来迭代你的JSON数据并在表格中显示它们。演示代码(你需要根据你的要求对以下代码进行更改):

//your json data
var response = [{
    "metric": "connected[qa_KCDz->Exre]"
  },
  {
    "metric": "connected[qa_KTDz->Exre]"
  },
  {
    "metric": "connected[qa_KPDz->Exre]"
  }
];

var str = '<table border=1><tr>\n' +
  '<th>Something</th>\n' +
  '<th>Submit</th>\n' +
  '</tr>';
//循环遍历json数据
$.each(response, function(key, value) {
  //获取值
  var s = value['metric'];
  //获取'['的索引
  var i = s.indexOf('[');
  var data = s.slice(10, i + 8).trim();

  str += '<td>' + value['metric'] + '</td>';
  str += '<td> <form method="post" action="servletname"><input type="hidden" name="data" value=' + data + ' ><input type="submit" value ="send" ></form></td></tr>';


});
str += "</table>";

//添加数据
$("#table").html(str);

将上述jQuery代码放在你的function renderProfiles(data){..}中。另外,在<form></form>标签下,我添加了一个带有所需值的hidden输入字段,你需要在servlet的doPost方法中使用request.getParameter("data")获取该值。

英文:

You can use $.foreach loop to iterate your json data and show them in your table. Demo code(you need to do changes in below code as per your requirment) :

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

//your json data
var response = [{
    &quot;metric&quot;: &quot;connected[qa_KCDz-&gt;Exre]&quot;
  },
  {
    &quot;metric&quot;: &quot;connected[qa_KTDz-&gt;Exre]&quot;
  },
  {
    &quot;metric&quot;: &quot;connected[qa_KPDz-&gt;Exre]&quot;
  }
];

var str = &#39;&lt;table border=1&gt;&lt;tr&gt;\n&#39; +
  &#39;&lt;th&gt;Something&lt;/th&gt;\n&#39; +
  &#39;&lt;th&gt;Submit&lt;/th&gt;\n&#39; +
  &#39;&lt;/tr&gt;&#39;;
  //looping through json data
$.each(response, function(key, value) {
  //get value
  var s = value[&#39;metric&#39;];
  //getting index of [
  var i = s.indexOf(&#39;[&#39;);
  var data = s.slice(10, i + 8).trim();

  str += &#39;&lt;td&gt;&#39; + value[&#39;metric&#39;] + &#39;&lt;/td&gt;&#39;;
  str += &#39;&lt;td&gt; &lt;form method=&quot;post&quot; action=&quot;servletname&quot;&gt;&lt;input type=&quot;hidden&quot; name=&quot;data&quot; value=&#39; + data + &#39; &gt;&lt;input type=&quot;submit&quot; value =&quot;send&quot; &gt;&lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&#39;;


});
str += &quot;&lt;/table&gt;&quot;;

//appending data
$(&quot;#table&quot;).html(str);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;table&quot;&gt; &lt;/div&gt;

<!-- end snippet -->

Put above code of jquery under your function renderProfiles(data){..}.Also,under &lt;form&gt;&lt;/form&gt; tag i have added a hidden input field with the required value that you need to send to your servlet .Get that value using request.getParameter(&quot;data&quot;) in doPost method of servlet.

huangapple
  • 本文由 发表于 2020年4月8日 19:59:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定