You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter 3中如何将行数据(双参数)传入模态框

解决方案:点击表格行按钮加载对应数据到Bootstrap模态框

1. 修改表格按钮,添加数据属性传递参数

给触发模态框的按钮添加数据属性,携带当前行的loan id和borrower id(请根据实际字段名调整),让点击时能获取到请求所需的参数:

<?php foreach($open_applications as $row): ?>
<tr>
    <td><?php echo $row->col_full_name; ?></td> <!-- 修复原代码中未闭合的标签问题 -->
    <td><?php echo $row->col_loan_assignid; ?></td>
    <td>
        <button class="btn btn-secondary btn-xs" 
                data-bs-toggle="modal" 
                data-bs-target="#loanDecision"
                data-loan-id="<?php echo $row->col_loan_assignid; ?>"
                data-borrower-id="<?php echo $row->col_borrower_id; ?>"> <!-- 替换为你实际的borrower id字段 -->
            Pending
        </button>
        <a href="<?php echo site_url('Users/borrowerdetails')?>" role="button" class="btn btn-link btn-xs">View</a> <!-- 修复原代码中a标签错误闭合为button的问题 -->
    </td>
</tr>
<?php endforeach; ?>

2. 调整控制器,返回JSON格式数据

原控制器方法仅赋值未输出数据,需修改为返回JSON,供AJAX接收处理:

public function fetchDetails()
{
    // 从GET请求中获取参数
    $col_loan_assignid = $this->input->get('loan_id');
    $col_borrower_id = $this->input->get('borrower_id');

    $data['getloandetails'] = $this->Users_model->getingloandetails($col_loan_assignid);
    $data['getactiondetails'] = $this->Users_model->getactiondetails($col_borrower_id);

    // 返回JSON格式数据
    echo json_encode($data);
    exit;
}

3. 修复模型SQL注入风险

原模型直接拼接SQL参数存在安全隐患,改用CodeIgniter查询绑定优化:

public function getingloandetails($col_loan_assignid){
    // 使用查询绑定避免SQL注入
    $sql = "select * from {$this->tbl_loan_application} where col_loan_assignid like ?";
    $query = $this->db->query($sql, array('%'.$col_loan_assignid.'%'));
    return $query->row(); // 单条数据用row()更高效,多条则用result()
}

public function getactiondetails($col_borrower_id){
    $sql = "select * from {$this->tbl_action} where col_loan_assignid like ?";
    $query = $this->db->query($sql, array('%'.$col_borrower_id.'%'));
    return $query->result();
}

4. 编写jQuery/AJAX代码填充模态框

在页面底部添加JS代码,监听模态框显示事件,通过AJAX请求数据并渲染到模态框:

$(document).ready(function(){
    // 监听模态框即将显示的事件
    $('#loanDecision').on('show.bs.modal', function(e){
        // 获取触发模态框的按钮
        var triggerBtn = $(e.relatedTarget);
        // 提取按钮上的参数
        var loanId = triggerBtn.data('loan-id');
        var borrowerId = triggerBtn.data('borrower-id');

        // 发起AJAX请求
        $.ajax({
            url: '<?php echo site_url('Users/fetchDetails'); ?>',
            type: 'GET',
            data: {
                loan_id: loanId,
                borrower_id: borrowerId
            },
            dataType: 'json',
            success: function(res){
                // 填充模态框标题(替换为你实际的字段名)
                $('#loanDecisionLabel').text(res.getloandetails.col_full_name);
                $('#loanDecisionSubtitle').text(res.getloandetails.col_job); // 替换为实际的工作信息字段

                // 渲染另一张表的数据到表格
                var tableHtml = '';
                $.each(res.getactiondetails, function(i, item){
                    tableHtml += '<tr>';
                    tableHtml += '<td>' + item.column_name1 + '</td>'; // 替换为实际列名
                    tableHtml += '<td>' + item.column_name2 + '</td>';
                    tableHtml += '<td>' + item.column_name3 + '</td>';
                    tableHtml += '</tr>';
                });
                // 替换tbody内容
                $('#loanDecision .modal-body tbody').html(tableHtml);
            },
            error: function(){
                alert('数据加载失败,请重试');
            }
        });
    });
});

5. 优化模态框结构(可选)

给模态框内的表格tbody添加id,方便更精准定位:

<div class="modal-body">
    <div class="table-responsive">
        <table class="table table-hover table-bordered">
            <thead class="thead">
                <tr>
                    <th scope="col">列名1</th> <!-- 替换为实际列名 -->
                    <th scope="col">列名2</th>
                    <th scope="col">列名3</th>
                </tr>
            </thead>
            <tbody id="action-table-body"> <!-- 添加id -->
                <!-- AJAX数据将填充到这里 -->
            </tbody>
        </table>
    </div>
</div>

对应JS代码中修改为:

$('#action-table-body').html(tableHtml);

内容的提问来源于stack exchange,提问作者charlesultor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 21:31:17