I would like to add a word count to the View List or View Single in a database activity that I am creating. I think it would be possible using the javascript template, but not sure how to go about it. Could I just copy a script from one of the JavaScript sites?
If the script knows what it is doing in your activity then yes you could put a script in the javascript template. Such a script would have to get the content of some container in the view count the words and put the result in another container for display. Or it could be a button which when clicked shows a message with the number of words. There are many ways to go about it.
Itamar,
Thanks for the reply. Your last sentence is a real "teaser". Do you think there would be easier or better ways to add a "word count" to a database activity? I would love some links or tips on how to go about it. I really don't know much about using Javascript, so anything really simple would be great.
html for View Single template
<table cellpadding="5"><tbody>
<tr>
<td style="vertical-align: top; text-align: right;"><span style="font-weight: bold;">Partner's Name:</span><br />
</td>
<td style="vertical-align: top;">Partner's Name
</td>
</tr>
<tr>
<td valign="top" style="font-weight: bold; text-align: right;">Partner's Info:
</td>
<td>Partner's Info
</td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;"><span style="font-weight: bold;">Topics:</span><br />
</td>
<td style="vertical-align: top;"> Topics
</td>
</tr>
<tr>
<td style="vertical-align: top;">##user##
</td>
<td style="vertical-align: top;"><br />
</td>
</tr>
<tr>
<td align="center" colspan="2">##timeadded## ##delete##
</td>
</tr></tbody>
</table>
html for View List template
<table cellpadding="5"><tbody>
<tr>
<td style="vertical-align: top; text-align: right;"><span style="font-weight: bold;">Partner's Name:</span><br />
</td>
<td style="vertical-align: top;">Partner's Name
</td>
</tr>
<tr>
<td valign="top" style="font-weight: bold; text-align: right;">Partner's Info:
</td>
<td>Partner's Info
</td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;"><span style="font-weight: bold;">Topics:</span><br />
</td>
<td style="vertical-align: top;"> Topics
</td>
</tr>
<tr>
<td style="vertical-align: top;">##user##
</td>
<td style="vertical-align: top;"><br />
</td>
</tr>
<tr>
<td align="center" colspan="2">##timeadded## ##delete##
</td>
</tr></tbody>
</table>
I haven't tried anything the Javaccript template yet.
Adding wordcount to the single view should be a bit simpler than the list view. First we need to identify the text which should be counted and add an identified container for the word count. We can also identify the container table so that we could do the wordcount directly from the javascript template. Suppose that the designated item is Partner's Info. The html may adjusted in the following way:
<table id="mySingleView" cellpadding="5"><tbody>
<tr>
<td style="vertical-align: top; text-align: right;"><span style="font-weight: bold;">Partner's Name:</span><br />
</td>
<td style="vertical-align: top;">[[Partner's Name] ]
</td>
</tr>
<tr>
<td valign="top" style="font-weight: bold; text-align: right;">Partner's Info:
</td>
<td id="myPartnerInfo">[[Partner's Info] ] (<span id="myWordCount"></span>)
</td>
...
Then in the javascript template we need to add something like the following:
if (document.getElementById('mySingleView')) {
var mySingleView = document.getElementById('mySingleView');
var myPartnerInfo = mySingleView.getElementById('myPartnerInfo');
var myWordCount = mySingleView.getElementById('myWordCount');
var text = myPartnerInfo.innerHTML.replace(/<[^>]+>/gi,'');
text = text.replace(/\s+/g,' ');
var wordCount = text.split(' ').length;
myWordCount.innerHTML = wordCount;
}
I have put in the additions to the html that you suggested and the script in the Javascript template, but I am not getting a number in the parenthesis after the text in Partner's Info. In looking at the template for SingleView the id="mySingleView" won't stay before the cellpadding="5". Whenever I save the template it moves after the cellpadding="5". I am not sure if that is the causing the problem, but it seems strange.
That's not a problem. It just arranges the attributes in alphabethical order.
Of course the whole thing doesn't work. Probably late night thing...
Here goes:
javascript template:
function singleViewWordCount() {
var myPartnerInfo = document.getElementById('myPartnerInfo');
var myWordCount = document.getElementById('myWordCount');
var text = myPartnerInfo.innerHTML.replace(/<[^>]+>/gi,'');
text = text.replace(/\s+/g,' ');
var count = text.split(' ').length;
myWordCount.innerHTML = '('+count+')';
}
Then the single template with editor disabled:
<tr>
<td valign="top" style="font-weight: bold; text-align: right;">Partner's Info:
</td>
<td><span id="myPartnerInfo">[ [Partner's Info] ]</span> <span id="myWordCount"></span>
</td>
</tr>
and at the bottom
<script type="text/javascript">
//<![CDATA[
singleViewWordCount();
//]]>
</script>
This time I checked so this should work.