如何通过在 codeigniter 中传递 id 将 json 数据传递到数据表中?

how to pass json data into datatable with passing id in codeigniter?

我通过传递 generator_id 作为参数从数据库中获取数据,我在 Model & Controller 中执行查询但是如何在jquery中传入generator_id,根据id取数据到数据table。谁能帮帮我?

示例:

当传递 generator id 1 时,数据 table 应该获取 id 1 的生成器详细信息。

非常感谢

我的模型:

public function getGeneratorRAReport($param,$generator_id){

    $arOrder = array('','RA_number');
    $this->db->where("RA_status",1);
    $this->db->where("generator_id",$generator_id);

    if ($param['start'] != 'false' and $param['length'] != 'false') {
        $this->db->limit($param['length'],$param['start']);
    }
    $this->db->select('*,DATE_FORMAT(RA_start_date,\'%d-%m-%Y\') as RA_start_date,DATE_FORMAT(RA_end_date,\'%d-%m-%Y\') as RA_end_date');
    $this->db->from('rental_agreement');
    $this->db->join('customer','customer_id = customer_id_fk');
    $this->db->join('generators','generator_id = generator_id_fk');
    $this->db->join('rental_plan','rental_plan_id = rental_plan_id_fk');
    $this->db->order_by('RA_id', 'DESC');
    $query = $this->db->get();

    $data['data'] = $query->result();
    $data['recordsTotal'] = $this->getGeneratorRAReportTotalCount($param,$generator_id);
    $data['recordsFiltered'] = $this->getGeneratorRAReportTotalCount($param,$generator_id);
    return $data;

}
public function getGeneratorRAReportTotalCount($param,$generator_id){

    $this->db->where("RA_status",1);
    $this->db->where("generator_id",$generator_id);
    if ($param['start'] != 'false' and $param['length'] != 'false') {
        $this->db->limit($param['length'],$param['start']);
    }
    $this->db->select('*');
    $this->db->from('rental_agreement');
    $this->db->join('customer','customer_id = customer_id_fk');
    $this->db->join('generators','generator_id = generator_id_fk');
    $this->db->join('rental_plan','rental_plan_id = rental_plan_id_fk');
    $this->db->order_by('RA_id', 'DESC');
    $query = $this->db->get();
    return $query->num_rows();

}

我的控制器:-

public function index()
{
    $template['body'] = 'Generators/Generator_RAReport';
    $template['script'] = 'Generators/Generator_RAReport_script';
    $this->load->view('template', $template);
}

public function get($generator_id){
    $this->load->model('Generator_model');
    $param['draw'] = (isset($_REQUEST['draw']))?$_REQUEST['draw']:'';
    $param['length'] =(isset($_REQUEST['length']))?$_REQUEST['length']:'10'; 
    $param['start'] = (isset($_REQUEST['start']))?$_REQUEST['start']:'0';
    $param['order'] = (isset($_REQUEST['order'][0]['column']))?$_REQUEST['order'][0]['column']:'';
    $param['dir'] = (isset($_REQUEST['order'][0]['dir']))?$_REQUEST['order'][0]['dir']:'';
    $param['searchValue'] =(isset($_REQUEST['search']['value']))?$_REQUEST['search']['value']:'';



    $data = $this->Generator_model->getGeneratorRAReport($param,$generator_id);
    $json_data = json_encode($data);
    echo $json_data;
}

查看:-

 <div class="box-body table-responsive">
          <table id="RA_details_table" class="table table-bordered table-striped">
            <thead>
            <tr>
              <th>Sl No.</th>
              <th>RA number</th>
              <th>RA type</th>
              <th>Customer</th>
              <th>RA start date</th>
              <th>RA end date</th>
              <th>Description</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
          </table>
        </div>
        <!-- /.box-body -->

脚本:-

$(function () { 
var RA_type = {'I':'Inside','O':'Outside'};
$table = $('#RA_details_table').DataTable( {
    "searching": false,
    "processing": true,
    "serverSide": true,
    "bDestroy" : true,
    dom: 'lBfrtip',
        buttons: [

        ],
    "ajax": {
        "url": "<?php echo base_url();?>index.php/Generator_RAReport/get/",
        "type": "POST",
        "data" : function (d) {
         }

    },
    "createdRow": function ( row, data, index ) {


        $table.column(0).nodes().each(function(node,index,dt){
        $table.cell(node).data(index+1);
        });
        $('td',row).eq(2).html(RA_type[data['RA_type']]);

    },

    "columns": [
        { "data": "RA_id", "orderable": false },
        { "data": "RA_number", "orderable": false },
        { "data": "RA_type", "orderable": false },
        { "data": "customer_name", "orderable": false },
        { "data": "RA_start_date", "orderable": false },
        { "data": "RA_end_date", "orderable": false },
        { "data": "RA_description", "orderable": false }
    ]

} );

在这种情况下,您可以在视图中同时使用模型和客户端代码(jquery)。 code igniter 不会强迫你使用 mvc

$ci =& get_instance();
$ci->load->model('some-model');
$ci->some-model->get();

在您的 html 区域放置此标签..

<input type="hidden" id="gen_id" value="<?=$gen_id?>" />

在你的js代码中...

   var id = document.getElementById('gen_id').value;   


 "ajax": {
            "url": "<?php echo base_url();?>index.php/Generator_RAReport/get/"+id,
            "type": "GET",
            "data" : function (d) {
             }

        },