Page 1 of 1

Add date as prefix to attachments

Posted: 04 Apr 2016
by Sebastian Haugland
Hi

When a task has several attachment it is difficult to find the latest attachment and one of "my users" asked if I could add the upload date in front of the filename that is uploaded to the task.

I tried to change the attachment field but that did not work, any suggestions? (do I have to change the filename after saving the file?)

Re: Add date as prefix to attachments

Posted: 04 Apr 2016
by rostislav
Hi,

Your course of action would be:

1. Create a multi-line plain text field 'UploadedAttachmentsList'

2. Paste the following JS code onto the form:

Code: Select all

fd.onsubmit(function(){
	if (fd.field('Attachments').control()._el().find('tr[id^="attachRow"]').length) {
		var attachments = '';
		fd.field('Attachments').control()._el().find('tr[id^="attachRow"]').each(function(i, e){
			attachments += $(e).find('span').text().split('\\').slice(-1)[0] + '\n';
		});
		fd.field('UploadedAttachmentsList').value(fd.field('UploadedAttachmentsList').value() + attachments);
	}
	return true;
});

var attachments = fd.field('UploadedAttachmentsList').value().split('\n');
var found = false;
for (var i = attachments.length - 1; i >= 0 && !found; i --) {
	fd.field('Attachments').control()._el().find('tr span a').each(function(){
		if ($(this).text().trim() === attachments[i].trim()) {
			$(this).text('most recent ' + $(this).text());
			found = true;
			return false;
		} 
	});
} 
3. Add the UploadedAttachmentsList field onto the form and hide it with the following CSS (the style attribute):

Code: Select all

display:none; 
What all this will do is use a hidden text field to log the order of files added and add 'most recent' to the first attachment option on the list.

You can advance this piece of functionality by removing deleted files (in the onsubmit handler), adding date and time to the list.

You also need to test this feature thoroughly as it's not intended to be a complete solution, but only a guide to one.

Re: Add date as prefix to attachments

Posted: 04 Apr 2016
by Sebastian Haugland
with "Paste the following JS code onto the form:" do you mean under Javasript? ie the red button?

or as a html field?

Re: Add date as prefix to attachments

Posted: 04 Apr 2016
by rostislav
The Javascript button at the top of Forms Designer.