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
Mishael LegaspiMishael Legaspi 

Problem in apex:input in apex:pageBlockTable when using a Map

Hi all,

I'm using pageBlockTable to display rows of an inner class in my controller. Now, I also want every row to have an option to be deleted. I found solutions online similar to http://sfdcsrini.blogspot.com/2014/12/adding-and-deleting-rows-dynamically-in.html . But I would like to avoid using apex:variable because I think it's not that straightforward. So I tried to use a Map instead of a List of Row objects where the keys are the rowId of the Row objects. 

Here's my controller code:
public class TestInputController {
    
    public Map<Integer, Row> rows { get; set; }
    
    public TestInputController() {
        rows = new Map<Integer, Row>();
    }
    
    public void addRow() {
        Integer rowId = rows.size();
        rows.put(rowId , new Row(rowId));
    }
    
    public void removeRow() {
        rows.remove(Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIdToRemove')));
    }
    
    public class Row {
        public Integer rowId { get; set; }
        public Row(Integer rowId) {
            this.rowId = rowId;
        }
    }

}

And here's my visualforce code:
<apex:page controller="TestInputController" docType="html-5.0">
    <apex:form >        
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!rows}" var="rid" id="tablet">
                    <apex:column headerValue="ID">
                        <apex:input type="number" value="{!rows[rid].rowId}" />
                    </apex:column>
                    <apex:column headerValue="Action">
                        <apex:commandButton action="{!removeRow}" value="x" rerender="tablet">
                            <apex:param value="{!rows[rid].rowId}" name="rowIdToRemove" />
                        </apex:commandButton>
                    </apex:column>
                </apex:pageBlockTable>
                <apex:pageBlockSectionItem >
                    <apex:commandButton value="Add Row" action="{!addRow}" rerender="tablet"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Unfortunately, when I try to save the VisualForce page, I get the error:
Error: Expected input type 'text', got 'number' for Id data type

I figured that this is the line that caused the error:
<apex:input type="number" value="{!rows[rid].rowId}" />

I know that I can go back to the solution I found online regarding deleting rows, but I just don't get it why I'm getting this error. When I was using Lists before, the line
<apex:input type="number" value="{!row.rowId}" />
works (where row is the var name i used in the pageBlockTable). Am I missing something here? Or is this a bug?

Thanks,
Mishael
Best Answer chosen by Mishael Legaspi
Akshay DeshmukhAkshay Deshmukh
use this. It should solve your problem.
<apex:input type="auto" value="{!rows[rid].rowId}" />

Mark this as best answer if it solves your problem.

All Answers

Akshay DeshmukhAkshay Deshmukh
use this. It should solve your problem.
<apex:input type="auto" value="{!rows[rid].rowId}" />

Mark this as best answer if it solves your problem.
This was selected as the best answer
Mishael LegaspiMishael Legaspi
Hi, Akshay Deshmukh!

That worked for me. Thank you very much!

Regards,
Mishael