Script for Printing

Viewing 7 reply threads
  • Author
    Posts
  • July 14, 2020 at 6:51 AM #41279

    Roy Helsing
    Participant

    I am new to TAP, and made a simple relational database for a lost of music I can play, attached to the genres that relate to that music. I want to be able to print a report for my customers that lists each genre and under it, all song titles that are related tot hat genre. It appears the thinly way to do that iw with a script – which I read about at length yesterday only to decide Java Script is not in my wheel house. Does anyone have a simple script they use to list related items under a field that could share? I think i am able to figure out how to replace the fields in someone script with my one. I am looking for a report that reads like this:

    GENRE 1
    Song 1
    Song 15
    Song 432
    Genre 2
    Song 15
    Sonf 321
    Genre 3
    Song 1
    Song 12

    July 17, 2020 at 8:28 PM #41356

    Brendan
    Keymaster

    Hi Roy,

    Maybe you don’t need a script to do this. Have you tried just printing out the Default Layout and see what kind of output you get?

    However, you can create a new field and select Script as the Field Type. Then select your Song Title field from the list of fields on the Script Editor. Then double-click on the Child Records Loop Snippet and Tap Forms will write the code for you to loop through all of the Songs for the selected record. Then all you need to do is concatenate them together into a single string and return that.

    Something like:

    function SongTitles() {
    
    	var songs_id = 'fld-1ad42e9404674124aa6871c367249f1d';
    	var song_title_id = 'fld-177726d9d3b44645941e6d6488bcfbd1';
    	var songs = record.getFieldValue(songs_id);
    	var song_titles = [];
    	
    	for (var index = 0, count = songs.length; index < count; index++){
         	var song_title = songs[index].getFieldValue(song_title_id);
    		if (song_title) {
    			song_titles.push(song_title);
    		}
    	}
    	return song_titles.join("\n");
    }
    
    SongTitles();
    July 17, 2020 at 8:29 PM #41357

    Brendan
    Keymaster

    By the way, the only code I actually wrote there was the var song_titles = []; and the song_titles.push(song_title); and return song_titles.join("\n"); part. Tap Forms wrote all the rest when I double-clicked on the Child Records Loop snippet while I had the Song Title child field selected.

    July 20, 2020 at 9:44 AM #41397

    Victor Warner
    Participant

    Sorry to highjack this thread, but it would be very helpful for beginners (such as myself) to have a worked example.

    Would it be possible to provide a simple database showing an example of how this works, as I tried to follow the original’s posters requirement and adding a script field wit the code but I just get errors.

    July 20, 2020 at 12:02 PM #41401

    T.L. Ford
    Participant

    Victor,

    This looks like this. I modified Brendan’s code to add comments explaining each line of the Javascript. Working example attached.

    Brendan – hope you don’t mind me adding things. I’m happily enjoying exploring your Tap Forms application and am learning new and better ways to do things.

    – T

    Edit: Technically it’s not the id of the song form, but the “link to form” field on the Genre form.

    Attachments:
    You must be logged in to view attached files.
    July 20, 2020 at 12:19 PM #41406

    Brendan
    Keymaster

    T.L. I don’t mind one bit! I love it when customers help each other and it’s exciting for me to see what new things people want to use Tap Forms for. The scripting engine has been especially great because it opens up a whole world of possibilities for different ways to use Tap Forms.

    July 21, 2020 at 7:22 AM #41417

    Victor Warner
    Participant

    T,

    Thank you very much for providing the example – it helped me understand where I was going wrong in implementing Brendan’s example (failure to pick up the difference between Song Title (the form) and Song Titles (the field name)) – without reference to the database I did not appreciate the difference.

    I cannot say I understand some of the code but I should be able to implement it at least.

    One query on an addition. If I wished to display a second field in the resulting script, how would I do this?

    For example, if I added a time field (as a text field) and then instead of display, e.g.

    Barry Went Out
    Bee Bounce
    Party On

    but instead

    Barry Went Out, 1:30
    Bee Bounce, 2:30
    Party On, 1:30

    I understand how to get the id of the time field into the script (and how to write the code to format shown immediately above, just not how to get into this part of the script:

    	// create an array to store the songs in
    	var song_titles = [];
    
    	// loop through the songs records	
    	for (var index = 0, count = songs.length; index < count; index++){
    
    		// get the Song Title from the current record
         	var song_title = songs[index].getFieldValue(song_title_id);
    
    		// if a song title exists
    		if (song_title) {
    
    			// add the song title to the array
    			song_titles.push(song_title);
    

    Help with this part would be greatly appreciated. I attached the datebase with the added field.

    Attachments:
    You must be logged in to view attached files.
    July 21, 2020 at 8:27 AM #41419

    T.L. Ford
    Participant

    The for loop changes to add the time:

    	// loop through the songs records	
    	for (var index = 0, count = songs.length; index < count; index++){
    
    		// get the Song Title from the current record
         	var song_title = songs[index].getFieldValue(song_title_id);
         	
         	// get the song time from the current record
    		var song_time = songs[index].getFieldValue(time_id);
    
    		// if a song title exists
    		if (song_title) {
    
    			// if we have a time, go ahead and add it to the song_title string
    			if (song_time) {
    				// shorthand for  song_title = song_title + ", " + song_time
    				song_title += ", " + song_time;
    			}
    
    			// add the song title to the array
    			song_titles.push(song_title);
    			
    		}
    	}

    The javascript term is “string concatenation” – fancy for “adding two strings together” (strings are javascript’s way of saying text). While I’m at it… an “array” is javascript for “a list of something”.

Viewing 7 reply threads

You must be logged in to reply to this topic.