Jquery 带有服务器端处理的Datatable的Codeigniter动态列

Jquery 带有服务器端处理的Datatable的Codeigniter动态列,jquery,codeigniter,datatables,server-side-rendering,Jquery,Codeigniter,Datatables,Server Side Rendering,CodeIgniter服务器端处理数据表中不支持动态列, 我尝试了很多方法 请告诉我如何动态使用jQuery数据表列 提前感谢有大量可用资源。对于视图代码,像这样从jquery函数调用控制器 <div class="table-responsive"> <table id="user_data" class="display table" style="width: 100%; cellspacing: 0;"> <the

CodeIgniter服务器端处理数据表中不支持动态列, 我尝试了很多方法

请告诉我如何动态使用jQuery数据表列
提前感谢

有大量可用资源。对于视图代码,像这样从jquery函数调用控制器

  <div class="table-responsive">
        <table id="user_data" class="display table" style="width: 100%; cellspacing: 0;">
            <thead>
            <tr>
                <th>Id</th>
                <th>Product title</th>
                <th>Product content</th>
                <th>Product price</th>
                <th>Product size</th>
                <th>Product qty</th>
                <th>Update</th>
                <th>Delete</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th>Id</th>
                <th>Product title</th>
                <th>Product content</th>
                <th>Product price</th>
                <th>Product size</th>
                <th>Product qty</th>
                <th>Update</th>
                <th>Delete</th>
            </tr>
            </tfoot>
        </table>
  </div>


<script type="text/javascript" language="javascript" >
    $(document).ready(function(){
        var dataTable = $('#user_data').DataTable({
            "processing":true,
            "serverSide":true,
            "order":[],
            "ajax":{
                url:"<?php echo base_url() . 'admin_panel/fetch_product'; ?>",
                type:"POST"
            },
            "columnDefs":[
             {
                 "targets":[0, 3, 4],
                 "orderable":false,
             },
           ],
       });
</script>
<?php
class Crud_model extends CI_Model
{
/*    var $table = "banner";
    var $select_column = array("banner_id", "banner_title", "banner_text", "banner_image");
    var $order_column = array("banner_id", "banner_title", "banner_text", null, null);*/


    function make_query($table,$select_column,$order_column,$where,$like,$or_like=false)
    {
        $this->db->select($select_column);
        $this->db->from($table);
        if(isset($_POST["search"]["value"]))
        {
            $this->db->like($like, $_POST["search"]["value"]);
            $this->db->or_like($or_like, $_POST["search"]["value"]);
        }
        if(isset($_POST["order"]))
        {
            $this->db->order_by($order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        }
        else
        {
            $this->db->order_by($where, 'DESC');
        }
    }
    function make_datatables($table,$select_column,$order_column,$where,$like,$or_like){
        $this->make_query($table,$select_column,$order_column,$where,$like,$or_like);
        if($_POST["length"] != -1)
        {
            $this->db->limit($_POST['length'], $_POST['start']);
        }
        $query = $this->db->get();
        return $query->result();
    }

    function get_filtered_data($table,$select_column,$order_column,$where,$like,$or_like){
        $this->make_query($table,$select_column,$order_column,$where,$like,$or_like);
        $query = $this->db->get();
        return $query->num_rows();
    }
    function get_all_data($table)
    {
        $this->db->select("*");
        $this->db->from($table);
        return $this->db->count_all_results();
    }
/*    function insert_crud($data)
    {
        $this->db->insert('users', $data);
    }*/

身份证件
产品名称
产品含量
产品价格
产品尺寸
产品数量
更新
删除
身份证件
产品名称
产品含量
产品价格
产品尺寸
产品数量
更新
删除
$(文档).ready(函数(){
var dataTable=$(“#用户_数据”).dataTable({
“处理”:对,
“服务器端”:正确,
“订单”:[],
“ajax”:{
url:“”,
类型:“职位”
},
“columnDefs”:[
{
“目标”:[0,3,4],
“可订购”:错误,
},
],
});
对于服务器端,调用codeigniter controller函数,该函数将使用modal函数从数据库中获取所有数据,并将其发送到ajax调用,ajax调用将在表中显示该数据:

public function fetch_product(){

    $this->load->model("crud_model"); //This will load Modal into controller
    $fetch_data = $this->crud_model->make_datatables("product",array("product_id", "product_title","product_desc","product_price","product_size","product_qty"),array("product_id", "product_title", null, null),"product_id","product_title","product_title"); // this will call modal function for fetching data
    $data = array();
    foreach($fetch_data as $row) // Loop over the data fetched and store them in array
    {
        $sub_array = array();
        $sub_array[] = $row->product_id;
        $sub_array[] = $row->product_title;
        $sub_array[] = $row->product_desc;
        $sub_array[] = $row->product_price;
        $sub_array[] = $row->product_size;
        $sub_array[] = $row->product_qty;
        $sub_array[] = '<button type="button" name="update" data-id="'.$row->product_id.'" data-option=0 data-toggle="modal" data-target="#userModal" class="edit_post btn btn-warning btn-xs">Update</button>';
        $sub_array[] = '<button type="button" name="delete" data-id="'.$row->product_id.'" data-toggle="modal" data-target="#myModal2" id="d_b" class="delete_post btn btn-danger btn-xs">Delete</button>';
        $data[] = $sub_array;
    }
    $output = array(
        "draw"                    =>     intval($_POST["draw"]),
        "recordsTotal"          =>      $this->crud_model->get_all_data("product"),
        "recordsFiltered"     =>     $this->crud_model->get_filtered_data("product",array("product_id", "product_title","product_desc","product_price","product_size","product_qty"),array("product_id", "product_title", null, null),"product_id","product_title","product_title"),
        "data"                    =>     $data
    );
    echo json_encode($output);
}
公共函数fetch_product(){
$this->load->model(“crud_model”);//这将把model加载到控制器中
$fetch_data=$this->crud_model->make_datatables(“product”、数组(“product_id”、“product_title”、“product_desc”、“product_price”、“product_size”、“product_qty”)、数组(“product_id”、“product_title”、null、null)、“product_id”、“product_title”、“product_title”);这将调用模式函数来获取数据
$data=array();
foreach($fetch_data as$row)//循环获取的数据并将其存储在数组中
{
$sub_array=array();
$sub_array[]=$row->product_id;
$sub_array[]=$row->product_title;
$sub_array[]=$row->product_desc;
$sub_array[]=$row->product_price;
$sub_array[]=$row->product_size;
$sub_数组[]=$row->product_数量;
$sub_数组[]='Update';
$sub_数组[]='Delete';
$data[]=$sub_数组;
}
$output=array(
“draw”=>intval($\u POST[“draw”]),
“recordsTotal”=>this->crud\u model->获取所有数据(“产品”),
“recordsFiltered”=>this->crud\u model->get\u filtered\u data(“产品”、数组(“产品id”、“产品标题”、“产品描述”、“产品价格”、“产品大小”、“产品数量”)、数组(“产品id”、“产品标题”、null、null)、“产品id”、“产品标题”、“产品标题”),
“数据”=>$data
);
echo json_编码($output);
}
现在从模式中获取数据,模式代码将如下所示

  <div class="table-responsive">
        <table id="user_data" class="display table" style="width: 100%; cellspacing: 0;">
            <thead>
            <tr>
                <th>Id</th>
                <th>Product title</th>
                <th>Product content</th>
                <th>Product price</th>
                <th>Product size</th>
                <th>Product qty</th>
                <th>Update</th>
                <th>Delete</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th>Id</th>
                <th>Product title</th>
                <th>Product content</th>
                <th>Product price</th>
                <th>Product size</th>
                <th>Product qty</th>
                <th>Update</th>
                <th>Delete</th>
            </tr>
            </tfoot>
        </table>
  </div>


<script type="text/javascript" language="javascript" >
    $(document).ready(function(){
        var dataTable = $('#user_data').DataTable({
            "processing":true,
            "serverSide":true,
            "order":[],
            "ajax":{
                url:"<?php echo base_url() . 'admin_panel/fetch_product'; ?>",
                type:"POST"
            },
            "columnDefs":[
             {
                 "targets":[0, 3, 4],
                 "orderable":false,
             },
           ],
       });
</script>
<?php
class Crud_model extends CI_Model
{
/*    var $table = "banner";
    var $select_column = array("banner_id", "banner_title", "banner_text", "banner_image");
    var $order_column = array("banner_id", "banner_title", "banner_text", null, null);*/


    function make_query($table,$select_column,$order_column,$where,$like,$or_like=false)
    {
        $this->db->select($select_column);
        $this->db->from($table);
        if(isset($_POST["search"]["value"]))
        {
            $this->db->like($like, $_POST["search"]["value"]);
            $this->db->or_like($or_like, $_POST["search"]["value"]);
        }
        if(isset($_POST["order"]))
        {
            $this->db->order_by($order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        }
        else
        {
            $this->db->order_by($where, 'DESC');
        }
    }
    function make_datatables($table,$select_column,$order_column,$where,$like,$or_like){
        $this->make_query($table,$select_column,$order_column,$where,$like,$or_like);
        if($_POST["length"] != -1)
        {
            $this->db->limit($_POST['length'], $_POST['start']);
        }
        $query = $this->db->get();
        return $query->result();
    }

    function get_filtered_data($table,$select_column,$order_column,$where,$like,$or_like){
        $this->make_query($table,$select_column,$order_column,$where,$like,$or_like);
        $query = $this->db->get();
        return $query->num_rows();
    }
    function get_all_data($table)
    {
        $this->db->select("*");
        $this->db->from($table);
        return $this->db->count_all_results();
    }
/*    function insert_crud($data)
    {
        $this->db->insert('users', $data);
    }*/

谢谢你的回复,我的问题是我想设置column/columnedef[标题、宽度等]数据表的动态设置不在“请建议我”选项中,与您动态设置的方式相同,首先您从数据库中获取所有数据,并查看希望显示的行,然后基于此,您可以创建一个列名数组,并将其与行一起传递给html。先生,是否可以在cod中使用服务器端处理数据表eigniter?如果您要求从服务器端获取th标记,那么是的。非常感谢您的快速回复,请与我分享相同的代码,我是CodeIgniter和jQuery的新手,datatable中没有关于服务器端进程的动态列的参考