1

I've got rather odd behavior here. When page just loaded, sort button isnt working. However after i add new item, sorting is working like a charm. Thanks in advance.

Controller code:

 public class MerchandiseController {

    public Merchandise__c product {get; set;}
    List<Merchandise__c> products {get; set;}
    public string selectedField {get; set;}

    public MerchandiseController() {
        product = new Merchandise__c();
        products = [SELECT Id, Name, Price__c, Quantity__c, Type__c, Date_Created__c, Date_Added__c, Available__c 
                    FROM Merchandise__c];
    }

    public List<Merchandise__c> getProducts(){
        return products;
    }

    public void save() {
        try {
            insert product;
            products = [SELECT Id, Name, Price__c, Quantity__c, Type__c, Date_Created__c, Date_Added__c, Available__c 
                        FROM Merchandise__c];
        }
        catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new product.'));
        }
    }

    public void sortMethod() {
        if (selectedField.equals('Name'))
            products = [SELECT Id, Name, Price__c, Quantity__c, Type__c, Date_Created__c, Date_Added__c, Available__c 
                        FROM Merchandise__c ORDER BY Name]; 
        else if (selectedField.equals('Price'))
            products = [SELECT Id, Name, Price__c, Quantity__c, Type__c, Date_Created__c, Date_Added__c, Available__c 
                        FROM Merchandise__c ORDER BY Price__c];
    }
 }

Visualforce code:

    <apex:page controller="MerchandiseController" tabStyle="Merchandise__c">
    <apex:form>
        <h1>Merchandise List</h1>

        <apex:pageBlock title="Sorting">
                <apex:selectList value="{!selectedField}" size="1" >
                    <apex:selectOption itemValue="Name" itemLabel="Merchandise Name"/>
                    <apex:selectOption itemValue="Price" itemLabel="Price"/>
                </apex:selectList>
                <apex:commandButton action="{!sortMethod}" value="Sort Table" reRender="merchandiseList"/>
        </apex:pageBlock>

        <apex:pageBlock title="Merchandise" id="merchandiseList">
            <apex:pageBlockTable value="{!products}" var="pitem">
                <apex:column value="{!pitem.name}"/>
                <apex:column value="{!pitem.Price__c}"/>
                <apex:column value="{!pitem.Quantity__c}"/>
                <apex:column value="{!pitem.Type__c}"/>
                <apex:column value="{!pitem.Date_Created__c}"/>
                <apex:column value="{!pitem.Date_Added__c}"/>
                <apex:column value="{!pitem.Available__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>

        <apex:pageBlock title="Add new" id="addNew">    
            <apex:pageBlockSection showHeader="false" columns="2">
                <apex:inputField value="{!product.name}"/>
                <apex:inputField value="{!product.Price__c}"/>
                <apex:inputField value="{!product.Quantity__c}"/>
                <apex:inputField value="{!product.Type__c}"/>
                <apex:inputField value="{!product.Date_Created__c}"/>
                <apex:inputField value="{!product.Date_Added__c}"/>
                <apex:inputField value="{!product.Available__c}"/>
            </apex:pageBlockSection>
            <apex:commandButton action="{!save}" value="Add item" reRender="merchandiseList"/>
        </apex:pageBlock>

    </apex:form>
</apex:page>

1 Answer 1

0

Try keeping the table sorted by name by default as below,

Constructor -

public MerchandiseController() {
        product = new Merchandise__c();
        selectedField='Name';
        products =new List<Merchandise__c>();
        sortMethod();
    }

VF Page: Add action region as below -

<apex:pageBlock title="Sorting">
<apex:actionRegion>
                <apex:selectList value="{!selectedField}" size="1" >
                    <apex:selectOption itemValue="Name" itemLabel="Merchandise Name"/>
                    <apex:selectOption itemValue="Price" itemLabel="Price"/>
                </apex:selectList>
                <apex:commandButton action="{!sortMethod}" value="Sort Table" reRender="merchandiseList"/>
</apex:actionRegion>
        </apex:pageBlock>
4
  • Did as you said. Initial table is sorted by name. But sorting isnt working until i add a new item.
    – squaremilk
    May 27, 2015 at 6:17
  • ok. Lemme check. Will fins a solution & get back to you.
    – snehakem
    May 27, 2015 at 6:28
  • I have made changes. Try that & let me know.
    – snehakem
    May 27, 2015 at 6:32
  • 1
    Huge thanks but i found my mistake. Some of the fields of object Mechandise__c set to 'required'. So when i tried to sort first, compiler needed some values in pageBlock "addNew". And i couldnt see it because of reRender attribute.
    – squaremilk
    May 27, 2015 at 6:38

You must log in to answer this question.

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