dsg41888 2016-04-21 12:42
浏览 52
已采纳

如何在Codeigniter中使用jquery Ajax在行中添加新的动态行?

I have list of data in a row of tables and added a details action link.

When i click on details link of a row than get another list of data for particular clicked row.

So now i want to add another row with another table with list of rows data below clicked row.

Jquery code :

function project_list(id)
{
    var row_ID = 'row'+id;
    $.ajax({
      url : '<?=$this->config->base_url()?>admin_panel/project_list',
      type : 'post',
      data : 'id='+id
    }).done(function (data){
      alert(data);
       $('#'+row_ID).after(data);
    });
    console.log(row_ID); 
}

Html Code:

<?php
foreach ($data as $item) {?>
 <a href="#" onClick="project_list('<?=$item->ID;?>')">Projects</a>
            </td>
          </tr>
<?php } ?>

Codeigniter Code:

function project_list()
    {
        $id = $_POST['id'];
        $project_list = $this->admin_model->project_list($id);
        if(is_array($project_list)){
            $i=1;
            $table .= '<tr colspan=7><div><table><thead><tr><th>SN</th><th>Project Name</th></tr></thead><tbody>';
            foreach($project_list as $row)
            {
                $table .="<tr><td>$i</td><td>$row->project_name</tr>";
                $i++;
            }
            $table .='</tbody></table></div></tr>';
            echo $table;
        }   
    }

New Row is Successfully added after click on particular clicked row But issue is table and it's header part is not showing and even div not showing.

Right Now i got this output:

<tr><td>1</td><td>PHP</td></tr>

But Output will be:

    <tr colspan=2>
<div>
<table>
<thead><tr>
<th>SN</th><th>Project Name</th></tr>
</thead>
<tbody>
    <tr><td>1</td><td>PHP</td></tr>
</tbody></table></div></tr>
  • 写回答

1条回答 默认 最新

  • 我在西湖1 2016-04-21 13:13
    关注

    You forgot to add <td> after <tr>.

    Also Apply colspan on <td> instead of <tr> in PHP code.

    $table .= '<tr class="dynamicAddedTr"><td colspan=2><div><table><thead><tr><th>SN</th><th>Project Name</th></tr></thead><tbody>';
                foreach($project_list as $row)
                {
                    $table .="<tr><td>$i</td><td>$row->project_name</tr>";
                    $i++;
                }
                $table .='</tbody></table></div></td></tr>';
    

    To replace the content every time you click on <tr> instead of appending, you can give a custom class to dynamic <tr> and on click just check if <tr> with specific class is exists or not. If yes remove that and add add new content.

    Working jsFiddle: https://jsfiddle.net/go03oj39/1/ with static data in JS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢