如何在AJAX中将参数从模型中导入视图

时间:2017-02-02 03:51:27

标签: php ajax codeigniter

你好我们在codeigniter和使用AJAX的新手。 所以我想问一个简单的问题

我想将参数中的数据解析为ajax中的模型

所以当我打开页面时,数据表显示的是我在模型

中查询的表格

这是我的代码段:

观点

<?php 
//echo form_open('redo/edit_simpan');
     //echo form_open('person/insert_detail');   
    foreach ($data->result() as $v) {
            $id         = $v->id_request;
            $unit       = $v->unit_permintaan;
            $dokumen    = $v->nomor_dokumen_pendukung;
            $tanggal    = $v->tanggal;
            $status     = $v->status;
        } 

&GT;

$(document).ready(function() {

//datatables
table = $('#table').DataTable({ 

    "processing": true, //Feature control the processing indicator.
    "serverSide": true, //Feature control DataTables' server-side processing mode.
    "order": [], //Initial no order.

    // Load data for the table's content from an Ajax source
    "ajax": {
        "url": "<?php echo site_url('barang/ajax_list/')?>/",
        "type": "POST"
    },

    //Set column definition initialisation properties.
    "columnDefs": [
    { 
        "targets": [ -1 ], //last column
        "orderable": false, //set not orderable
    },
    ],

}); }

控制器

public function ajax_list()
{
    $list = $this->barang->get_datatables();
    $data = array();
    $no = $_POST['start'];
    foreach ($list as $person) {
        $no++;
        $row = array();
        $row[] = $person->nama_barang;
        $row[] = $person->jumlah_barang;
        $row[] = $person->satuan_barang;
        $row[] = $person->keterangan_barang;

        //add html for action
        $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person('."'".$person->id_request."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
              <a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person('."'".$person->id_request."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>
              <a class="btn btn-sm btn-success" href="edit_data/'.$person->id_request.'" title="Add_Detail" ><i class="glyphicon glyphicon-plus"></i> Barang</a>';


        $data[] = $row;
    }

    $output = array(
                    "draw" => $_POST['draw'],
                    "recordsTotal" => $this->barang->count_all(),
                    "recordsFiltered" => $this->barang->count_filtered(),
                    "data" => $data,
            );
    //output to json format
    echo json_encode($output);
}

MODEL

    function get_datatables($idreq)
{
    $this->_get_datatables_query();
    if($_POST['length'] != -1)
    $this->db->limit($_POST['length'], $_POST['start']);

    $this->db->where('id_request',$idreq); //paramter
    $query = $this->db->get();
    return $query->result();
}
  

我希望参数 $ idreq 显示在数据表上   查看任何线索如何?

     

我想要的数据是$ no = $ v-&gt; $ id_request到get_datatables($ idreq)中的模型

1 个答案:

答案 0 :(得分:0)

在视图中首先加载这样的模型..

$this->load->model('model_name');

然后将数据传递给模型这样的函数..

$this->model_name->get_datatables($id);