0

I have an Object Package__c with fields Available__c (Checkbox) and Quantity__c. I have a VF datatable as follows -

<apex:dataTable> value="{!pkglist}" var="pkg1" id="fam">
            <apex:column>
                <apex:inputField value="{!pkg1.Quantity__c}"/>
            </apex:column>
</apex:dataTable>

The Apex code is-

public List<Package_Item__c> pkglist{get; set;}
pkglist = [SELECT Quantity__c FROM Package_c WHERE Available__c = 'TRUE'];

So suppose, I have a total of 10 records in Package__c, and 4 of them Have Available__c = 'TRUE', the datatable in the VF page will have only 4 Rows. I dont want this. I want the datatable to display all the 10 rows for each of the records, but with the inputField populated with '0', instead of the Quantity of the Package. How should I achieve this?

4
  • 2
    You have a decent description of what you want to have happen. Have you made an attempt to turn that into code? If you need a bit of help to get you going in the right direction, then look at your query. Your current filters (the things in the WHERE clause) restrict you to 4 results. What would you need to do to get all of your records, regardless of the value of Available__c?
    – Derek F
    Commented Dec 11, 2020 at 15:16
  • I just need to remove the WHERE clause, but that wont help me get 0 on the inputfields where the Available is False
    – An Amateur
    Commented Dec 11, 2020 at 16:11
  • You could handle that part (displaying the quantiy as 0) through Visualforce, but I'd suggest doing that in your controller (or controller extension, whatever the case might be). Are you familiar with using a loop to iterate over items in a list (or from a query, which returns a list)? If you're not, then you should look for documentation/trailhead units/blogs to get a basic understanding. The idea being that you can modify the in-memory instances of the records displayed by your visualforce page before the visualforce page is rendered.
    – Derek F
    Commented Dec 11, 2020 at 16:33
  • I'm not familiar as such with using loops to iterate over items in a list from a query, so I'll look for some articles online. Also, if you could provide some kind of a hint for doing that on visualforce as well
    – An Amateur
    Commented Dec 11, 2020 at 16:45

1 Answer 1

0

Try this:

Controller

public Package_Item__c[] packageItems {
  get {
       PackageItems = [SELECT Quantity__c, Available__c FROM Package__c];
       for (Package_Item__c pi : packageItems) {
        if (!pi.Available__c) {pi.Quantity__c = 0;}
       }
       return packageItems;
  } set;
}

VF

<apex:dataTable> value="{!packageItems}" var="pi" id="fam">
            <apex:column>
                <apex:inputField value="{!pi.Quantity__c}"/>
            </apex:column>
</apex:dataTable>

The VF table is iterating over a collection of sobjects in memory (packageItems) that the controller has enhanced with Quantity__c=0 when Available__c is false.

N.B. I renamed your variables for readability and concision

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .