【问题标题】:Need to populate JSON Array data in HTML Table需要在 HTML 表中填充 JSON 数组数据
【发布时间】:2015-04-22 11:57:41
【问题描述】:

我有这个 HTML

    <div class="searchresults">
    <div class="result">
        <!-- document ISO number -->
        <h4><a class="published" href="#">ISO 105-A05:1996</a></h4>
        <!-- document title -->
        <p>
            Textiles -- Tests for colour fastness -- Part A05: Instrumental assessment of change in colour for determination of grey scale rating
        </p>
        <h2 class="toggle">More details<span></span></h2>
        <div class="more_details clearfix">
            <table class="table table-striped table-bordered">
                <tr>
                    <th>Edition</th>
                    <th>Stage</th>
                    <th>TC</th>
                    <th>ICS</th>
                    <th>Document available as of</th>
                </tr>
                <tr>
                    <td>1</td>
                    <td>90.93</td>
                    <td><a href="#">ISO/TC 38/SC 1</a></td>
                    <td><a href="#">59.080.01</a></td>
                    <td>1996-03-28</td>
                </tr>
            </table>
        </div>  
    </div>  <!--result /--> 
</div>

如果是普通的 JSP,我可以获取列表并迭代 div 并获取这样的结果

    <div class="searchresults">
    <%
    for(List lists:list) {
    %>
    <div class="result">
        <!-- document ISO number -->
        <h4><a class="published" href="#">ISO 105-A05:1996</a></h4>
        <!-- document title -->
        <p>
            Textiles -- Tests for colour fastness -- Part A05: Instrumental assessment of change in colour for determination of grey scale rating
        </p>
        <h2 class="toggle">More details<span></span></h2>
        <div class="more_details clearfix">
            <table class="table table-striped table-bordered">
                <tr>
                    <th>Edition</th>
                    <th>Stage</th>
                    <th>TC</th>
                    <th>ICS</th>
                    <th>Document available as of</th>
                </tr>
                <tr>
                    <td>1</td>
                    <td>90.93</td>
                    <td><a href="#">ISO/TC 38/SC 1</a></td>
                    <td><a href="#">59.080.01</a></td>
                    <td>1996-03-28</td>
                </tr>
            </table>
        </div>  
    </div>  <!--result /--> 
    <%
     }
     %>
</div>

但在我的情况下,我正在进行 ajax 调用并获取 JSON 数组如何使用 JSON 数据迭代 div,如上所示并填充表我应该在 ajax 调用成功时调用什么?

【问题讨论】:

  • 除了使用 JavaScript 代替 Java 之外,你会为 JSP 做些什么?
  • 先生在这里使用 ajax 调用获取 JSON 数组我将如何在 div 上循环(即:像一个列表)?
  • 你有一个对象数组。您有一个表,该表需要使用该数组中的数据填充行。为表头和表脚使用模板,并通过迭代行以填充数据来构建主体。最好的办法是试一试,当您发现特定问题(例如数据未正确填充行)时,向我们展示您尝试过的内容、期望看到的内容以及代码运行时实际看到的内容。跨度>
  • 您的 JSON 数据是什么样的?这确实推动了如何解决问题。一般来说,The Head Rush 所说的与 JSP 一样,是正确的。您实际上只需遍历文档集合,然后使用模板,将文档数据绑定到模板。冲洗并重复,直到完成。

标签: jquery html ajax json


【解决方案1】:

jQuery's website 上很容易找到如何进行 AJAX 调用,所以我将跳过它。我还将假设您将使用 JSON 作为服务器的结果。

您可以做的是使用模板库,我将在此示例中使用handlebars.js

首先使用一个特殊的脚本标签从上面获取结果 HTML 并将其用作模板。

<script id="results" type="text/x-handlebars-template"> 
 ... notice the double braces ...
       <h4><a class="published" href="#">ISO {{published}}</a></h4>
</script>

用车把的双括号替换所有动态的并且来自您的 AJAX 调用的项目。

现在您将获取 AJAX 结果集并将其放入本地数组中。从您的页面中,获取要添加数据的 html 元素。

  <div id="replacement"></div>

现在遍历本地结果集中的每个项目,并使用项目数据填充您之前设置的模板化变量。继续将模板化的 HTML 元素添加到占位符。完成后,您现在可以将创建的模板设置为替换元素。

// define the source of your template and complie for use.
var source = document.getElementById('results').innerHTML;
var template = Handlebars.compile(source);

// faked AJAX results set  
var resultSet=[ {
  published: '105-A05:1996',
  title: "Textiles -- Tests for colour fastness -- Part A05:
          Instrumental assessment of change in colour for 
          determination of grey scale rating",
  Edition: '1',
  Stage: "90.93",
  TC: 'ISO/TC 38/SC 1',
  ICS: '59.080.01',
  available: '1996-03-28'
}

var appendedHtml = '';
// get the element where you will add your data.
var element = document.getElementById('replacement');    

// iterate over each entry from your data
resultSet.forEach(function(entry) {
  // the entey data now will fill in the templated variables
   var html = template(entry);
   // keep adding the html elements to a placeholder.
   appendedHtml += html
});

// now set the created templates to the replacement element.
element.innerHTML = appendedHtml;

这里有一个working Plunker 供您查看。这应该足以让你振作起来。此解决方案有两个关键,使用模板库进行数据替换并迭代结果以创建同一模板的多个副本。

祝你好运,希望这对一些人有所帮助。

【讨论】:

  • 克里斯感谢您帮助它工作,但我可以得到一个迭代 JSON 数组而不是使用模板的示例。
  • 我必须在单击复选框时调用 ajax 调用,所以我将您在 onSuccess 中给出的代码附加到它不起作用但是当我在页面加载时给出确切的代码时它可以正常工作并且复选框是使用的是模板之外的 AUI,我的意思是在单独的 div 中
  • 所以您不想让结果以您拥有 HTML 的方式显示?您确实需要展示您的 JSON 示例,以便我们更好地了解您想要做什么。据我目前所知,您需要的唯一迭代是您的 JSON 结果上的 forEach ,它将插入到您在问题中的模板化 div 中。如果这不正确,请提供更多信息。
  • 非常感谢 Chris 和 Head Rush 付出的时间和努力。Chris 我已经按照您在使用 handlebars.js 工作 plunker 中的建议使用和实施了,非常感谢你们 :)
猜你喜欢
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 2014-07-19
  • 1970-01-01
  • 2017-02-10
  • 2014-11-16
相关资源
最近更新 更多