⚠ 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

A better filter



Crashcode
  • profile picture
  • Member

Posted 17 December 2014 - 12:17 PM

Hello,

 

I just  want to show how to create a better filter. You only have to add a little bit more code and the filter will work more specify.

 

assets/grocery_crud/themes/flexigrid/views/list_template.php

 

Line: 127 to 153

<div class="sDiv quickSearchBox" id='quickSearchBox' >
		<div class="sDiv2" style="width: 80%;">
            <?php echo $this->l('list_search');?>:
            <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="wildcard" id="wildcard">
                <option value="equal">equal</option>
                <option value="not_equal">does not equal</option>
				<option value="begin">begins with</option>
				<option value="end">ends with</option>
				<option value="contain">contain</option>
				<option value="not_contain">does not contain</option>
			</select>
			 <input type="text" class="qsbsearch_fieldox search_text" name="search_text" size="30" id='search_text'>

			
            <input type="button" value="<?php echo $this->l('list_search');?>" class="crud_search" id='crud_search'>
		</div>
        <div class='search-div-clear-button'>
            
        	<input type="button" value="<?php echo $this->l('list_clear_filtering');?>" id='search_clear' class="search_clear">
        </div>
</div>

On the first picture you can see the few options.

[attachment=880:Options.jpg]

 

 

Now edit the file Grocery_CRUD.php.

 

Start at line 593.

 

application/libaries/Grocery_CRUD.php

				elseif(isset($this->relation_n_n[$state_info->search->field]))
				{
					$escaped_text = $this->basic_model->escape_str($state_info->search->text);
					$this->having($state_info->search->field." LIKE '%".$escaped_text."%'");
				}
				else
				{
				$wildcard = $_POST['wildcard'];
                                if($wildcard == "equal")
                                {
					$this->like($state_info->search->field , $state_info->search->text , 'none');
                                }
                                elseif($wildcard == "not_equal")
                                {
					$this->where($state_info->search->field.' !=', $state_info->search->text);
                                }
                                elseif($wildcard == "begin")
                                {
					$this->like($state_info->search->field , $state_info->search->text , 'after');
                                }
                                elseif($wildcard == "end")
                                {
					$this->like($state_info->search->field , $state_info->search->text , 'before');
                                }
                                elseif($wildcard == "contain")
                                {
					$this->like($state_info->search->field , $state_info->search->text);
                                }
                                elseif($wildcard == "not_contain")
                                {
					$this->where($state_info->search->field.' not like ', "%".$state_info->search->text."%");
                                }

				}

Thats all.

 

I wrote this because I didn't found something like this.

It isn't very much but I hope someone of you could use it for his own table. And I hope it will help somebody of you.


Amit Shah
  • profile picture
  • Member

Posted 18 December 2014 - 05:12 AM

Good effort-  some one might build something useful from this!!


Anten
  • profile picture
  • Member

Posted 15 May 2018 - 10:26 AM

@Crashcode, Thank you so much for your post. Helped me customize my code such that its similar to yours.

 

 

I had been a silent reader & silent user of grocerycrud for almost 2 years, and your code is what tipped me over.

Just wanna give credits where its due. Thanks guys!!


nurdiansyah
  • profile picture
  • Member

Posted 04 July 2019 - 02:17 AM

thank you sir, this is very helpful.