douhu2898 2017-08-14 03:20
浏览 233
已采纳

从数据库中获取数据并显示到表

(I already asked this but my past question is quite confusing) Im going to display database values to the table. I need to display it dynamically. All the tables are dynamic.

The tables in database:

Items Table:
itemID | Item Name
   1   |   item1
   2   |   item2
   3   |   item3
   4   |   item4
   5   |   item5 ..and so on

SkillSet Table:
skillID| Skill Name
   1   |   CS
   2   |   IT
   3   |   ES
   4   |   IS .. and so on

Values Table:
valueID | itemID | skillID | values
   1    |    1   |    1    |    0
   2    |    1   |    2    |    1
   3    |    1   |    3    |    4
   4    |    1   |    4    |    4
   5    |    2   |    1    |    3
   6    |    2   |    2    |    0
   7    |    2   |    3    |    2
   8    |    2   |    4    |    2 .. and so on

the output must be:

      | itm1 | itm2 | itm3 | itm4 | itm5
------|------|------|------|------|-----
CS    |    0 |    3 |    1 |    4 |   0
------|------|------|------|------|-----
IT    |    1 |    0 |    4 |    2 |   0
------|------|------|------|------|-----
ES    |    4 |    2 |    3 |    0 |   1
------|------|------|------|------|-----
IS    |    4 |    2 |    3 |    0 |   1

I've done it using a jquery/ajax to display it in each cell only by clicking/hovering each respective td's. But i want to display it automatically as soon as the page loaded like what "foreach statement" do so.. i dont know how...

well, this is my jquery for displaying values by clicking/hovering.

$('tbody tr td').click(function(){
  var row = $(this).closest('td');
  var skill = row.find('.skillID').val();
  var index = row.index();
  var item = $('table thead tr').find('td').eq(index).val();

  $.ajax({
   type: "POST",
   url: "<?php echo base_url(); ?>controller/get_level",
   data: {'skillID':skill,'itemID':item},
   cache: false,
   success: function(data){
      row.find("input[type=text]").attr("value",data);
     }
  });
});

Here's the link of my last question.. Display the value from database to dynamically created textfield in the table using jQuery

  • 写回答

2条回答 默认 最新

  • dsz7121 2017-08-14 04:42
    关注

    this was the answer from @gjijo, and its working

    $(function() {
    $('tbody tr td').each(function() {
    var col = $(this);
    var skill = col.find('.skillID').val();
    var index = col.index();
    var item = $('table thead tr').find('td').eq(index).text();
    
    console.log('Skill_ID - ' + skill + ';
    Item_ID - ' + item);
    $.ajax({
      type: "POST",
      url: "<?php echo base_url(); ?>controller/get_level",
      data: {
        'Skill_ID': skill,
        'Item_ID': item
      },
      cache: false,
      success: function(data) {
        col.find("input[type=text]").val("Level " + data);
      }
    });
    });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件