0

I need to design a table whose values in specific columns depends on values in a Case.

My initial visualforce page controller filters the cases so that I end up with a list of just a single type of cases. Lets call these rmcases.

In my table certain columns are straightforward like "{!rmcases.Subject}", "{!rmcases.CaseNumber}", etc. However I need to generate a certain number of columns, let say 1 column whose value depends on "{!rmcases.Date__c}", for example, the column should show + if the date is in the future.

Initially I designed a page that looked like:

<apex:pageBlock >
    <apex:pageBlockTable value="{!cases}" var="rmcase">
            <apex:column value="{!rmcase.CaseNumber}"/>
            <apex:column value="{!rmcase.Subject}"/>
            <apex:column value="{!rmcase.Status}"/>
            <c:rmcomponent case="{!rmcase}" />     
    </apex:pageBlockTable>
    </apex:pageBlock>

The idea being that the component would check the value of date for rmcase and return a column. However, this doesn't work because I get that a column needs to be between pageBlockTable tags.

The alternative is to have the component generate the whole table but then I get into another issue, which is that I would need to call a method in the controller for rmcase that would give me the value of the column.

What would be a best practice solution?

  • Is there a reason you don't want to put the logic in-line in an apex:column in the apex:pageBlockTable you show in your question? (I know the page would be cleaner if you factored this out into a component but as you have discovered it isn't always possible.) – Keith C Dec 1 '16 at 22:16
  • @caleb.breckon Not sure that will work. I always want the column to be rendered, just that the value should be blank if date is not in future. Another issue is that works for one column, but if I want several columns, lets say first one if value is in the future, second if value is later than a month for now, third if value is later than 2 months for now things get tricky. I can't understand how there wouldn't be a better solution to this in apex. – Lily Dec 1 '16 at 22:32
  • @KeithC inline like caleb mentioned? Can you give an example? – Lily Dec 1 '16 at 22:33
  • I revised my answer. I think you're overthinking this. There are dozens of ways to do this via Apex, Visualforce, or JS. – caleb.breckon Dec 2 '16 at 1:00
-1
<apex:column>{!IF(rmcases.Date__c > TODAY(),'in the future','')}</apex:column>
<apex:column>{!IF(rmcases.Date__c > TODAY()+30,'later than a month','')}</apex:column>
<apex:column>{!IF(rmcases.Date__c > TODAY()+60,'later than two months','')}</apex:column>

Of course you could always set the comparison dates in your controller.

// Apex
public Date today {
    get {
        if ( today == null ) {
            today = System.today();
        }
        return today;
    }
    set;
}
public Date oneMonthFromToday {
    get {
        return today.addMonths(1);  
    }
    set;
}
public Date twoMonthsFromToday {
    get {
        return today.addMonths(2);  
    }
    set;
}
// Visualforce
<apex:column>{!IF(rmcases.Date__c > today,'in the future','')}</apex:column>
<apex:column>{!IF(rmcases.Date__c > oneMonthFromToday,'later than a month','')}</apex:column>
<apex:column>{!IF(rmcases.Date__c > twoMonthsFromToday,'later than two months','')}</apex:column>

You could create a function that takes the date as an input and outputs, if any, what string to put in the column. There's dozens of different ways to implement this.

  • Is this supposed to be an answer? Can you please elaborate to at least explain what you are trying to share with the community? Code dump answers are generally not very valuable. – Adrian Larson Dec 1 '16 at 23:52
  • @AdrianLarson We've clearly got different baselines on how low level we feel explanations should be. Glad you had a moment to comment on the value of my perfectly viable answer, but not to write a textbook on the fifty ways to solve this very simple question. – caleb.breckon Dec 2 '16 at 0:59
  • You literally did not add one word of explanation. Now that you have, I will remove my down vote. A code only post is not an explanation (barring clear and thorough comments). It doesn't have to be a novel. In fact, concise is better. – Adrian Larson Dec 2 '16 at 3:30
  • Thanks @caleb.breckon , assuming I want to follow the suggestion of having a function that takes a date and outputs a string, how do you call it from the visual force page? Would something like this do <apex:column>{!{columnString(oneMonthFromToday)}}</apex:column>? – Lily Dec 2 '16 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.