Javascript templates and Excel export

Javascript templates and Excel export

Max Monclair -
回帖数:1

I need to set validation and required fields in an Add template for my Database activity. I looked for any documentation for a Javascript template to accomplish this, but only found this WIki that didn't have many details: http://docs.moodle.org/20/en/Database_templates. Is there any documentation out there, with some examples, that would be helpful? My Javascript is pretty rudimentary, so some pointers, especially in regards to templates for the Database activity, would be greatly appreciated.

Also, I need to customize the Excel output to order the fields in the way my client would like them, as he needs to drop the resulting output into a purchase order for our textbook supplier. The fewer steps he needs to do this, the fewer headaches for me. Could someone point me in the direction of the script that handles export to Excel format so I can see if this is a simple enough task for me? Eventually, I'd like to write a script to just render and email a purchase order to the supplier, and save our account manager some work, but first things first.

回复Max Monclair

Re: Javascript templates and Excel export

Itamar Tzadok -

Adding simple javascript to the Add template should be fairly simple. You can define functions in the js template and call them from the desired template. For example. Type in the js template something like:

function helloAddTemplate(){ alert('Hello add template');}

Then, in the Add template, in html mode, add something like:

<script type="text/javascript">
//<![CDATA[
helloAddTemplate();
//]]>
</script>

Now, if you want to access html elements on the page you can either pass their id to the javascript function in the template or traverse the DOM from within the function (provided you organize the template such that you know how to get the element). For the first approach you have field id tags which look something like ||fieldname#id||. A function call with a field id in the args may look something like:

<script type="text/javascript">
//<![CDATA[
helloAddTemplate('||fieldname#id||');
//]]>
</script>

Or you can get the element in the template script and pass it to the function:

<script type="text/javascript">
//<![CDATA[
var myField = document.getElementById('||fieldname#id||');
helloAddTemplate(myField);
//]]>
</script>

Etc.

 

The list of fields for the export is generated in export.php. So if you want to reorder it you should probably be able to do that by modifying the code there.

hth 微笑