Javascript get userid calc element always NULL

Mustafa_s

Member
Hi Guys,

I have the javascript snippet below that I'm using to hide specific form elements depending on the current logged on user. The Javascript is called OnLoad and is set on the ddp___Currentloggeduser element.

ddp___user is a user element that stores the user who created the record.
ddp___Currentloggeduser is a calc element that returns the current logged on user
ddp_12_repeat___attachments_delete_span is what I'm trying to hide.
Code:
$user =& JFactory::getUser();
return $user->get('username');
If the current logged on user matches the user who created the record than make the attachments element visibile, else make it hidden. For whatever the reason it's always hidden. Is this because my values in the DB are all NULL?

There must be a more elegant way to do this, surely I'm not being creative enough?
Code:
var user = Fabrik.getBlock('form_3').elements.get('ddp___user');
var currentuser = Fabrik.getBlock('form_3').elements.get('ddp___Currentloggeduser').getValue();
var attach1 = Fabrik.getBlock('form_3').elements.get('ddp_12_repeat___attachments_delete_span');
 
if (user==currentuser)
{
document.getElementById('ddp_12_repeat___attachments_delete_span').style.visibility='visibile';
}
else
{
document.getElementById('ddp_12_repeat___attachments_delete_span').style.visibility='hidden';
}
Q8p92Xa.png


Any guidance or comment is appreciated, this is the final peace of the puzzle for me.

Regards.
 
Code:
var user = Fabrik.getBlock('form_3').elements.get('ddp___user');
console.log('user: ' + user);
var currentuser = Fabrik.getBlock('form_3').elements.get('ddp___Currentloggeduser').getValue();
console.log('currentuser: ' + currentuser);
var attach1 = Fabrik.getBlock('form_3').elements.get('ddp_12_repeat___attachments_delete_span');

You should use console.log() to help debug your javascript code - see examples above. The console log would show the values and you'd know where you went wrong.

Look at the Element Javascript examples in the Wiki. http://fabrikar.com/forums/index.php?wiki/element-javascript-advanced/
You'll find that you never really set the value to var user - and used the wrong syntax to get the value for currentuser.

(And " ='visibile' " is misspelled, should be "visible" - http://www.w3schools.com/jsref/prop_style_visibility.asp)

Try this...
Code:
var user = Fabrik.getBlock('form_3').elements.get('ddp___user');
var userVal = user.get('value');
var currentuser = Fabrik.getBlock('form_3').elements.get('ddp___Currentloggeduser');
var currentuserVal = currentUser.get('value');
var attach1 = Fabrik.getBlock('form_3').elements.get('ddp_12_repeat___attachments_delete_span');
 
if (userVal == currentuserVal)
{
document.getElementById('ddp_12_repeat___attachments_delete_span').style.visibility='visible';
}
else
{
document.getElementById('ddp_12_repeat___attachments_delete_span').style.visibility='hidden';
}
And you may be able to get the values with one line of code, for example
Code:
var userVal = Fabrik.getBlock('form_3').elements.get('ddp___user').value;
You'd have to test that - I'm not really certain - as I mostly use jQuery for this sort of thing.
 
Bauer thanks a lot for the detailed information, not only did your snippet work on first try but I also learned of console.log() !! What a tool to troubleshoot :)
This function now opens a new level of access granularity for me.
 
We are in need of some funding.
More details.

Thank you.

Staff online

Members online

Back
Top