IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Seam Java Discussion :

Vrai pagination d'un rich:datatable avec un rich:datascroller


Sujet :

Seam Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    75
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2007
    Messages : 75
    Par défaut Vrai pagination d'un rich:datatable avec un rich:datascroller
    Salut.

    J'ai eut une belle surprise avec richfaces:

    J' utilisais la pagination avec richfaces en utilisant les composants datatable et datascroller simplement (ou betement je sais pas).

    un truc qui simplifié donne genre:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    @Entity
    public class MyLog implements Serializable{
     
            private static final long serialVersionUID = -4235759150267631L;
     
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	@Column(name = "ID", unique = true)
    	private Long id;
     
    	private int httpStatus;
     
    	private boolean checkStringStatus;
     
    	private String url;
     
    	// getter and setter
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    <rich:dataTable value="#{myLogList.resultList}" var="log" rows="20" id="simpletable">
    	<f:facet name="header">
    		<rich:columnGroup>
    			<rich:column  >
    				<h:outputText value="ID" />
    			</rich:column>
    			<rich:column>
    				<h:outputText value="Http Status" />
    			</rich:column>
    			<rich:column>
    				<h:outputText value="Is Checked" />
    			</rich:column>
    			<rich:column>
    				<h:outputText value="URL" />
    			</rich:column>
    		</rich:columnGroup>
    	</f:facet>
    	<rich:column>
    		<h:outputText value="#{log.id}" />
    	</rich:column>
    	<rich:column>
    		<h:outputText value="#{log.httpStatus}" />
    	</rich:column>
    	<rich:column>
    		<h:outputText value="#{log.checkStringStatus}" />
    	</rich:column>
    	<rich:column>
    		<h:outputText value="#{log.url}" />
    	</rich:column>
    </rich:dataTable>
    <rich:spacer height="10" />
    <rich:datascroller for="simpletable" maxPages="10" renderIfSinglePage="false" />
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    @Name("myLogList")
    public class MyLogList extends EntityQuery<MyLog> {
     
    	private static final long serialVersionUID = 3565361771593939734L;
     
    	private static final String EJBQL = "SELECT l FROM MyLog l";
     
    		public MyLogList() {
    		setEjbql(EJBQL);
    	}
     
    }
    bien en faite ca fait de la fausse pagination.

    Il fait un select de toute la table et la ramene dans son backingbean et fait le trie apres...

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    75
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2007
    Messages : 75
    Par défaut
    J'ai trouvé comment faire de la "vrai" pagination a partir de l'exemple sur la demo richfaces: http://livedemo.exadel.com/richfaces...el&cid=3737637

    Ca donne un truc du genre:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    <rich:dataTable value="#{myLogDataModel}" var="log" rows="20" id="simpletable">
    	<f:facet name="header">
    		<rich:columnGroup>
    			<rich:column  >
    				<h:outputText value="ID" />
    			</rich:column>
    			<rich:column>
    				<h:outputText value="Http Status" />
    			</rich:column>
    			<rich:column>
    				<h:outputText value="Is Checked" />
    			</rich:column>
    			<rich:column>
    				<h:outputText value="URL" />
    			</rich:column>
    		</rich:columnGroup>
    	</f:facet>
    	<rich:column>
    		<h:outputText value="#{log.id}" />
    	</rich:column>
    	<rich:column>
    		<h:outputText value="#{log.httpStatus}" />
    	</rich:column>
    	<rich:column>
    		<h:outputText value="#{log.checkStringStatus}" />
    	</rich:column>
    	<rich:column>
    		<h:outputText value="#{log.url}" />
    	</rich:column>
    </rich:dataTable>
    <rich:spacer height="10" />
    <rich:datascroller for="simpletable" maxPages="10" renderIfSinglePage="false" />

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
     
    @Name("myLogDataModel")
    public class MyLogDataModel extends SerializableDataModel {
     
    	@In(create = true,required = true)
    	MyLogDataProvider dataProvider;
    	private Long currentPk;
    	private Map<Long,MyLog> wrappedData = new HashMap<Long,MyLog>();
    	private ArrayList<Long> wrappedKeys = null;
    	private static final long serialVersionUID = -1956179896877538628L;
     
    	/**
             * This method never called from framework.
             * (non-Javadoc)
             * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
             */
    	@Override
    	public Object getRowKey() {
    		return currentPk;
    	}
     
            /**
             * This method normally called by Visitor before request Data Row.
             */
    	@Override
    	public void setRowKey(Object key) {
    		this.currentPk = (Long) key;
     
    	}
     
    	/**
             * This is main part of Visitor pattern. Method called by framework many times during request processing. 
             */
    	@Override
    	public void walk(FacesContext context, DataVisitor visitor, Range range, Object argument) throws IOException {
    		int firstRow = ((SequenceRange)range).getFirstRow();
    		int numberOfRows = ((SequenceRange)range).getRows();
    		wrappedKeys = new ArrayList<Long>();
    		for (MyLog item:dataProvider.getItemsByrange(new Integer(firstRow), numberOfRows, null, true)) {
    			wrappedKeys.add(item.getId());
    			wrappedData.put(item.getId(), item);
    			visitor.process(context, item.getId(), argument);
    		}
    	}
     
    	/**
             * This method must return actual data rows count from the Data Provider. It is used by pagination control
             * to determine total number of data items.
             */
    	private Integer rowCount; // better to buffer row count locally
    	@Override
    	public int getRowCount() {
    		if (rowCount==null) {
    			rowCount = new Integer(getDataProvider().getRowCount());
    			return rowCount.intValue();
    		} else {
    			return rowCount.intValue();
    		}
    	}
     
    	/**
             * This is main way to obtain data row. It is intensively used by framework. 
             * We strongly recommend use of local cache in that method. 
             */
    	@Override
    	public Object getRowData() {
    		if (currentPk==null) {
    			return null;
    		} else {
    			MyLog ret = wrappedData.get(currentPk);
    			if (ret==null) {
    				ret = getDataProvider().getMyLogByPk(currentPk);
    				wrappedData.put(currentPk, ret);
    				return ret;
    			} else {
    				return ret;
    			}
    		}
    	}
     
    	/**
             * Unused rudiment from old JSF staff.
             */
    	@Override
    	public int getRowIndex() {
    		throw new UnsupportedOperationException();
    	}
     
    	/**
             * Unused rudiment from old JSF staff.
             */
    	@Override
    	public Object getWrappedData() {
    		throw new UnsupportedOperationException();
    	}
     
    	/**
             * Never called by framework.
             */
    	@Override
    	public boolean isRowAvailable() {
    		if (currentPk==null) {
    			return false;
    		} else {
    			return getDataProvider().hasMyLogByPk(currentPk);
    		}
    	}
     
    	/**
             * Unused rudiment from old JSF staff.
             */
    	@Override
    	public void setRowIndex(int rowIndex) {
    		throw new UnsupportedOperationException();
    	}
     
    	/**
             * Unused rudiment from old JSF staff.
             */
    	@Override
    	public void setWrappedData(Object data) {
    		throw new UnsupportedOperationException();
    	}
     
    	/**
             * This method suppose to produce SerializableDataModel that will be serialized into View State and used on a post-back.
             * In current implementation we just mark current model as serialized. In more complicated cases we may need to 
             * transform data to actually serialized form.
             */
    	public  SerializableDataModel getSerializableModel(Range range) {
    		if (wrappedKeys!=null) {
    			return this; 
    		} else {
    			return null;
    		}
    	}
    	/**
             * This is helper method that is called by framework after model update. In must delegate actual database update to 
             * Data Provider.
             */
    	@Override
    	public void update() {
    		getDataProvider().update();
    	}
     
    	public MyLogDataProvider getDataProvider() {
    		return dataProvider;
    	}
     
    	public void setDataProvider(MyLogDataProvider dataProvider) {
    		this.dataProvider = dataProvider;
    	}

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    @Name("dataProvider")
    public class MyLogDataProvider {
     
    	@In
    	EntityManager entityManager;
     
    	public MyLog getMyLogByPk(Long currentPk) {
    		Query q = entityManager.createQuery("SELECT l FROM MyLog l WHERE l.id=:currentPk");
    		q.setParameter("currentPk", currentPk);
    		MyLog myLog = (MyLog) q.getSingleResult();
    		if (myLog != null)
    			return myLog;
    		throw new RuntimeException("MyLog ID=" + currentPk.toString() + " not found");
    	}
     
    	public boolean hasMyLogByPk(Long currentPk) {
    		Query q = entityManager.createQuery("SELECT l FROM MyLog l WHERE l.id=:currentPk");
    		q.setParameter("currentPk", currentPk);
    		MyLog myLog = (MyLog) q.getSingleResult();
    		if (myLog != null)
    			return true;
    		else
    			return false;
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<MyLog> getItemsByrange(Integer startPk, int numberOfRows, String sortField, boolean ascending) {
    		List<MyLog> ret = new ArrayList<MyLog>();
    		Query q = entityManager.createQuery("SELECT l FROM MyLog l");
    		q.setFirstResult(startPk);
    		q.setMaxResults(numberOfRows);
    		ret = q.getResultList();
    		return ret;
    	}
     
    	public void update() {
    		// nothing need to do
    	}
     
    	public int getRowCount() {
    		Query q = entityManager.createQuery("SELECT COUNT(l) FROM MyLog l");
    		Long tmp = (Long) q.getSingleResult();
    		return tmp.intValue();
    	}
    }
    Voila, c'est plus verbeux mais cela limite les requetes SQL aux row que la datatable affiche.

    Problème: cette methode ne fonctionne pas avec les sortby et orderby dynamique (on peut toujours mettre un order by dans la requete SQL mais on ne sait pas modifier l'ordre dans la page web)

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    75
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2007
    Messages : 75
    Par défaut
    Donc je cherche une facon qui me permetrais d'utiliser le sortby et filterby avec de la vrai pagination. (ca marche avec la fausse pagination)

    C'est possible vu il le font dans la demo: http://livedemo.exadel.com/richfaces...el&cid=3737943

    Mais je pige pas grand choses sur ce coup la (j'ai jamais utilise un rich:columnS)

    Il part d'un fichier CSV ce qui m'embrouille encore plus.

    question: je suis obligé d'utilisé un rich:columnS ou je peux le faire avec un rich:column?

    Comment passé à ma fonction getItemsByrange() pour savoir si c'est un sort ou un filter ou les 2 + la valeur du champs de filter.

    si je met un sortby ou filterby, ma methode getItemsByrange() dans LogSpeosDataProvider recoit un -1 comme parametre numberOfRows.
    Et donc un q.setMaxResults(-1) il aime pas trop

    personne a une idée ,un site, n'importe quoi ou il explique comment je pourrais sorter et filtrer correctement?

    Merci

    edit: du blabla ici:
    http://eclecticprogrammer.com/2008/0...ith-richfaces/
    http://eclecticprogrammer.com/2008/0...ith-richfaces/

    edit bis: bon avec les liens ci dessus ca permet de faire du sortby en plus de la pagination mais tjs pas de filterby (puis c'est un sort manuel y'a pas les petite fleche vers le haut ou vers le bas)

Discussions similaires

  1. Réponses: 0
    Dernier message: 14/01/2011, 09h17
  2. Fermer rich:panel avec un rich:tab
    Par stratocasters dans le forum JSF
    Réponses: 2
    Dernier message: 11/10/2010, 22h39
  3. Réponses: 0
    Dernier message: 03/07/2009, 11h16
  4. [<rich:dataTable>] avec 2 var
    Par AmineDev9 dans le forum JSF
    Réponses: 1
    Dernier message: 22/05/2009, 16h25
  5. Réponses: 9
    Dernier message: 24/01/2008, 08h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo