⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Search doesn't work with my callback_column



web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 09 January 2017 - 05:23 AM

Hello Community! As this is a known issue for many people, there is a known issue (also reported at: http://www.grocerycrud.com/documentation/known-issues)

 that it is bothering many people. The problem is that if we have a callback_column the search doesn't work as we are expecting to. I will give you an example:

 

Let's say we have the column_callback:
 

$crud->callback_column('fullname', function _callback_fullname($value, $row)
{
    return $row->first_name . ' ' . $row->last_name
});

 

The problem with the above code is that the search will not work. 

The only solution that I can give for that is a third parameter ? That we will have the columns to check. A simple solution could be something like that:

 

$crud->callback_column('fullname', function _callback_fullname($value, $row)
{
    return $row->first_name . ' ' . $row->last_name
}, '{first_name} {last_name}');

What do you think about that? As this will be an ugly work-around. Do you have any other idea of how this should be done?

Thanks
Johnny


Amit Shah
  • profile picture
  • Member

Posted 18 January 2017 - 03:06 AM

Well John, - i agree to your solution -it sems to be more prominent and a generic approach as the GC library itself will work its way out basis the columns it looks for. But there is a catch to the approach. Here we are expecting the output to be generated basis the column available in the same table :) ... that might be a very less possible situation. Many a times, users have tendency to call up some 3rd table to fetch up a column value -that time it will be difficult for us to actually evaluate as how to write a generic callback. Let me do one thing, i have had a similar situation and i have had written a callback around (surely it uses multiple columns - so it aint the perfect example ..) but it might just get a hands on for ones who are looking for the appropriate solution. Once i have hands back on the code, i will surely paste the sample code in here :)


Amit Shah
  • profile picture
  • Member

Posted 25 January 2017 - 13:43 PM

Sorry - got again lost in the project work.

 

Here is one of the piece of code where i worked for search with column callback.

<?php
	function lists() {

		if(isset($_POST['search_field']))  {
			$searchFields = $_POST['search_field'];
			$searchValues = $_POST['search_text'];
			$key = array_search('partner_assigned_to', $searchFields);
			if($key !== false) {
				$searchFields[$key] = 'emp_first_name';
				$_POST['search_field'] = $searchFields;
			}
		}
//Here partner assigned to is populated with column callback -- where the value going to be first_name / last_name of employee
//What used to happen was - when the user searched in for the text - GC library used to ignore the search on the partner assigned to -
// as it is not a field in actual. So what i did in here - if i found the search as option, and user tried to search a user in the 
//partner_assigned_to - i rather preferred to make user look up for the actual field. 

		$data = array();
		$this->load->library('grocery_crud');
		$CI =& get_instance();
		$CI->load->library("session");
		$CI->load->library("location");
		$session = $CI->session->userdata('logged_in');

		$crud = new Grocery_crud();
		$crud->set_theme('bootstrap');
		$crud->set_table('trade_partners');
		$crud->callback_column('partner_assigned_to',array($this,'_cb_col_full_name'));

		$crud->where('(partner_type = "KP+")');
		foreach ($this->config->item('display_as_trade_partners') as $fields) {
			list($field_name, $title) = $fields;
			$crud->display_as($field_name, $title);
		}

		if($crud->getState() == 'read')
			$crud->set_relation('created_by', 'user_profiles', '{upro_first_name} {upro_last_name}');

		if(isset($_POST['search_field']))  {
			$searchFields = $_POST['search_field'];
			$searchValues = $_POST['search_text'];
			$key = array_search('emp_first_name', $searchFields);
			if($key !== false) {
				$crud->or_like('trade_partners.emp_last_name', $searchValues[$key]);
			}
		}

//Now here if you notice, what i did was = looked up for the emp_first_name in searchfields list, (gc is anyways going to search for it) 
//but there was 1 more value that i wanted to search along with - the emp_last_name too .. so i added my or like condition

		$crud->order_by('id', 'desc');
		if($session['user_level']!=1) {
			$where = $this->location->getDataForList();
			$crud->where($where);
		}
		$crud->hide_search('shop_email');
		$crud->hide_search('mobile');

		$output = $crud->render();
		$data = buildData();

		$this->stencil->layout('crud_layout');
		$output->data=$data;
		$this->stencil->paint("crud.php", $output);
	} ?>

I am here just giving an example as how we can go ahead giving in desired solution. Every coder is a creative person who can come up with his / her creative solutions. This is just an example.