Welcome, Guest
Username: Password: Remember me

TOPIC: Create Search box

Create Search box 12 Jan 2012 06:28 #948

  • mossss
  • mossss's Avatar
  • Offline
  • Premium Member
  • Posts: 100
  • Thank you received: 7
  • Karma: 9
giori

I had the same problem with search boxes, but have found a solution. It may not be perfect but will perhaps get you through. It relies on you having an editor for your component with a good sitewide search and replace function. I use dreamweaver. (it is possible without the seach and replace function but will take a lot longer).


What I do.

1. On the table that you want to search on, I create a dummy enumertor type field. call it dummya (its important that this sequence of letters is not used anywhere else!)

2. go to the page that you want to put the search box on, can be any front end or back end collection view.

3. Add a combo box filter.

4. go to Fields and drag the dummya enumertor field into the filter

5. download your component

6. in your html editor (dreamweaver for me) do a search and replace. Make sure you have case sensitive selected. search for: dummya replace with: <yourfieldname>
(you must make sure that the field name is exactly as it is on the table). The search should find and replace many occurences (over 100 in my case)

7. then repeat the process but this time with search for DUMMYA and replace with <YOURFIELDNAME> (all capitals).
(in my case, this only finds one occurence. I don't know if this is important, but I do it anyway)

8. open the <yourpagename>_filters.php file and look for :
<?php echo JDom::_('html.form.input.select', array(
								'dataKey' => 'filter_yourfieldname',
								'dataValue' => $this->filters['yourfieldname']->value,
								'list' => $this->filters['yourfieldname']->list,
								'listKey' => 'value',
								'labelKey' => 'text'
									));

			?>


and change it to:
<?php echo JDom::_('html.form.input.text', array(              
								'dataKey' => 'filter_yourfieldname',
								'dataValue' => $this->filters['yourfieldname']->value,
								'list' => $this->filters['yourfieldname']->list,
								'listKey' => 'value',
								'labelKey' => 'text'
									));

			?>

(all you are doing is changing the word "select" to "text"

9. Then go to <your componentname>models<your tablename.php>

find the function
function _buildQueryWhere()
	{
		$where = array();

		$app = &JFactory::getApplication();
		$db=& JFactory::getDBO();
		$option	= JRequest::getCmd('option');
		$view	= JRequest::getCmd('view');
		$layout	= JRequest::getCmd('layout', 'default');

		$baseUserState = $option . '_' . $view . '.' . $layout . '.';

		if (isset($this->_active['filter']) && $this->_active['filter'])
		{


			$filter_yourfieldname = $this->getState('filter_yourfieldname');
			if ($filter_yourfieldname != '')		$where[] = "a.yourfieldname = " . $db->Quote $filter_yourfieldname);


then, assuming this is a VARCHAR field and you want to test on partial values, you change to:

function _buildQueryWhere()
	{
		$where = array();

		$app = &JFactory::getApplication();
		$db=& JFactory::getDBO();
		$option	= JRequest::getCmd('option');
		$view	= JRequest::getCmd('view');
		$layout	= JRequest::getCmd('layout', 'default');

		$baseUserState = $option . '_' . $view . '.' . $layout . '.';

		if (isset($this->_active['filter']) && $this->_active['filter'])
		{


			$filter_yourfieldname = $this->getState('filter_yourfieldname');
			if ($filter_yourfieldname != '')		$where[] = "a.yourfieldname LIKE " . $db->Quote("%" . $filter_yourfieldname . "%");


10. test

I now have several search boxes all on the same collection and it works. Its not perfect. I haven't quite worked out why, but sometimes, after clearing a search and putting in new search text, I then have to press the search button twice. But, its ok for my purposes until some better way comes along.

Hope it helps

**Update

There are a couple of additional steps needed to clean out some extra code.

1. This was causing filters on the table not to work correctly

Go to your front end component view folder for the table that you have added the search boxes. Open the view.html.php file. You will see one section for each of the search fields you have added that will look something like this
$lists['enum']['<your_field_name>'] = GardenHelper::enumList('<your_table_name>', '<your_field_name>');

Delete these lines.


2. This was causing an error when saving a new table item.

Open administrator->components-><your_component>->tables->your_tableitem.php

at the top there are lots of occurrences of

var $field_name = null;

At the bottom of the list you will find one entry for each search field you added. These are duplicates (as they were actually the dummy enumerator fields). Delete them. You can check that they are duplicates by looking in the list above and you should be able to find them.


After this, all seems to work ok.
Last Edit: 13 Jan 2012 15:29 by mossss.
The administrator has disabled public write access.

Re: Create Search box 12 Jan 2012 16:31 #957

  • crisvall
  • crisvall's Avatar
Nice try! I will use in my project. Maybe mix with some javascript for searchbox.
thanks for the tip!
The administrator has disabled public write access.
Time to create page: 0.162 seconds

Get Started