add a word count to database activity

add a word count to database activity

David Campbell -
回帖数:14

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?

回复David Campbell

Re: add a word count to database activity

Itamar Tzadok -

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 Tzadok

Re: add a word count to database activity

David Campbell -

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.

回复David Campbell

Re: add a word count to database activity

Itamar Tzadok -

Any chance you're using 2.1? 微笑

回复Itamar Tzadok

Re: add a word count to database activity

David Campbell -

No, I am still on 1.9.10 for another 6 months or so.

回复David Campbell

Re: add a word count to database activity

Itamar Tzadok -

Where exactly do you want the word count to appear and words from what field type it is supposed to count? 微笑

回复Itamar Tzadok

Re: add a word count to database activity

David Campbell -

The field type is text area and I would like it to appear below this field in the View List and View Single, if possible.

回复David Campbell

Re: add a word count to database activity

Itamar Tzadok -

It would be helpful to see the html of your template. 微笑

回复Itamar Tzadok

Re: add a word count to database activity

David Campbell -

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.

回复David Campbell

Re: add a word count to database activity

Itamar Tzadok -

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;
}

微笑

回复Itamar Tzadok

Re: add a word count to database activity

David Campbell -

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.

回复David Campbell

Re: add a word count to database activity

Itamar Tzadok -

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. 微笑

回复David Campbell

Re: add a word count to database activity

Itamar Tzadok -

Here is a real word-count teaser. It shows word limit and counts the words as you type:

微笑

回复Itamar Tzadok

Re: add a word count to database activity

David Campbell -

That is nice! I am sure my students who only want to meet the minimum  number of word requirement would like it.