This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Apr 6, 2012, 6:03 PM
45 Posts
topic has been resolvedResolved

Display document category and number of documents in Repeat Control.

  • Category: Server Side JavaScript
  • Platform: All
  • Release: 8.5.3
  • Role: End user
  • Tags:
  • Replies: 8
Hi
 
I have a several requirements for displaying the 'top 10' customers for a given criteria in Repeat Controls.  A basic example being our customer support application, i.e. Fault record with a Customer Field etc etc. I would like to display on an XPage the top 10 customer by number of faults logged displayed in descending order. This would then lead to other applications like top 10 customers by sales value, i.e. all invoices values for a particular customer added up then displayed in descending order etc.
 
So far I have created a view and  displayed the customers in a repeat by using @Unique on and @DbColumn, then adding a ViewEntryCollection Count to get the number of documents per customer. This is okay but I then do not have a way of finding the top 10 and displaying in descending order.
 
I have tried to get the underlying view just showing categories and with a total column, but I can't get that to sort on the total per category for further use in the repeat.
 
I would have thought this application was very common but my research has come to nothing, tutorials and the like do not seem to cover it. If someone could point me in the right direction it would be greatly appreciated.
 
Many thanks.   
 
 
Apr 6, 2012, 8:26 PM
17 Posts
Re: Display document category and number of documents in Repeat Control.
Mark,
you can, for example, build an array of JavaScript objects based on your data from NotesViewEntries.
Then simply call Array.sort() + you can also provide comparator function if you wish.
Then you can either create a subarray with only 10 elements or just be lazy and set the number of rows in the repeat control to 10 and do not provide any pager controls :-).
Of course there are more methods to use ...
 
HTH,
Rick.
 
Apr 6, 2012, 9:40 PM
45 Posts
Re: Display document category and number of documents in Repeat Control.
Thanks very much for the direction Rick. I thought I may need to get in to javascript arrays and the like, not my strong point, if anyone has any samples it would be great, if not, no problem I am sure it will be a good learning experience.
Apr 7, 2012, 12:49 PM
17 Posts
Re: Display document category and number of documents in Repeat Control.
Hi Mark,
I like your attitude :-). The code sample is here:
 
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:viewPanel rows="30" id="viewPanel1" var="person">
        <xp:this.value><![CDATA[#{javascript:
var values = []; // create an empty array
var obj1 = {};     // create an empty object
obj1.firstName = "John"; // add attributes
obj1.lastName = "Doe";
values.push(obj1); // add to array

var obj2 = {};
obj2.firstName = "David";
obj2.lastName = "Doe";
values.push(obj2);

// define comparator function, sorts by lastname + firstname
var comparator = function(a,b){
    var v1 = a.lastName + " " + a.firstName;
    var v2 = b.lastName + " " + b.firstName;
    return v1.compareTo(v2);
}
values.sort(comparator)}]]></xp:this.value>
        <xp:viewColumn columnName="view1" id="viewColumn1"
            value="#{person.firstName}">
        </xp:viewColumn>
            <xp:viewColumn columnName="view1" id="viewColumn2"
            value="#{person.lastName}">
        </xp:viewColumn>
    </xp:viewPanel>
</xp:view>
Apr 7, 2012, 3:42 PM
45 Posts
Re: Display document category and number of documents in Repeat Control.
Thanks again Rick,
 
I think I am not on the right track now. I need to count (for example) how many 'tickets' have been raised for a given customer, each ticket is a document in a view, determine which 10 customers have raised the most tickets in descending order, then display each of the 10 as a row in a Repeat Control along with any further data from a document, e.g customer name, contact.
 
 E.g
 
Bob Smith      Acme Co   150 
Fred Bloggs  Blogs Co    125
Ste  Wright     Wright Co   115
etc.
 
 
 
The repeat so far looks like this without any sorting. Can you spot where it is wrong?
 
  <xp:repeat id="repeat1" rows="10" var="rowDataSReqs">
<xp:this.value><![CDATA[#{javascript:var dbname = new Array("Titan","NCLCusto.nsf");
var users = @Unique(@DbColumn(dbname,"$xPages Fault By User",1));
var db:NotesDatabase = session.getDatabase("Titan", "NCLCusto.nsf", false);
var v:NotesView = db.getView("$xPages Fault By User");
var x;
var listarray = [];
var obj1 = {};
               
for (x in users)
{
obj1.numbercount = v.getAllEntriesByKey(users[x]).getCount();
obj1.username = users[x];
listarray.push(obj1);
};

return listarray;    
                }]]></xp:this.value>
<xp:text escape="true" id="computedField1"
value="#{javascript:rowDataSReqs.numbercount}">
</xp:text>

<xp:text escape="true" id="computedField2"
value="#{javascript:rowDataSReqs.username}">
</xp:text>

<xp:br></xp:br>
        </xp:repeat>
Apr 8, 2012, 8:04 AM
17 Posts
Re: Display document category and number of documents in Repeat Control.
Hi Mark,
and what is your question, please? Why are the records unsorted?
Please see the example above...
 
You must sort the array first. To override the default sorting mechanism for the array, please define a comparator function.
This function is called for any two records when the array is being sorted.
 
and you would return the values to be displayed in the repeat control like this:
return listarray.sort(comparator);
 
 
 
 
Apr 8, 2012, 12:35 PM
45 Posts
Re: Display document category and number of documents in Repeat Control.
Hi Rick,
 
Sorry, I removed the comparitor just to try to break it down as I could not achieve the desired results. 
I am experiencing two problems. The count simply shows 1.0 for every row rather than the count of each collection and the user name does not display at all.
 
Thank you for your patience, I know it is something simple or stupid that I am doing wrong, but for the life of me...  trust me I have tried to solve the problem ;-).
 
        <xp:repeat id="repeat1" rows="100" var="rowDataSReqs">
            <xp:this.value><![CDATA[#{javascript:var dbname = new Array("Titan","NCLCusto.nsf");
var users = @Unique(@DbColumn(dbname,"$xPages Fault By User",1));
var db:NotesDatabase = session.getDatabase("Titan", "NCLCusto.nsf", false);
var v:NotesView = db.getView("$xPages Fault By User");
var x;
var listarray = [];
var obj1 = {};
                
for (x in users)
{
obj1.numbercount = v.getAllEntriesByKey(users[x]).getCount();
obj1.username = users[x];
listarray.push(obj1);
};


var comparator = function(a,b){
    var v1 = a.numbercount + " " + a.username
    var v2 = b.numbercount + " " + b.username
    return v1.compareTo(v2);
};
return listarray.sort(comparator)


}]]></xp:this.value>

            <xp:text escape="true" id="computedField2"
                value="#{javascript:rowDataSReqs.username}">
            </xp:text>
            
            <xp:text escape="true" id="computedField1"
                value="#{javascript:rowDataSReqs.numbercount}">
            </xp:text>
            
            <xp:br></xp:br>
        </xp:repeat>
Apr 8, 2012, 2:20 PM
17 Posts
Re: Display document category and number of documents in Repeat Control.
Hi Mark,
please be careful when:
- using the same javascript object - you are changing the same object and adding it to the array, and as a result, the values are the same
- defining the comparator, by casting values to strings, you are comparing strings so 1, 11 are before 2.
 
Try this:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:viewPanel rows="30" id="viewPanel1" var="person">
        <xp:this.value><![CDATA[#{javascript:var values = []; // create an empty array
var users = @Unique(@DbColumn("", "Bugs", 1));
var db = session.getCurrentDatabase();
var view1 = db.getView("Bugs");
print("\ntotal count: " + users.length);
for (i = 0; i < users.length; i++){
    var obj = {};     // create an empty object
    obj.user = users[i];
    obj.numbercount = view1.getAllDocumentsByKey(obj.user, true).getCount();
    //debug if you wish print("\nuser: " + obj.user + ", count: " + obj.numbercount);
    values.push(obj);
}


// define comparator function, sorts by lastname + firstname
var comparator = function(a,b){
    var v1 = a.numbercount;
    var v2 = b.numbercount;
    if (v1 > v2)
        return -1;
    else if (v1 < v2)
        return 1;
    else
        return (a.user).compareTo(b.user);    
}

values.sort(comparator)}]]></xp:this.value>
        <xp:viewColumn columnName="view1" id="viewColumn1"
            value="#{person.user}">
        </xp:viewColumn>
        <xp:viewColumn columnName="view1" id="viewColumn2"
            value="#{person.numbercount}">
        </xp:viewColumn>
    </xp:viewPanel>
</xp:view>


BTW: Depending on the number of documents in your database, calling getAllDocumentsByKey() may be slow => consider using category view entries instead.
 
 
Apr 8, 2012, 3:18 PM
45 Posts
Re: Display document category and number of documents in Repeat Control.
Fantastic, works a treat.
 
I had thought the problem lay with the way I was dealing with the object but I did not know how to solve it.  
I take your point on the getAllDocumentsByKey(), I will have several of these on the one page so I will look at categorizing. 
 
I have learnt so much from this excercise, your help has been invaluable I really appreciate your time and patience Rick
 
Many thanks 
 
Mark. 

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal