Document Title: Solution - Document Count in $$SearchView results
Author: Alan E Boyd on 03/09 at 06:33 PM
Category: Notes Designer -- Forms
Message Content:
I did some searching for the answer on this forum which is what led me to the solution I'm now going
to offer. The real credit goes to those who offered their ideas and answers in the past.
Getting a document count was simple once I found the answer, but as usual I wasn't satisfied with it. I
wanted a way to calculate the number of documents without having to count and subtract the number
of embedded links on the form prior to the $$ViewBody field since 100% of the HTML generated on
my search forms are stored in a database and called up with @DbColumn formulas. So, if I were to
change the stored HTML in a way that reduced or increased the number of links before the
$$ViewBody field, I'd have to edit the JavaScript on my search forms to reflect that change. Here is
how I made it automatic. Keep in mind that this pretty much sums up my knowledge of JavaScript, so
don't expect answers from me if this doesn't work for you. You can ask, but I can't make any
promises. All I know is it works wonderfully in my application.
On your $$SearchTemplate for [viewName] form, you want to include the following text as
passthrough HTML immediately prior to your $$ViewBody field:
<SCRIPT>
<!--
count = document.links.length
multiple = " documents found"
singular = " document found"
// --></SCRIPT>
$$ViewBody (this is the computed for display $$ViewBody field)
count, multiple, and singular are variables that I set. count = document.links.length counts the number
of URL links on the page just before the search results. After the $$ViewBody field, I included the
following in a computed for display field called documentCount (what you call it is up to you):
*** THIS IS WRONG! SEE BELOW FOR UPDATE. ***
"<p><font" + face2 + size2 + svcColor + "><SCRIPT>" + @NewLine +
"<!-- " + @NewLine +
"total = document.links.length - (count + 1)" + @NewLine +
"if (total < 0)" + @NewLine +
" total = 0" + @NewLine +
" if (total == 1)" + @NewLine +
" document.write(total + singular)" + @NewLine +
" else" + @NewLine +
" document.write(total + multiple)" + @NewLine +
"// --></script></font>"
*** THIS IS WRONG! SEE BELOW FOR UPDATE. ***
face2, size2, and svcColor are computed variables that I use for calling up font styles stored in the
database. You can replace this with your own hard coded font tags of your choice. I also used
@NewLine at the end of each line of text so the HTML code is formatted properly.
total = document.links.length - (count + 1)
This line sets the variable "total" equal to the number of URL links on the page through the end of your
search results, minus "count" (the number of URL links before the $$ViewBody field). For some
reason, there is a 2 count difference before and after the $$ViewBody field which is why we need to
subtract (count + 1) from the total. This will give you the actual number of search result links (or
documents).
The first "if" statement checks to see if "total" is equal to -1. This is necessary since the 2 count
difference I mentioned will cause "total" to be equal to -1 if 0 documents are returned. This statement
sets "total" to be equal to 0 if such is the case.
*** UPDATE ************************************
This was my fault. I hard coded the URL in the column formula while Notes was generating a 2nd
URL automatically. This is what caused the 2 count difference. Since the view I was working with had
only 1 document in it, I wasn't aware of my mistake. The correct code is as follows:
<SCRIPT>
<!--
total = document.links.length - count
if (total == 1)
document.write(total + singular)
else
document.write(total + multiple)
// --></script></b></font>
**********************************************
The "if" statement checks to see if "total" is equal to 1. If so, it displays the document count and the
text " document found." If "total" is not equal to 1, it displays the document count and the text "
documents found."
Hope you find this helpful. Good luck with your project(s).
Al
alan.boyd@worldresearch.com
****************8
Author:
Category:
Response to:
C Ku
on 03/09 at 08:37 PM
Notes Designer -- Forms
Solution - Document
Count in $$SearchView
results
Document Title: RE: Solution - Document Count in $$SearchView results
Message Content:
Thanks for the tip. I tried the technique, and it works great. Just a minor bug I found in the code. To
comment out the JavaScript code block, instead of
<!--
-->
it should be
<!-
//-->
I don't actually use this every time, since not many users are using browser without JavaScript
support. I use <NoScript></NoScript> sometimes.
Barry
previous page
|