Individual Column Filter Datatable Serverside

Individual Column Filter Datatable Serverside

aldo122aldo122 Posts: 4Questions: 1Answers: 0

I have a problem when implementing column filter on datatable serverside. Based on reference this I have follow the instruction and change my code.
It does do the ajax request, but my table still the same (not doing the filter)

here is my code. Is there something wrong ? Really appreciate your help. Thanks

    var table = $('#table').dataTable({ 
            aLengthMenu: [
                [15, 25, 50, 100, 200, -1],
                [15, 25, 50, 100, 200, "All"]
              ],
              iDisplayLength: 15,
            "processing": true, //Feature control the processing indicator.
            "serverSide": true, //Feature control DataTables' server-side processing mode.
            "ordering": false,
            "searching": true,
            "scrollY": true,
            "scrollX": true,
            "order": [], //Initial no order.

            // Load data for the table's content from an Ajax source
            "ajax": {
                "url": "<?php echo site_url('drawing/ajax_list_welding_plan')?>",
                "type": "POST",
                "data": function ( data ) {
                    data.drawing_no = $('#drawing_no_table').val();
                    data.project_no = $('#project_table').val();
                    data.drawing_title = $('#drawing_title_table').val();
                    data.document_title = $('#document_title_table').val();
                    data.client = $('#client_table').val();
                }
            },

            //Set column definition initialisation properties.
            "columnDefs": [
                { 
                    "targets": [ -1 ], //last column
                    "orderable": false, //set not orderable
                },
                { 
                    "targets": [ -2 ], //2 last column (photo)
                    "orderable": false, //set not orderable
                },
                {
                    "targets": [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21], // your case first column
                    "className": "text-center",
                    "width": "4%"
                 },
            ],

            initComplete: function() {
              var api = this.api();

              // Apply the search
              api.columns().every(function() {
                var that = this;

                $('input', this.footer()).on('keyup change', function() {
                  if (that.search() !== this.value) {
                    that
                      .search(this.value)
                      .draw();
                  }
                });
              });
            },

        });

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Is the server returning the correct data? That would be the place to look,

    Colin

  • aldo122aldo122 Posts: 4Questions: 1Answers: 0

    Hi Colin, Thanks for reply.

    No, The server does not returning the correct data. (Not filtering)
    Do you know what's the problem ?

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    It will be a problem with the server-side script - if it's not complying with the search request. Can you post the script here, please?

    Colin

  • aldo122aldo122 Posts: 4Questions: 1Answers: 0

    Hi Colin.

    I use Codeigniter and Ajax.
    This is the controller.

    public function ajax_list_welding_plan()
        {
            $this->load->model('drawing_model');
            $list = $this->drawing_model->get_datatables_welding_plan();
            $data = array();
            $no = $_POST['start'];
            foreach ($list as $drawing) {
                $row = array();
                $row[] = $drawing->welding_no;
                $row[] = $drawing->weld_length;
                $row[] = $drawing->weld_type;
                $row[] = $drawing->ssmb;
                $row[] = $drawing->size_throat;
                $row[] = $drawing->welding_position;
                $row[] = $drawing->mat_1_spec;
                $row[] = $drawing->plate_pipe;
                $row[] = $drawing->dia_pipe;
                $row[] = $drawing->thk;
                $row[] = $drawing->grouping;
                $row[] = $drawing->mat_2_spec;
                $row[] = $drawing->plate_pipe_2;
                $row[] = $drawing->dia_pipe_2;
                $row[] = $drawing->thk_2;
                $row[] = $drawing->grouping_2;
                $row[] = $drawing->branch_angle;
                $row[] = $drawing->surface;
                $row[] = $drawing->quality_class;
                $row[] = $drawing->visual;
                $row[] = $drawing->mpi_pt;
                $row[] = $drawing->ut;
                $row[] = $drawing->rt;
                $row[] = $drawing->identification_number;
                $row[] = $drawing->rev;
                $row[] = $drawing->spesification;
                $row[] = $drawing->welder_name;
                $row[] = $drawing->welder_id;
                $row[] = $drawing->welding_process;
                $row[] = $drawing->report_no_ndt;
                $row[] = $drawing->welding_repair;
    
                $data[] = $row;
            }
    
            $output = array(
                            "draw" => $_POST['draw'],
                            "recordsTotal" => $this->drawing_model->count_all_welding_plan(),
                            "recordsFiltered" => $this->drawing_model->count_filtered_welding_plan(),
                            "data" => $data,
                    );
            echo json_encode($output);
        }
    

    And this the Model.

       var $table_welding_plan = 'weldmap.t_welding_plan_detail';
        var $column_order_welding_plan = array(null); 
        var $column_search_welding_plan = array('welding_no','weld_type','ssmb', 'size_throat', 'welding_position', 'mat_1_spec', 'plate_pipe', 'mat_2_spec', 'plate_pipe_2', 'identification_number'); 
        var $order_welding_plan = array(null); 
    
        private function _get_datatables_query_welding_plan() { 
    
            $this->db->from('weldmap.v_welding_plan_detail');
            $i = 0;
            foreach ($this->column_search_welding_plan as $item) { 
                if(@$_POST['search']['value']) { 
                    if($i===0) { 
                        $this->db->group_start();
                        $this->db->like($item, $_POST['search']['value']);
                    } else {
                        $this->db->or_like($item, $_POST['search']['value']);
                    }
                    if(count($this->column_search_welding_plan) - 1 == $i) 
                        $this->db->group_end(); 
                }
                $i++;
            }
    
            if(isset($_POST['order'])) { 
                $this->db->order_by($this->column_order_welding_plan[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
            }  else if(isset($this->order)) {
                $order = $this->order;
                $this->db->order_by(key($order), $order[key($order)]);
            }
        }
    
    
    function get_datatables_welding_plan() {
            $this->_get_datatables_query_welding_plan();
            if(@$_POST['length'] != -1)
            $this->db->limit(@$_POST['length'], @$_POST['start']);
            $query = $this->db->get();
            return $query->result();
        }
    
    function count_all_welding_plan() {
            $this->db->from('weldmap.t_welding_plan_detail');
            return $this->db->count_all_results();
        }
    
    function count_filtered_welding_plan() {
            $this->_get_datatables_query_welding_plan();
            $query = $this->db->get();
            return $query->num_rows();
        }
    
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I don't see anything in your code there doing the column filtering? DataTables will send a columns[i][search][value] parameter for each column. If you are writing your own server-side processing script, and you want server-side processing, you'd need to use that parameter.

    Allan

  • aldo122aldo122 Posts: 4Questions: 1Answers: 0

    Hello Allan.

    Could you give me an example ? Thanks

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    Here is the PHP code used in the server side examples for this site:
    https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php

    Kevin

This discussion has been closed.