Page 1 of 1

Make Filters as buttons

Posted: 2014-01-29 22:30
by scorpio_x73
It would be nice when you save a filter to have the option to assign this filter to a a button in the relevant table (it can have 3 default places for filters so we can assign the filter we ant to one of the three buttons)

Thank you again.

Re: Make Filters as buttons

Posted: 2014-03-01 08:40
by a.gneady
You can use the table_header or table_footer hooks to insert the button into the page. In future releases, I plan to add an option to allow users to "bookmark" filters for easy access later.

Re: Make Filters as buttons

Posted: 2014-07-13 13:24
by xithyl
Dear a.gneady

Can you please explain how can we add custom filters as a buttons to specific table views (table_header) ? It would be nice to see the example of that.

Thank you.

Re: Make Filters as buttons

Posted: 2014-07-13 21:33
by a.gneady
I'll try to write a detailed tutorial on this topic ... but here is a starting point ... in the generated "hooks" folder, create a file and name it "tablename-tv.js" (where tablename is the name of the table you want to add the button in) ... this file will be automatically executed in the table view of the table. Add this JavaScript code to the file:

Code: Select all

jQuery(function(){
	jQuery('#Filter').after('<button class="btn btn-default" type="button" id="foebar"><i class="glyphicon glyphicon-ok"></i> Foe bar</button>');
        jQuery('button[id=foebar]').click(function(){
              // do something when the user clicks this button
        });
});
The above code would add a new button next to the "Filter" button and label it "Foe bar" .. I hope this would get you started.

Re: Make Filters as buttons

Posted: 2014-07-14 10:32
by xithyl
a.gneady wrote:I'll try to write a detailed tutorial on this topic ... but here is a starting point ... in the generated "hooks" folder, create a file and name it "tablename-tv.js" (where tablename is the name of the table you want to add the button in) ... this file will be automatically executed in the table view of the table. Add this JavaScript code to the file:

Code: Select all

jQuery(function(){
	jQuery('#Filter').after('<button class="btn btn-default" type="button" id="foebar"><i class="glyphicon glyphicon-ok"></i> Foe bar</button>');
        jQuery('button[id=foebar]').click(function(){
              // do something when the user clicks this button
        });
});
The above code would add a new button next to the "Filter" button and label it "Foe bar" .. I hope this would get you started.
a.gneady; Thank you for your quick reply. By following your explanations above, I added the buttons without any problems. Now how can i trigger the saved filters? Sorry, but my php knowledge is zero. Should i use the following code:

Code: Select all

<form method="post" action="http://localhost/deb/let_view.php">
	<input name="FilterAnd[1]" value="" type="hidden">
	<input name="FilterField[1]" value="4" type="hidden">
	<input name="FilterOperator[1]" value="like" type="hidden">
	<input name="FilterValue[1]" value="aaa" type="hidden">


	<input type="submit" value="Show Filtered Data">
</form>
Thanks in advance for your reply.

Re: Make Filters as buttons

Posted: 2014-07-15 00:36
by a.gneady
Replace

Code: Select all

// do something when the user clicks this button
with:

Code: Select all

window.location = 'let_view.php?FilterField%5B1%5D=4&FilterOperator%5B1%5D=like&FilterValue%5B1%5D=aaa';

Re: Make Filters as buttons

Posted: 2014-07-16 10:31
by xithyl
a.gneady wrote:Replace

Code: Select all

// do something when the user clicks this button
with:

Code: Select all

window.location = 'let_view.php?FilterField%5B1%5D=4&FilterOperator%5B1%5D=like&FilterValue%5B1%5D=aaa';
a.gneady, thanks for your quick response and for this software. :)

Re: Make Filters as buttons

Posted: 2015-05-24 13:56
by tmathew
I have added few buttons after 'Add New' in tablename-tv.js. Works perfectly ok. But when I reduced the size of window to check how it appears on Tablets & Mobiles, all newly added buttons disappeared and only the 'Add New' button displayed.

Now I made the 'Add New' button hidden and checked. Surprisingly the display is exactly same as before -- means it displayed 'Add New' button alone, even though I made it hidden. Its displaying perfectly ok (without the hidden AddNew button) when the window is maximized.

Is it due to the absence of any include files in the tablename-tv.js file? Or did I miss out something else?

An immediate advice/help will be highly appreciated.

Re: Make Filters as buttons

Posted: 2015-07-22 10:20
by scorpio_x73
Ahmad thank you for all the replies this piece of code is SUPER.

But we still have problem with the responsiveness. When we make the screen smaller the buttons are disappear .

Do we have to make changes somewhere else also ?

Thank you in advance

Re: Make Filters as buttons

Posted: 2015-10-13 22:21
by gonzalo
Hi:

Any further developments on this mater? I tried using this and I can't get it to work - I get an error when the window.location line is executed. My statement is like this:

window.location = 'let_view.php?FilterField%5B1%5D=9&FilterOperator%5B1%5D=like&FilterValue%5B1%5D="In Use"';

I found a solution in another post that seems quite elegant, but my limited PHP / JS abilities do not allow me to get it to work.

http://test.spgon.in/filtering/Tasks_view.php

Re: Make Filters as buttons

Posted: 2015-10-13 23:59
by peebee
I tried using this and I can't get it to work - I get an error when the window.location line is executed. My statement is like this:

window.location = 'let_view.php?FilterField%5B1%5D=9&FilterOperator%5B1%5D=like&FilterValue%5B1%5D="In Use"';
If that is the exact code you are using, you will need to change 'let_view.php? to the table table name concerned (unless of course your table name is actually named 'let')

(i.e.: window.location = 'YOURTABLENAMEHERE_view.php?FilterField%5B1%5D=9&FilterOperator%5B1%5D=like&FilterValue%5B1%5D="In Use"')

You will likely also need to change the specific filter fields & filter operators depending on the result you are after?

The code example provided by Ahmad is specific to the requirements of the op who asked the question.

Re: Make Filters as buttons

Posted: 2015-10-14 15:03
by gonzalo
Thanks - worked perfectly!!

Re: Make Filters as buttons

Posted: 2015-12-16 12:56
by Authen-TIC
Hi Gonzalo,

Have you be able to do like this example, Can you share your solution.

http://test.spgon.in/filtering/Tasks_view.php

Thanks!

Re: Make Filters as buttons

Posted: 2021-04-30 13:26
by A Bindi
a.gneady wrote:
2014-07-15 00:36
Replace

Code: Select all

// do something when the user clicks this button
with:

Code: Select all

window.location = [size=150][b]'let_view.php?FilterField%5B1%5D=4&FilterOperator%5B1%5D=like&FilterValue%5B1%5D=aaa';[/b][/size]
What means exactly the row 'FilterField%5B1%5D=4&FilterOperator%5B1%5D=like&FilterValue%5B1%5D=aaa' ?

ALex.

Re: Make Filters as buttons

Posted: 2021-04-30 16:05
by pfrumkin
Hi Alex,

That is the querystring, effectively passing in values to the php script. It is telling the php script how to perform the SQL query, defining the WHERE clause, WHERE field #4 LIKE 'aaa'.

I think it is the SortFields array in the tablename_view.php that maps the field # to the field in the table.

The %5B and %5D are html encoding for characters, so that the browser passes those characters to the script literally rather than interpreting them. See something like https://en.wikipedia.org/wiki/Percent-encoding for more explanation.

Hope that helps.

~Paul

Re: Make Filters as buttons

Posted: 2021-05-02 06:55
by onoehring
Hi,

if you want to generate JS from within PHP, I find it useful to call the following function to make sure, all characters are properly escaped. Please see the source ( https://gist.github.com/Chengings/9599473 ) for more information.

Code: Select all

/**
* Escape php string to javascript string
* @param string $str
* @return string escaped string
* src: https://gist.github.com/Chengings/9599473
*/
function escape_javascript_string($str){
	// if php supports json_encode, use it (support utf-8)
	if (function_exists('json_encode')) {
		return json_encode($str);
	}
	// php 5.1 or lower not support json_encode, so use str_replace and addcslashes
	// remove carriage return
	$str = str_replace("\r", '', (string) $str);
	// escape all characters with ASCII code between 0 and 31
	$str = addcslashes($str, "\0..\37'\\");
	// escape double quotes
	$str = str_replace('"', '\"', $str);
	// replace \n with double quotes
	$str = str_replace("\n", '\n', $str); 
	return "'{$str}'";
}
/* for testing:
$test_case = array(
	'',
	"https://demo/path/download.html?q=query",
	"~!@#$%^&*(){}[]`/=?+\|-_;:'\,<.>",
	'These are the problems => . \ \' \\ \\\\ \\\\\\ \\\\\\\\ "',
);
foreach ($test_case as $test_str) {
	print '<script>';
	print 'alert(' . escape_javascript_string($test_str) . ')';
	print '</script>';
}
*/
Olaf