twitter-bootstrap 通过 jquery 数据填充 bootstrap html 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25791178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 21:30:53  来源:igfitidea点击:

populating a bootstrap html table via jquery data

javascriptjqueryhtmlcsstwitter-bootstrap

提问by andriatz

i just buil a data APIT trough Kimono -> https://www.kimonolabs.com/api/2ewmh21u?apikey=lvafgzGqR6fOqrI0mXAbiPEmQGh7rR4m. i would like to include it in a simple and nice bootstrap table, like this: http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html.

我只是通过 Kimono -> https://www.kimonolabs.com/api/2ewmh21u?apikey=lvafgzGqR6fOqrI0mXAbiPEmQGh7rR4m构建了一个数据 APIT 。我想将它包含在一个简单而漂亮的引导表中,如下所示:http: //wenzhixin.net.cn/p/bootstrap-table/docs/examples.html

i just tried to insert the jquery code given by kimono

我只是尝试插入和服给出的 jquery 代码

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
function kimonoCallback(data) {
// do something with the data
// please make sure the scope of this function is global
}

$.ajax({
"url":"https://www.kimonolabs.com/api/2ewmh21u?apikey=lvafgzGqR6fOqrI0mXAbiPEmQGh7rR4m&callback=kimonoCallback",
"crossDomain":true,
"dataType":"jsonp"
});
 </script>

but i didn't manage to create the table. any advice?

但我没有设法创建表。有什么建议吗?

回答by wenyi

you can active bootstrap table via javascript:

您可以通过 javascript 激活引导表:

<table id="table">
  <thead>
    <tr>
      <th data-field="nome" data-formatter="linkFormatter">Nome</th>
      <th data-field="descrizione" data-formatter="linkFormatter">Descrizione</th>
    </tr>
  </thead>
</table>

<script>
function linkFormatter(value) {
  return '<a href="' + value.href + '">' + value.text + '</a>';
}

function kimonoCallback(data) {
  $('#table').bootstrapTable({
    data: data.results.collection1
  });
}

$.ajax({
  "url":"https://www.kimonolabs.com/api/2ewmh21u?apikey=lvafgzGqR6fOqrI0mXAbiPEmQGh7rR4m&callback=kimonoCallback",
  "crossDomain":true,
  "dataType":"jsonp"
});
 </script>

jsFiddle: http://jsfiddle.net/wenyi/8svjf80g/33/

jsFiddle:http: //jsfiddle.net/wenyi/8svjf80g/33/

回答by Brett

I have no idea what you want to display from your JSONP feed, but generally you can handle the display like so:

我不知道你想从你的 JSONP 提要中显示什么,但通常你可以像这样处理显示:

<table class="table table-striped table-bordered">
  <thead>
    <tr>
      <th>Href</th>
      <th>Text</th>
    </tr>
  </thead>
  <tbody id="loadHere">
  </tbody>
</table>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
function kimonoCallback(data) {
  // this will help you see data structure while you develop the table
  console.log(data);
  // then create your table
  var results = data.results.collection1,
     i;
  for (i = 0; i < results.length; i += 1) {
    $('#loadHere').append(
      '<tr>' +
        '<td>' + results[i].nome.href + '</td>' +
        '<td>' + results[i].nome.text + '</td>' +
      '<td>' +
    '</table>');
  }
}

$.ajax({
"url":"https://www.kimonolabs.com/api/2ewmh21u?apikey=lvafgzGqR6fOqrI0mXAbiPEmQGh7rR4m&callback=kimonoCallback",
"crossDomain":true,
"dataType":"jsonp"
});
 </script>

Be sure to look at the console to see how the data is structured so you can determine which fields to populate the table with.

请务必查看控制台以了解数据的结构方式,以便您可以确定要使用哪些字段填充表。

回答by animatedgif

Well.. You need to actually make the table when kimonoCallbackgets called.

嗯.. 你需要在kimonoCallback被调用时实际制作表格。

See?

看?

// do something with the data
// please make sure the scope of this function is global