ypnos-web / cakephp-datatables

CakePHP3 Plugin for DataTables plug-in for jQuery
MIT License
27 stars 24 forks source link

Searchable not working properly #63

Closed kkcodes closed 5 years ago

kkcodes commented 6 years ago

I have used the

'searchable' => false,

For a couple of columns, however, if I see the MySQL query for search, then I can see that it searches for the given query string for those columns as well, whose searchable is false.

Please provide some help regarding it.

ypnos-web commented 6 years ago

Please post your code and the query.

kkcodes commented 6 years ago

The Datatables going from the view:

$options = [
        'ajax' => [
            'url' => $this->Url->build(),
            'data' =>  $this->DataTables->callback("function ( d ) {
        d.filter_sport = $('#filter_sprt').val();
    }") // current controller, action, params
        ],
        'columns' => [
            [
             'name' => 'TableName.category_id',
              'data' => 'category_id',
              'searchable' => false,
            ],
            [
                'name' => 'TableName.title',
                'data' => 'cat',
                'searchable' => false,
            ],
            [
                'name' => 'TableName.media_url',
                'data' => 'images',
                 'render' => $this->DataTables->callback("function ( data, type, row ) {

                        if(data != null || data != undefined)
                        {
                            return '<img src=\"'+data+'\" height=\"50\" width=\"50\">';
                        }
                        else
                        {
                            return 'No Image Found';
                        }

                      }"),
                 'searchable' => false
             ],
            [
                 'name' => 'TableName.title',
                 'data' =>'name',
                 'searchable' => true,
            ],
            [
                 'name' => 'TableName.display_rank',
                 'data' =>'cat_rank',
                 'searchable' => false,
            ],
            [
                'name' => 'TableName.created_utc',
                'data' => 'sportdate',
                'render' => $this->DataTables->callback("function ( data, type, row ) {

               var d = new Date(data);
               var monthNames = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\",
                 \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"
               ];
               var curr_date = d.getDate();

               var curr_month = monthNames[d.getMonth()];

               var curr_year = d.getFullYear();

               curr_year = curr_year.toString().substr(2,2);

               return curr_date+\" \"+curr_month+\" \"+curr_year+\" \"+ d.toLocaleString('en-IN', { hour: 'numeric',minute:'numeric', hour12: true });;
                                  }")
            ],
            [
                'name' => 'TableName.question_id',
                'data' =>'question_cnt',
                'searchable' => false
            ]
],
'responsive' => true,
'dom' => '<"html5buttons"B>lTfgitp',
'buttons' => [
    [
        "extend" => "csv"
    ]
   ]
 ];

 echo $this->DataTables->table('table-name', $options, ['class' => 'table table-striped']);

Now the query has TableName.question_id like 's%', however above it the searchable is false for question_id above.

ypnos-web commented 6 years ago

Searchable is a property read by the component, not by the helper. It is the component that queries the ORM. You need to set this property in the columns you pass to the component's method.

Also it is good practice to carry-over the columns as defined in the controller to the view as a view variable. Have a look at my comment here on how this can be done in an elegant way, and you only have to define few of the colum properties in the controller.