Javascript in Elements

Can you either PM me ftp details, or install JoomlaExplorer (or whatever) so I can edit the form JS?

I don't even see it being included on the form page any more (I'm looking at the form on the backend), so it's throwing a JS error 'cos the updateFoodStampPercent() funciton isn't defined.

It was there earlier today ... ?

-- hugh
 
OK, I just tested on the backend, and when I change either the total population or the recipients fields, the % field updates. I haven't saved anything, as I don't know if this is your live site or not.

Just to make sure your expectations are correct - the way this is set up, the % will only be calculated when either of the source values are changed. It won't automatically calculate that field just by opening the form. So to fix your existing entries, you'll have to go in and force a change by modifying one of the two source fields.

Oh, and here's what you need to do to round it down to 2 places:

Code:
var foo = ($('jos_fb_foodstamps___raw_stamps').getValue() / $('jos_fb_foodstamps___total_pop').getValue()) * 100;
$('jos_fb_foodstamps___percent_stamps').value = foo.toFixed(2);

-- hugh
 
Well the point being, you shouldn't need to. You only "need" it now because you have entered data with non-working JS that calculates the values when you input the data, so you have blank % fields.

Once the JS is working, the % values will just get stored with the form when you submit. No need to calc them when you open the form, as they'll already be there.

But if you really want to, you could add something like this to the form JS file:

Code:
window.addEvent('domready', function() {
        updateFoodStampPercent();
});
I just had another thought, which is that we probably need to add some code to the function to make sure the two source values have non-zero values, to avoid throwing divide by zero / NaN errors.

So the content of the func should probably look something like this:

Code:
var raw_stamps = parseInt($('jos_fb_foodstamps___raw_stamps').getValue());
var total_pop = parseInt($('jos_fb_foodstamps___total_pop').getValue());
if (total_pop != 0 && raw_stamps != 0) {
     var foo = (raw_stamps / total_pop) * 100;
     $('jos_fb_foodstamps___percent_stamps').value = foo.toFixed(2);
}
else {
     $('jos_fb_foodstamps___percent_stamps').value = '0.00';
}
-- hugh
 
Works like a charm! OK so I am finally starting to understand fabrik more. Javascript only works with the forms correct? How would you be able to do an update of all fields within a table at once?
 
With an SQL query. :)

I think you could just execute this in phpMyaAdmin / mysql / whatever you use:

UPDATE jos_fb_foodstamps SET percentage_stamps = (raw_stamps / total_pop) * 100;

Would suggest you try this on some throw away data in a test table first, though!

-- hugh
 
And yes, JavaScript is mostly commonly used with forms / elements.

There's nothing to stop you doing JS on tables, you just have to embed it by hand into a custom table template. But you can't modify actual SQL table data from a table view, without getting into AJAX/PHP programming. But it can be useful for GUI purposes, adding buttons, custom links, etc.

-- hugh
 
Very last question...thank you again for providing all this help...

In the initial inquiry I made about the javascript, I was also unsure how to use the prefilter to provide users with some type of dropdown menu (or some similar mechanism) that will allow filtering the data in the table that appears to show a specific year. We are going to be storing data back to 1999 and want to allow users to be able to switch between the data easily from that table (using the foodstamps table for example).

Is the prefilter syntax also going to be javascript?
 
You should be able to do it with a URL filter. Just append this to the URL to your table:

&table_name___time_date[value][1]=2003-01-0 00:00:01&table_name___time_date[value][2]=2003-12-31 23:59:59

This should filter it to dates in 2003.
You may need to replace the space with %20, or whatever the urlencoded value of space is.

And obviously you'll have to modify the table_name___time_date to match your table name and the date element name you are using. And adjust the time format I've used to match whatever format you are storing your dates as.

Note that we only recently added support for ranged URL filters, which only got minimal testing. It was working a month or so ago, but I haven't tested it recently, and we may have broken it since then. Just come on back if it doesn't work.

The only alternative to a ranged URL filter would be a simple URL filter, but that would only work if you have a date column which ONLY has the year in it. Then it would just be ...

&table_name___year[value]=2003

-- hugh


-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top