Using jquery

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Using jquery

Post by shasta59 » 2015-02-09 17:52

Version 5.31 is what I am using - cannot verify the code with work with earlier version but it should

I have switched over to using the power of jQuery for many things now and will start posting those solutions.

Here is a teaser. I use the following lines of code, placed in the header.php file to turn some fields off or make them read only. Works really well. I place it just above the </head> tag. This way I can just have certain fields showing based upon the group. The example below just allows the admin to see all the fields. It turns some off or disables them based upon group. You can do it many ways. (I have left my field names in for ease of explanation)

I echo the lines to get them to work within the php script. I also put in the full word jQuery for ease of understanding. You could use the short form as seen a few lines above this in the header file if you look at your header.php file. Here is the line in the header.php file which gives you the variable to use to call jQuery in the short form. <script>var $j = jQuery.noConflict();</script>. It is what Ahmed has set for the noConflict option in jQuery.It should work but would not for me and I did not have time to figure out why.

What I do is wrap it in a php statement as following:

Code: Select all

<?php
if(getLoggedGroupID() != 2){
		
		echo "<script>jQuery(document).ready(function(){ jQuery('#paid0, #paid1, #paid2').attr('disabled', true);});</script>";
		echo "<script>jQuery(document).ready(function(){ jQuery('#datestopped').hide();});</script>";
		echo "<script>jQuery(document).ready(function(){ jQuery('#confirmed1, #confirmed2, #confirmed3, #confirmed4').attr('disabled', true);});</script>";
	}
		?>
Here is what it does line by line:

if(getLoggedGroupID() != 2){ - this line gets the group number of the person logged in and if not group 2 (two) executes the code below. It does this by calling the getLoggedGroupID() function.

echo "<script>jQuery(document).ready(function(){ jQuery('#paid0, #paid1, #paid2').attr('disabled', true);});</script>"; - this line calls jQuery and then when the document is ready - very important - it then finds fields paid0, paid1, paid2 and disables them. By only disabling them the end user can see them but not change their setting. Only someone with the correct permissions can change the setting (in this case a radio button). But is is important for the end user to see the setting.

echo "<script>jQuery(document).ready(function(){ jQuery('#datestopped').hide();});</script>"; - This line does the same thing but instead of disabling it hides the datestopped field from being seen.

The third line above is the same as the first.

A couple of points:

The only way I could get jQuery to run as a script was to echo it out of the php function. Works but I think there may be a better way.
Next it is very important how you put in the quotes. Notice where I have the double and single quotes. Would not work any other way.

Yes the code can be shorter and my real example is. I have combined multiple lines into one but felt this example was a better way to show it.I will be using the power of jQuery more and will post more examples and methods as I go along.

I did not try this as a hook as it worked so well in the header.php file.

Enjoy

As usual any questions just ask.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

User avatar
a.gneady
Site Admin
Posts: 1287
Joined: 2012-09-27 14:46
Contact:

Re: Using jquery

Post by a.gneady » 2015-02-12 06:14

Thanks for the tip, Alan.
<script>var $j = jQuery.noConflict();</script>. It is what Ahmed has set for the noConflict option in jQuery.It should work but would not for me and I did not have time to figure out why.
I guess it's because when you echo $j inside a PHP string, PHP attempts to parse it as a variable .. try escaping it like this: \$j
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: Using jquery

Post by shasta59 » 2015-02-12 14:26

Thanks Ahmed - I did not have enough time to try various options and yesterday morning put in that solution and it worked. Problem when you have too much on the go and not enough time to critically look at everything and find the optimum solution. I get stuck in needing a solution and I go with what comes to mind first to get something done. It became one of those things which was bugging me to find an answer because I knew there was one. I have to stop taking on last minute "we need it yesterday" tasks.

I will now remove that off my must post list since you have posted the solution.

Thanks
Calgary, Alberta, Canada - Using Appgini 5.50 -

Post Reply