0

I am using jquery datables for showing some stocks. By default all stock are showing but when I type some text in search box nothing happens, I have also check console there is no error, Please help me. Here is some code:

stock.js

$('#view-stock').DataTable({
    serverSide:true,
    searchable:true,
    ajax:{
        url:base_url+"stock/get_stock",
    }..and so on

stock_controller:

public function get_stock(){
    $stock['data']=$this->Stock_model->get_stock();
    if(isset($_GET['search[value]'])){
        $stock['data']=$this->Stock_model->get_stock($_GET['search[value]']);
    }
    $stock=json_encode($stock);
    print_r($stock);
}

stock model:

public function get_stock($search=null){
            $this->db->order_by('id','desc');
            if($search!=null){
                $this->db->like('p_name', $search);
                $stock=$this->db->get('stockdetail');
            }else{
                $stock=$this->db->get('stockdetail');
            }
            if($stock){
                return $stock->result_array();
            }
            return false;
}

Please help.

3
  • Check request response body in browser developer console what php return. Commented Dec 5, 2018 at 2:01
  • try change print_r to echo. Commented Dec 5, 2018 at 2:08
  • check out ignited datatables Commented Dec 5, 2018 at 7:21

2 Answers 2

1

try this:i hope it will help you. //js

 var dtable = $('#view-stock').DataTable({
            processing: true,
            serverSide: false,
            searching: true,
            dom: 'lBfrtip',
            lengthMenu: [[5, 10, 25, 50, 100, -1], [5, 10, 25, 50, 100, "All"]],
            pageLength: 10,
            buttons: [
                'csv', 'print'
            ],
            ajax: {
                "url": base_url + "/stock/datatables",
                'method': 'POST',
                'data': function (d) {
                    d._token = $("input[name=_token]").val();
                }
            }
//controller


 function index() {
        $data = array();
        $data['view'] = 'view_page_name';
      $this->load->view('header', $data);
    }
     function datatables() {
            $data = array();
            $stock =$this->Stock_model->get_stock($this->input->post());
            parent::json_output(["draw" => intval($this->input->post('draw')), "recordsTotal" => $stock[1], "recordsFiltered" =>$stock[1], "data" => $stock[0]]);
            return;
        }

//model

function get_stock($requestArray) {

        if (isset($requestArray['keyword']) && $requestArray['keyword'] != '') {
            $this->db->where .= " AND (field_name_by_which_you_want_search LIKE '%" . $requestArray['keyword'] . "%')";
            $this->db->where .= " AND (field_name_by_which_you_want_search LIKE '%" . $requestArray['keyword'] . "%')";
        }
        $this->db->where($where);
        if ($this->input->post('length') != '-1') {
            $this->db->limit($this->input->post('length'), $this->input->post('start'));
        }
 $this->db->order_by('id','desc');
        $query = $result->get('stockdetail')->result_array();

        $count = $result->get('stockdetail')->num_rows();
        return [$query, $count];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it helped me, Thanks.
0

$_GET is an array. 'search' is a key which contains another array so you've accessed the parameters incorrectly. Change this:

if(isset($_GET['search[value]'])){
    $stock['data']=$this->Stock_model->get_stock($_GET['search[value]']);
}

To:

if(isset($_GET['search']['value'])){
    $stock['data']=$this->Stock_model->get_stock($_GET['search']['value']);
}

Also, I'm not sure what your method returns but if you are doing server side processing you should make sure that it's a json in the proper format — which is roughly:

$json = json_encode(array(
    'data'              => $results,
    'draw'              => (int)$_GET['draw'],
    'recordsTotal'      => // total records in unfiltered result set,
    'recordsFiltered'   => // number of records returned
));

If your result set is empty you should still return results in this format and just send an empty array in the data key. Don't return false — this will cause DataTables to throw an error since it's an invalid response. Read up more on server-side processing in datatables here: https://datatables.net/manual/server-side

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.