+ Start a Discussion
SimrinSimrin 

hiding field based on condition

Hello,

I have a pageBlockTable on visualForce like below.
 
<apex:pageblock id="PB2">
                    <apex:pageblocktable value="{!pList}" var="item">
                        <apex:column headervalue="User with Skill">
                            {!item.User.FirstName}&nbsp;{!item.User.LastName}
                        </apex:column>
                        <apex:column headervalue="Skill Name">
                            {!item.ProfileSkill.Name}
                        </apex:column>                     
                    </apex:pageblocktable>
                </apex:pageblock>

User-added image
 

I will have many Users.  I am ordering it as per user name. I want to just display the first name and hid the repeated names.

Is there way to achieve my use case using CSS or javascript.

Thank you

Best Answer chosen by Simrin
Prady01Prady01
Hi Simrin, Yes if you want to do this on the UI layer, Yes this can be done, Below is the detailed code for the VF page and controller. Hope this helps! I have done this using Jquery.
 
<apex:page sidebar="false" controller="HideRepeatingController">
<apex:includeScript value="{!URLFOR($Resource.JqGrid, 'js/jquery-1.11.0.min.js')}"/>

<script type="text/javascript">   
    var jq = jQuery.noConflict();
    function HideCallstoJs() {
        var seen = {};
        jq("td[id*='Fname']").each(function() {
            var key = jq(this).index() +  jq(this).text();
            if (seen[key]) {
                jq(this).text('');
            }
            else {
                seen[key] = true;
            }
        });
        }
      jq( document ).ready(function(){
            HideCallstoJs();
      }); // end of DOM load
</script>

    <apex:pageBlock >
        <apex:pageBlockTable value="{!conLst}" var="cl">
            <apex:column value="{!cl.FirstName}" id="Fname"/>
            <apex:column value="{!cl.LastName}"/>
        </apex:pageBlockTable>    
    </apex:pageBlock>
</apex:page>
public with sharing class HideRepeatingController {
 public List<Contact> conLst{get;set;}
 
 public HideRepeatingController(){
     conLst = new List<Contact>([select id,name, FirstName, LastName from Contact LIMIT 1000]);
 }

}
You can copy pase this code and give it a try in your Org. I have just hidden the First names i.e. first column. Whenever there is a duplicate values in Row that gets automatically hidden from the Jquery.

Thanks
Prady01

 

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Simrin,

This is possible through javascript , but doing it through its controller is way easy .
Please paste your controller code.

Please look into below link as well.

http://salesforce.stackexchange.com/questions/30522/how-to-avoid-duplicates-in-visualforce-page

Let us know if it helps you
SimrinSimrin
Hi Ashish,

Thanks for reply.

My code like this..
 
transient public List<ProfileSkillUser> pList{get;set;}  

public void initSearchTable() {
  pList = [SELECT User.FirstName, User.LastName, ProfileSkill.Name
                        FROM ProfileSkillUser ORDER BY UserId ];
}
 
<apex:pageblock id="PB2">
      <apex:pageblocktable value="{!pList}" var="item">
           <apex:column headervalue="User with Skill">
                    {!item.User.FirstName}&nbsp;{!item.User.LastName}
            </apex:column>
            <apex:column headervalue="Skill Name">
                   {!item.ProfileSkill.Name}
            </apex:column>                     
      </apex:pageblocktable>
</apex:pageblock>


<apex:commandButton value="Search" reRender="PB2"  action="{!initSearchTable)}"> 
</apex:commandButton>

 
doravmondoravmon
what you can do is just add some code in  the controller:


public void initSearchTable() {
  pList = [SELECT User.FirstName, User.LastName, ProfileSkill.Name
                         FROM ProfileSkillUser ORDER BY UserId ];
  displayOrNot();
}
public void displayOrNot()
{  
    for(Integer i=1;i<pList.size();i++)
    {
        if(pList[i].User.FirstName==pList[i-1].User.FirstName && pList[i].User.LastName==pList[i-1].User.LastName){
              pList[i].User.FirstName='';
              pList[i].User.LastName=";
        }
     }
}

if you still need to use the pList in the future, I recommand you can create another variable to store the whole value~
Let me know if this helpes~

 
SimrinSimrin
Thank you very much.  
It works for some users and not for some. I will check what is going wrong. but indeed a nice start to what i needed
Prady01Prady01
Hi Simrin, Yes if you want to do this on the UI layer, Yes this can be done, Below is the detailed code for the VF page and controller. Hope this helps! I have done this using Jquery.
 
<apex:page sidebar="false" controller="HideRepeatingController">
<apex:includeScript value="{!URLFOR($Resource.JqGrid, 'js/jquery-1.11.0.min.js')}"/>

<script type="text/javascript">   
    var jq = jQuery.noConflict();
    function HideCallstoJs() {
        var seen = {};
        jq("td[id*='Fname']").each(function() {
            var key = jq(this).index() +  jq(this).text();
            if (seen[key]) {
                jq(this).text('');
            }
            else {
                seen[key] = true;
            }
        });
        }
      jq( document ).ready(function(){
            HideCallstoJs();
      }); // end of DOM load
</script>

    <apex:pageBlock >
        <apex:pageBlockTable value="{!conLst}" var="cl">
            <apex:column value="{!cl.FirstName}" id="Fname"/>
            <apex:column value="{!cl.LastName}"/>
        </apex:pageBlockTable>    
    </apex:pageBlock>
</apex:page>
public with sharing class HideRepeatingController {
 public List<Contact> conLst{get;set;}
 
 public HideRepeatingController(){
     conLst = new List<Contact>([select id,name, FirstName, LastName from Contact LIMIT 1000]);
 }

}
You can copy pase this code and give it a try in your Org. I have just hidden the First names i.e. first column. Whenever there is a duplicate values in Row that gets automatically hidden from the Jquery.

Thanks
Prady01

 
This was selected as the best answer