⚠ 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

Is there any callback for search fields ???



Peter

Peter
  • profile picture
  • Member

Posted 26 November 2014 - 12:28 PM

Please let me know if any callback function available for search field

 

I tried these two

$crud->callback_column('user_id',array($this,'get_username'));
$crud->callback_field('user_id',array($this,'get_username'));

function get_username returns username corresponds to integer user id, when I search suppose peter in search field in flexigrid it returns nothing, if suppose I search with userid say 1 , it returns all rows with username Peter

 

Please guys at least answer me to this post, my two old posts nobody answered even a author of GC, feeling so sad..

 

- Peter


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 26 November 2014 - 16:08 PM

No sir,... there aint any callback for search as such...

And the scenario you referring .. there aint any straight forward solution available for the same. You may look up to write a custom model yourself.. or

you may on the other hand take up a certain logic in the controller...

if state is search ...and the user is refering to the user_id field exclusively

you may exclusively reset the value to the user id based on the name (u may need to query the table to get the id against the name)

 

The only best solution to this is.. the one which u can derive to achieve the target.

 

Happy Gcing :)


Peter

Peter
  • profile picture
  • Member

Posted 27 November 2014 - 09:32 AM

Thank you master.


Akshay Hegde

Akshay Hegde
  • profile picture
  • Member

Posted 24 December 2014 - 14:29 PM

Hi ! Peter Thanks for your feedback on my first post, I don't know whether you could solve your problem or not, as Amit Shah said there is no straight forward solution

available, but it can be achieved something like this, I did it in one of my projects, might help you too :)

 

Please note since we are comparing numbers (user_id), this method may not give you good search results, for example if you search amit, it will return user_id 2, which is ok, suppose if you just search letter 'a', we actually expect it to list both amit and akshay, it will just return user_id 2 as it finds first in given array, so I will not tell that this method is best, its just a hint for you



In your controller create  function or array which contains user_id and and user name, which looks something like this

/* do your db stuffs and return array */
function _get_users()
{
	// Do your db stuffs here
	return array(
			1 => 'Peter',
			2 => 'Amit Shah',
			3 => 'Akshay',
			4 => 'John',
			5 => 'Dummy'
	);
}

and another small function which compares partially for example

/* Compare if there is any match partially return user id, else some bad numbers */ 
function compare_user($search=NULL)
{
    if(!is_null($search))
    {   
        foreach($this->_get_users() as $k => $v)
        {
            if(strpos(strtolower($v),strtolower($search)) !== false){
                    return $k;
            }
        }
    }   
        return -99;
}

Finally modify $_POST array, remaining things GC does :)

function test()
{
....
....

$crud->callback_column('user_id',array($this,'get_username'));
$crud->callback_field('user_id',array($this,'get_username'));

// If search field is user then do this
if(in_array($crud->getState(),array("ajax_list","ajax_list_info")) &&  array_key_exists("search_field",$this->input->post() ) )
{   
     if($this->input->post("search_field") === "user_id" && strlen($this->input->post("search_text")) )
     {   
            $_POST["search_text"] = $this->compare_user($_POST["search_text"]);
     }
}

....
....
$this->load->view('your_view',$crud->render());