【问题标题】:populate table by dropdownlist通过下拉列表填充表格
【发布时间】:2019-10-10 10:21:05
【问题描述】:

我正在尝试在多对多 sql 表中获取数据(请参阅上传的图像)并将其提取到视图表(通过 client_id 获取 user_id)问题是下拉菜单不再可选择时我添加了 ajax 脚本,但表格没有加载必要的信息。请帮我解决这个问题。非常感谢。

--查看表格和表格--

<select  id="client_id" name="client_id" class="form-control" >
   <option value="">Select Client</option>
   <?php foreach ($clientList as $value){ ?>
   <option data-client-id="<?=$value['id']?>" value="<?php echo $value['id'] ?>"><?php echo $value['account_name'] ?></option>
  <?php }?>
</select>

<table style="width: 100%" class="table">
   <thead><tr><th>No.</th><th>user name</th><th>sl_rate</th></tr></thead>
   <tbody id="table-details"></tbody>
</table>

---AJAX---

<script type="text/javascript">
$(document).ready(function(){
    $('#client_id').change(function(){ 
        var id=$(this).val();
        var base_url = '<?php echo base_url() ?>';
        var $sr = ($(".jdr1").length + 1);
        var rowid = Math.random();
        $.ajax({
            url: base_url + "client/getClientUser",
            method : "POST",
            data: {'client_id': client_id},
            async : true,
            dataType : "JSON",
            success: function(data){         
                var html = '';
                var i;
                for(i=0; i<data.length; i++){
                    html += '<tr class="jdr1" id="' + rowid + '">' +
                            '<td><span class="btn btn-sm btn-default">' + $sr + '</span><input type="hidden" name="count[]" value="'+Math.floor((Math.random() * 10000) + 1)+'"></td>' +
                            '<td><input type="text" id="user_id-' + $sr + '" name="user_id[]" value="" /></td>' +
                            '<td><input type="text" id="sl_rate-' + $sr + '" name="sl_rate[]" value="" /></td>' +
                            '</tr>'
                }
                $('#table-details').html(html);
                }
            });
            return false;
    });      
});

--控制器--

public function getClientUser(){
$client_id = $this->input->get('client_id');
$data = $this->ClientModel->getUserByClientId($client_id);
echo json_encode($data);}

--型号--

public function getUserByClientId($clientid){
   $this->db->select('client_user.id, client_user.user_id, users.givenname');
    $this->db->from('client_user');
    $this->db->join('users', 'users.id = client_user.client_id');
    $this->db->where('client_user.client_id', $clientid);
    $this->db->order_by('client_user.id');
    $query = $this->db->get();
    return $query->result_array();
}

View screenshot of the sql table

【问题讨论】:

    标签: javascript ajax codeigniter activerecord


    【解决方案1】:

    我认为client_id 变量丢失了您正在传递数据。

    data: {'client_id': client_id}
    

    【讨论】:

    • 请多多指教?
    • 第一个问题在我添加了一个client_id变量时解决了,但它仍然没有在表中填充信息。
    • 如果您遇到任何错误,请检查您的控制台或网络选项卡。
    猜你喜欢
    • 1970-01-01
    • 2019-01-02
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多