Jump to content


Kenta Saito

Member Since 01 Mar 2012
Offline Last Active Aug 25 2012 12:30 PM
-----

#887 Multiple search conditions on flexigrid

Posted Kenta Saito on 22 March 2012 - 01:04 PM

Version:
grocery_CRUD_v1.2.zip

To search with multiple conditions, I modified 3 files in grocery CRUD.

multiple.png

Form interface:
1. assets/grocery_crud/themes/flexigrid/views/list_template.php

Original:
<select name="search_field">
<input type="text" name="search_text">

Mine:
<select name="search_field[]">
<input type="text" name="search_text[]">
<select name="search_type[]"> (">=", "<"... etc. "" for 'like')
* repeat above as array.



Search engine:
2. application/libraries/grocery_crud.php

Oridinal:
$state_info->search

Mine:
$state_info->search[] (as array)


Other little things:
3. assets/grocery_crud/themes/flexigrid/js/flexigrid.js

To deal with Cookie made me confused. I turn it off this time.
Search clear button will clear all text.




Form interface:
1. assets/grocery_crud/themes/flexigrid/views/list_template.php
diff -ru --strip-trailing-cr CodeIgniter_2.1.0.orig/assets/grocery_crud/themes/flexigrid/views/list_template.php CodeIgniter_2.1.0/assets/grocery_crud/themes/flexigrid/views/list_template.php
--- CodeIgniter_2.1.0.orig/assets/grocery_crud/themes/flexigrid/views/list_template.php 2012-03-15 21:27:32.000000000 +0900
+++ CodeIgniter_2.1.0/assets/grocery_crud/themes/flexigrid/views/list_template.php 2012-03-21 14:45:12.000000000 +0900
@@ -52,13 +52,41 @@
  <form action='<?php echo $ajax_list_url?>' method='post' id='filtering_form' autocomplete = "off" >
  <div class="sDiv" id='quickSearchBox'>
   <div class="sDiv2">
-   <?php echo $this->l('list_search');?>: <input type="text" class="qsbsearch_fieldox" name="search_text" size="30" id='search_text'>
-   <select name="search_field" id="search_field">
+   <select name="search_field[]" id="search_field">
	 <option value=""><?php echo $this->l('list_search_all');?></option>
	 <?php foreach($columns as $column){?>
	 <option value="<?php echo $column->field_name?>"><?php echo $column->display_as?>&nbsp;&nbsp;</option>
	 <?php }?>
	</select>
+   <select name="search_type[]" id="search_type">
+	<option value="">like</option>
+	<option value="=">=</option>
+	<option value="&lt;&gt;">&lt;&gt;</option>
+	<option value="&lt;=">&lt;=</option>
+	<option value="&lt;">&lt;</option>
+	<option value="&gt;=">&gt;=</option>
+	<option value="&gt;">&gt;</option>
+	<option value="&lt;=&gt;">&lt;=&gt;</option>
+   </select>
+   <input type="text" class="qsbsearch_fieldox" name="search_text[]" size="30" id='search_text'>
+   <br />
+   <select name="search_field[]" id="search_field">
+	<option value=""><?php echo $this->l('list_search_all');?></option>
+	<?php foreach($columns as $column){?>
+	<option value="<?php echo $column->field_name?>"><?php echo $column->display_as?>&nbsp;&nbsp;</option>
+	<?php }?>
+   </select>
+   <select name="search_type[]" id="search_type">
+	<option value="">like</option>
+	<option value="=">=</option>
+	<option value="&lt;&gt;">&lt;&gt;</option>
+	<option value="&lt;=">&lt;=</option>
+	<option value="&lt;">&lt;</option>
+	<option value="&gt;=">&gt;=</option>
+	<option value="&gt;">&gt;</option>
+	<option value="&lt;=&gt;">&lt;=&gt;</option>
+   </select>
+   <input type="text" class="qsbsearch_fieldox" name="search_text[]" size="30" id='search_text'>
			 <input type="button" value="<?php echo $this->l('list_search');?>" id='crud_search'>
   </div>
		 <div class='search-div-clear-button'>


Search engine:
2. application/libraries/grocery_crud.php
diff -ru --strip-trailing-cr CodeIgniter_2.1.0.orig/application/libraries/grocery_crud.php CodeIgniter_2.1.0/application/libraries/grocery_crud.php
--- CodeIgniter_2.1.0.orig/application/libraries/grocery_crud.php 2012-03-15 21:27:32.000000000 +0900
+++ CodeIgniter_2.1.0/application/libraries/grocery_crud.php 2012-03-21 14:46:38.000000000 +0900
@@ -473,44 +473,88 @@
	 foreach($this->relation as $relation_name => $relation_values)
	  $temp_relation[$this->_unique_field_name($relation_name)] = $this->_get_field_names_to_search($relation_values);
  
-   if($state_info->search->field != null)
+   foreach ($state_info->search as $search)
	{
-	if(isset($temp_relation[$state_info->search->field]))
+	$fields = array();
+	if ($search->type === '')
	 {
-	 if(is_array($temp_relation[$state_info->search->field]))
-	  foreach($temp_relation[$state_info->search->field] as $search_field)
-	   $this->or_like($search_field , $state_info->search->text);
+	 if($search->field != null)
+	 {
+	  if(isset($temp_relation[$search->field]))
+	  {
+	   if(is_array($temp_relation[$search->field]))
+	   {
+		foreach($temp_relation[$search->field] as $search_field)
+		 $fields[] = $search_field;
+	   }
+	   else
+		$fields[] = $temp_relation[$search->field];
+	  }
+	  else
+	  {
+	   $fields[] = $search->field;
+	  }
+	 }
+	 else
+	 {
+	  $columns = $this->get_columns();
+	
+	  #region temporary solution for the search with relation_n_n
+	  if(!empty($this->relation_n_n))
+	   foreach($columns as $num_row => $column)
+		if(isset($this->relation_n_n[$column->field_name]))
+		 unset($columns[$num_row]);
+	  #endregion
+	
+	  foreach($columns as $column)
+	   if(isset($temp_relation[$column->field_name]))
+		if(is_array($temp_relation[$column->field_name]))
+		 foreach($temp_relation[$column->field_name] as $search_field)
+		  $fields[] = $search_field;
+		else
+		 $fields[] = $temp_relation[$column->field_name];
+	   else
+		$fields[] = $column->field_name;
+	 }
+	 if (count($fields) == 1)
+	 {
+	  $this->like($fields[0], $search->text);
+	 }
	  else
-	  $this->like($temp_relation[$state_info->search->field] , $state_info->search->text);
+	 {
+	  $this->like('concat('.implode(', " ", ', $fields).')', $search->text);
+	 }
	 }
-	else
+	else
	 {
-	 $this->like($state_info->search->field , $state_info->search->text);
+	 if($search->field != null)
+	 {
+	  if(isset($temp_relation[$search->field]))
+	  {
+	   if(is_array($temp_relation[$search->field]))
+	   {
+		foreach($temp_relation[$search->field] as $search_field)
+		 $fields[] = $search_field;
+	   }
+	   else
+		$fields[] = $temp_relation[$search->field];
+	  }
+	  else
+	  {
+	   $fields[] = $search->field;
+	  }
+
+	  if (count($fields) == 1)
+	  {
+	   $this->where($fields[0].' '.$search->type, $search->text);
+	  }
+	  else
+	  {
+	   $this->where('concat('.implode(', " ", ', $fields).')'.' '.$search->type, $search->text);
+	  }
+	 }
	 }
	}
-   else
-   {
-	$columns = $this->get_columns();
-  
-	#region temporary solution for the search with relation_n_n
-	if(!empty($this->relation_n_n))
-	 foreach($columns as $num_row => $column)
-	  if(isset($this->relation_n_n[$column->field_name]))
-	   unset($columns[$num_row]);
-	#endregion
-  
-	$search_text = $state_info->search->text;
-  
-	foreach($columns as $column)
-	 if(isset($temp_relation[$column->field_name]))
-	  if(is_array($temp_relation[$column->field_name]))
-	   foreach($temp_relation[$column->field_name] as $search_field)
-		$this->or_like($search_field, $search_text);  
-	  else
-	   $this->or_like($temp_relation[$column->field_name], $search_text);	
-	 else
-	  $this->or_like($column->field_name, $search_text);
-   }
   }
  }

@@ -2309,19 +2353,25 @@
	 {
	  $state_info->order_by = $_POST['order_by'];
	 }
-	if(!empty($_POST['search_text']))
+	if (isset($_POST['search_text']))
	 {
-	 if(empty($_POST['search_field']))
+	 for ($i = 0; $i < count($_POST['search_text']); $i++)
	  {
+	  if(!empty($_POST['search_text'][$i]))
+	  {
+	   if(empty($_POST['search_field'][$i]))
+	   {
	  
-	  $search_text = strip_tags($_POST['search_field']);
+		$search_text = strip_tags($_POST['search_field'][$i]);
	  
-	  $state_info->search = (object)array( 'field' => null , 'text' => $_POST['search_text'] );
+		$state_info->search[] = (object)array( 'field' => null , 'text' => $_POST['search_text'][$i] , 'type' => $_POST['search_type'][$i] );
	  
-	 }
-	 else
-	 {
-	  $state_info->search = (object)array( 'field' => strip_tags($_POST['search_field']) , 'text' => $_POST['search_text'] );
+	   }
+	   else
+	   {
+		$state_info->search[] = (object)array( 'field' => strip_tags($_POST['search_field'][$i]) , 'text' => $_POST['search_text'][$i] , 'type' => $_POST['search_type'][$i] );
+	   }
+	  }
	  }
	 }
	break;


Other little things:
3. assets/grocery_crud/themes/flexigrid/js/flexigrid.js
diff -ru --strip-trailing-cr CodeIgniter_2.1.0.orig/assets/grocery_crud/themes/flexigrid/js/flexigrid.js CodeIgniter_2.1.0/assets/grocery_crud/themes/flexigrid/js/flexigrid.js
--- CodeIgniter_2.1.0.orig/assets/grocery_crud/themes/flexigrid/js/flexigrid.js 2012-03-15 21:27:32.000000000 +0900
+++ CodeIgniter_2.1.0/assets/grocery_crud/themes/flexigrid/js/flexigrid.js 2012-03-21 00:04:34.000000000 +0900
@@ -41,12 +41,12 @@
	 }
   });
  
-  createCookie('crud_page_'+unique_hash,crud_page,1);
-  createCookie('per_page_'+unique_hash,$('#per_page').val(),1);
-  createCookie('hidden_ordering_'+unique_hash,$('#hidden-ordering').val(),1);
-  createCookie('hidden_sorting_'+unique_hash,$('#hidden-sorting').val(),1);
-  createCookie('search_text_'+unique_hash,$('#search_text').val(),1);
-  createCookie('search_field_'+unique_hash,$('#search_field').val(),1);
+//  createCookie('crud_page_'+unique_hash,crud_page,1);
+//  createCookie('per_page_'+unique_hash,$('#per_page').val(),1);
+//  createCookie('hidden_ordering_'+unique_hash,$('#hidden-ordering').val(),1);
+//  createCookie('hidden_sorting_'+unique_hash,$('#hidden-sorting').val(),1);
+//  createCookie('search_text_'+unique_hash,$('#search_text').val(),1);
+//  createCookie('search_field_'+unique_hash,$('#search_field').val(),1);
  
   return false;
  });
@@ -58,7 +58,8 @@

  $('#search_clear').click(function(){
   $('#crud_page').val('1');
-  $('#search_text').val('');
+  $("[name='search_text[]']").val('');
+
   $('#filtering_form').trigger('submit');
  });