DataTable don't select correct row..

UI Components for JSF
Post Reply
rafaelps.java
Posts: 2
Joined: 14 Feb 2011, 14:28

18 Mar 2011, 18:35

This week i needed implement next and foward row to navigate over the data of datable for user navigate on rows when in form mode. In words it's difficult to explain see a image at this url :
http://img855.imageshack.us/f/nav.jpg/

The problem that i note i change the value of the cargoSelecionado on managed bean with bindCargo(int index) ,
the datatable don't change the page and rowIndex if i'm on the last row of a page and click next or if i'm on the first row of a page greater than one and i click back button.
( On these cases the page don't change to correct and none row be selected although need ).

Relevant code of xhtml:

Code: Select all

<p:commandButton image="ui-icon ui-icon-circle-arrow-w" title="Next" update="opFormCargos, formCargos, opModoGrade"
	action="#{cargoMB.back}" process="@this"/>
				
<p:commandButton image="ui-icon ui-icon-circle-arrow-e" title="Back" update="opFormCargos, formCargos, opModoGrade" 
	action="#{cargoMB.next}" process="@this"/>

<p:dataTable id="dataTableCargos"
   						var="cargo" value="#{cargoMB.cargos}" 
   						paginator="true" dynamic="true"  
   						emptyMessage="Nenhum registro foi encontrado."
   						rows="10" rowsPerPageTemplate="5,10,15,20"  
   						selection="#{cargoMB.cargoSelecionado}" selectionMode="single"
   						paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
   						onRowSelectUpdate="formCargos, novoCargo" >  
Relevant code of CargoMB

Code: Select all

        public void next() {
		if (cargo != null) {
			Integer id = cargo.getId();

			if (id == null) {
				findData();
				if (cargos.size() > 0) {
					bindCargo(0);
				}
			} else {
				int idxSelected = getIndexCargoById(id);

				if (idxSelected + 1 < cargos.size()) {
					bindCargo(idxSelected + 1);
				}
			}
		}
	}

	public void back() {
		if (cargos.size() > 1 && cargo != null) {
			Integer id = cargo.getId();
			int idxSelected = getIndexCargoById(id);

			if (idxSelected > 0) {
				bindCargo(idxSelected - 1);
			}
		}
	}

        private void bindCargo(int index) {
		cargoSelecionado = cargo = cargos.get(index);
	}
As regards Rafael Peres dos Santos { SCJP 6, SCWCD 5, OCPBCD 5 }

rafaelps.java
Posts: 2
Joined: 14 Feb 2011, 14:28

18 Mar 2011, 18:46

What i did to solve this problem, i modify
DataTableRenderer.java class of the primefaces 3.0-SNAPSHOT

I think this code could be add to official version of primefacesfaces.

Code: Select all

 protected void encodeTbody(FacesContext context, DataTable table) throws IOException {
                 ...
             
                if(hasData) {
        	// i add this
        	if( "single".equalsIgnoreCase(selectionMode)) {
        		for( int rowIndex = 0; rowIndex < rowCount; rowIndex++ ) {
        			table.setRowIndex(rowIndex);
                	if(!table.isRowAvailable()) {
                		break;
                	}
                	
                	// test if a row is selected
        	    	if(handlePreselection(table, rowIndex, selectionMode, selection)) { 
    	        		int currentPage = table.getPage();
    	        		
	        			if(rowIndex >= currentPage * rows  ) { // if this .. go to next page and select the apropriate rowIndex
	        				table.setPage(++currentPage);
	        				table.setRowIndex(rowIndex);
	        			} else if((currentPage -1) * rows > rowIndex) {
	        				table.setPage(--currentPage);
	        				table.setRowIndex(rowIndex);
	    				}
    	        		break;
        	    	}
        		}
        	
        		if( table.getPage() > 1) {
        			first = (table.getPage() - 1) * rows; // update the first record of page
        		}
        	}// end
        	
            for(int i = first; i < (first + rowCountToRender); i++) {
                encodeRow(context, table, clientId, i, rowIndexVar, selMode, selection);
            }
        }

As regards Rafael Peres dos Santos { SCJP 6, SCWCD 5, OCPBCD 5 }

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 43 guests