function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NervosaNervosa 

Datepicker with calendar in VF + Erasing content of inputText

Hi there! =)

 

Look what i've got! Here is my Apex class:

 

public class FullFunctionality {

    public String NewItemName {get; set;}
    public Integer NewItemQuantity { get; set; }
    public Integer NewItemPrice { get; set; }
    public String NewItemType { get; set; }
    public Date NewItemReleaseDate { get; set; }



    public List<SelectOption> getTypes(){
        List<SelectOption> types = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Item__c.Item_Type__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry f : ple)
   {
      types.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return types;
    }
    
    public void add() {
        
        Item__c NewItem = new Item__c(            
            Name = NewItemName,
            Item_Price__c = NewItemPrice,
            Items_Available__c = NewItemQuantity,
            Item_Type__c = NewItemType,
            Release_Date__c = NewItemReleaseDate);
        insert NewItem;    
    }
    
    DisplayMerchandise[] products;

    public class DisplayMerchandise {
        public Item__c merchandise { get; set; }
        public DisplayMerchandise(Item__c item) {
            this.merchandise = item;
        }
    }

    public DisplayMerchandise[] getProducts() {
        if (products == null) {
            products = new DisplayMerchandise[]{};
            for (Item__c item : 
                [SELECT id, name, item_price__c, createddate, item_type__c 
                 FROM Item__c ]) {
               products.add(new DisplayMerchandise(item));
             }
         }
     return products;      
    }
}

 and here is my VF page:

 

public class FullFunctionality {

    public String NewItemName {get; set;}
    public Integer NewItemQuantity { get; set; }
    public Integer NewItemPrice { get; set; }
    public String NewItemType { get; set; }
    public Date NewItemReleaseDate { get; set; }



    public List<SelectOption> getTypes(){
        List<SelectOption> types = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Item__c.Item_Type__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry f : ple)
   {
      types.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return types;
    }
    
    public void add() {
        
        Item__c NewItem = new Item__c(            
            Name = NewItemName,
            Item_Price__c = NewItemPrice,
            Items_Available__c = NewItemQuantity,
            Item_Type__c = NewItemType,
            Release_Date__c = NewItemReleaseDate);
        insert NewItem;    
    }
    
    DisplayMerchandise[] products;

    public class DisplayMerchandise {
        public Item__c merchandise { get; set; }
        public DisplayMerchandise(Item__c item) {
            this.merchandise = item;
        }
    }

    public DisplayMerchandise[] getProducts() {
        if (products == null) {
            products = new DisplayMerchandise[]{};
            for (Item__c item : 
                [SELECT id, name, item_price__c, createddate, item_type__c 
                 FROM Item__c ]) {
               products.add(new DisplayMerchandise(item));
             }
         }
     return products;      
    }
}

 There are two problems that i can't solve because of my inexperience:

1) How can i implement formatted input of ReleaseDate? It is desirable a calendar to be rendered when focus is on the corresponding inputText.

2) How can i clean all the inputText elements on my page when commandButton is down and new record is added successfully?

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 

All Answers

kiranmutturukiranmutturu

for date widget you can make create a standard object instance which contains date field in the class and make use of that..

 

or you can go with some thing like this

 

<apex:page >
<apex:form >
<table>
<td> Date<br></br><input id="t" name="date" onfocus="DatePicker.pickDate(false,
't', false);" size="12" tabindex="28" type="text" /><span class="dateFormat">&nbsp;<a
href="javascript&colon;DatePicker.insertDate('2/3/2011', 't', true);"
></a>&nbsp;</span></td>
</table>
</apex:form>
</apex:page>

 

 

i didn't get the second question..

NervosaNervosa

I'm afraid this won't fit.

As far as i can see - there are no any methods in visualforce to do that and Javascript methods are inacceptable.

Maybe there is a way to make something like formatted input?

 

Concerning my second question - to make the long story short - i'd like to refresh(rerender) my vf page so that new item will be already displayed.

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

Please add your Visualforce Page code .I saw only apex code in your Post

NervosaNervosa

Oops =))

Here it is:

<apex:page standardStylesheets="false" showHeader="false" sidebar="false" Controller="FullFunctionality">
<apex:stylesheet value="{!URLFOR($Resource.Styles, 'styles.css')}"/>
<apex:form style="align:left">
<apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even" align="center">

    <apex:column style="width:100">
        <apex:commandLink value="Del" title="{!pitem.merchandise.id}"/>
    </apex:column>
    <apex:column headerValue="Item name" style="text-align:left">
        <apex:outputText value="{!pitem.merchandise.name}"/>
    </apex:column>
    <apex:column headerValue="Price" >
        <apex:outputText value="{!pitem.merchandise.Item_Price__c}"/>
    </apex:column>
    <apex:column headerValue="Date of adding" >
        <apex:outputText value="{0,date,MM/dd/yyyy}">
            <apex:param value="{!pitem.merchandise.createddate}"/>
        </apex:outputText>
    </apex:column>    
</apex:dataTable>
</apex:form>

<apex:form >
<apex:messages />
<apex:pageBlock > 
    <apex:pageBlockSection columns="1" >
        <apex:inputText value="{!NewItemName}" label="Name"/>            
            <apex:selectList id="types" size="1" required="true" label="Type" >
                <apex:selectOptions value="{!types}"/>
            </apex:selectList>
            <apex:inputText value="{!NewItemPrice}" label="Price"/>
            <apex:inputText value="{!NewItemQuantity}" label="Quantity"/>
            <apex:inputText value="{!NewItemReleaseDate}"  label="Release Date"/>
        <apex:commandButton value="Add item" action="{!add}" />
    </apex:pageBlockSection>
</apex:pageBlock>        
</apex:form>
</apex:page>

 

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

1)First point using multiple forms is not a better approach

 

2)Kiran was right that we will have to use an already existing object instance to bring the Calendar picker

 

3)The question were you are about to rerender whole page i would recommend you to use Pagerefernce method returning null.This will solve your problem and already added line item will display.

NervosaNervosa

Thank you for reply.

 

About refreshing my page. I added a method like this -

 

    public PageReference refresh(){
    
        return null;
    
    }

 and then i invoke it in my add() method -

 

   public void add() {
        
        Item__c NewItem = new Item__c(            
            Name = NewItemName,
            Item_Price__c = NewItemPrice,
            Items_Available__c = NewItemQuantity,
            Item_Type__c = NewItemType,
            Release_Date__c = NewItemReleaseDate);
            insert NewItem;  
            refresh();  
    }

 but still it doesn't work properly. What i did wrong?

Mohith Kumar ShrivastavaMohith Kumar Shrivastava
public PageReference add(){

Item__c NewItem = new Item__c(            
            Name = NewItemName,
            Item_Price__c = NewItemPrice,
            Items_Available__c = NewItemQuantity,
            Item_Type__c = NewItemType,
            Release_Date__c = NewItemReleaseDate);
            insert NewItem;  

return null;
}

 Try this .

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

Also better is on Add method you collect in a list.Make a getter setter and collect in a list .You can insert at the last through submit button.For each Add button making a DML call is not good practice

NervosaNervosa

mohit_shrivastava wrote:
public PageReference add(){

Item__c NewItem = new Item__c(            
            Name = NewItemName,
            Item_Price__c = NewItemPrice,
            Items_Available__c = NewItemQuantity,
            Item_Type__c = NewItemType,
            Release_Date__c = NewItemReleaseDate);
            insert NewItem;  

return null;
}

 Try this .


This doesn't work =\

 

Also i can't get an idea you wrote in your next post. =(