Jquery easyui datagrid从codeigniter控制器加载数据

Posted

技术标签:

【中文标题】Jquery easyui datagrid从codeigniter控制器加载数据【英文标题】:Jquery easyui datagrid load data from codeigniter controller 【发布时间】:2021-08-02 19:54:13 【问题描述】:

我正在使用 Jquery easyui 表的 Codeigniter Web 应用程序,在表中我创建了一个选择下拉列表,它被附加到表行中

我可以通过 ajax post 将数据库中的下拉值保存到控制器,但我不知道如何根据id 选择存储在数据库中的下拉值?

HTML 和 Jquery

 <table
            id="tt"
            class="easyui-datagrid"
            url="<?php echo base_url()."contacts/contacts_list_data"; ?>"

            pagination="true"
            rownumbers="true"
            toolbar="#tb"
            pageSize="10"
            pageList="[5,10,20,50,100]"
            fit= "true"
            fitColumns= "true"
            nowrap= "true"
            view= "detailview"
            idField="id"
            >
              <thead>
                  <tr>
                   <th field="id"  checkbox="true"></th>

                   <th field="email"  sortable="true">
                    <?php echo $this->lang->line('Email'); ?></th>

                      <th field="status_type"  formatter='status_column'>
                      <?php echo $this->lang->line('Status'); ?></th>

                  </tr>
              </thead>

            </table>

<script>
    function status_column(value,row,index)
    
        var inputVal = document.getElementById("status_column").innerhtml;
        return inputVal;



    
</script>

<script>
    function getSelectedValue(dg, field)
        var dg = $('#tt');
        var row = dg.datagrid('getSelected');
        if (!row)return undefined;
        var fields = dg.datagrid('getColumnFields',true).concat(dg.datagrid('getColumnFields',false));
        if (typeof field == 'number')
            return row[fields[field]];
         else 
            return row[field];
        
    

</script>

<div id="status_column" style="display:none">
    <select name="stat_column" id="stat_column"  class="form_control stat_column">
        <option value="0">Select Status</option>
        <option value='All'>All</option>
        <option value='Open'>Open</option>
        <option value='Closed'>Closed</option>
        <option value='Won'>Won</option>
        <option value='Lost Price is High'>Lost Price is High</option>
        <option value='Lost Not Enough Features'>Lost Not Enough Features</option>
        <option value='Lost Other'>Lost Other</option>
        <option value='Never Replied Back'>Never Replied Back</option>
        <option value='Bad Email'>Bad Email</option>
        <option value='Bad Number'>Bad Number</option>
        <option value='Do Not Disturb'>Do Not Disturb</option>
    </select>
</div>

<script>

    $('body').on('change', '.stat_column', function() 

        var stat_column = $(this).find("option:selected").val();
        var user_email = getSelectedValue('#tt', 2);


        $.ajax(
            url: base_url+'contacts/stat_column',
            type: 'POST',
            dataType: "json",
            data: 
                stat_column: stat_column,
                user_email:user_email,
            ,
            success: function(data) 
                if (data.statcolumn) 
                    $('#tt').datagrid('reload');
                    $(".reg-alert-success").css('display', 'block');
                    $(".reg-alert-success").html(data.statcolumn);



                

            
        );

    );

</script>

从数据库控制器函数中获取值

  public function contacts_list_data()
    


    $user_type = $this->session->userdata('user_type');
    $user_id = $this->session->userdata('user_id');

    $where_simple['user_id'] = $user_id;
    $where  = array('where'=>$where_simple);

    $offset = ($page-1)*$rows;
    $result = array();

    $table = "contact";

    $info = $this->basic->get_data($table, $where, $select='', $join='', $limit=$rows, $start=$offset, $order_by=$order_by_str);

     // echo $this->db->last_query();
    if ($search_contact_type_id)
    
        // $where_simple['contact_type_id like ']    = "%".$search_contact_type_id."%";
        $this->db->where("FIND_IN_SET('$search_contact_type_id',contact.contact_type_id) !=", 0);
    
    $total_rows_array = $this->basic->count_row($table, $where, $count="id");
    // echo $this->db->last_query();
    $total_result = $total_rows_array[0]['total_rows'];

    $info_count = count($info);


    foreach ($info as $key => $value)
    
      $value = $info[$key]['contact_type_id'];

      $type_id = explode(",",$value);

      $table = 'contact_type';
      $select = array('type');

      $where_group['where_in'] = array('id'=>$type_id);
      $where_group['where'] = array('deleted'=>'0');

      $info1 = $this->basic->get_data($table,$where_group,$select);

     $str = '';
     foreach ($info1 as  $value1)
      
        $str.= $value1['type'].",";
      


    $str = trim($str, ",");

    $info[$key]['contact_type_id']= $str;

    
    echo convert_to_grid_data($info, $total_result);


在数据库控制器函数中存储下拉值

public function stat_column()

    $stat_column= $this->input->post('stat_column');
    $user_email = $this->input->post('user_email');

    $data['status'] = $stat_column;

    $results = $this->basic->update_stat_column($user_email, $data);
   
    if ($results == TRUE) 
        echo json_encode('successfully ');
        //redirect('/thankyou/');
    

【问题讨论】:

谁能提供解决方案? 【参考方案1】:

这是基于您的结构的一种方法

$('body').on('change', '.stat_column', function() 
    var stat_column = $(this).find("option:selected").val();
    var user_id = $(this).closest('tr').find('td')[0].innerHTML

【讨论】:

实际上我需要数据库中的值,这些值根据用户 ID 存储在数据库中??

以上是关于Jquery easyui datagrid从codeigniter控制器加载数据的主要内容,如果未能解决你的问题,请参考以下文章

jQuery EasyUI- DataGrid使用 从数据库查询得到数据显示在前台的jsp页面,如何实现?

jquery easyUI datagrid 的宽度能不能设置成百分比

jquery easyUI datagrid 的宽度能不能设置成百分比

jQuery EasyUI datagrid 删除多行

jQuery easyUI easyui-datagrid 选中指定内容的行

jQuery EasyUI教程之datagrid应用