JSF – 29 – Data Tables – 02

Merhaba Arkadaslar
Bu bolumde JSF Datatable konusunu incelemeye devam ediyoruz.

Add & Remove Row

Onceki bolumde kullandigimiz PersonDataBean.java ve Person.java siniflarimizi burada tekrar kullanabiliriz.
Yeni row eklemek ya da silmek icin 2 yeni metot ve forma girilen verileri bind etmek icin property’ler tanimlayalim.

PersonDataBean.java

package _34.dataTable;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class PersonDataBean {

	private String name;
	private String surname;
	private char gender;
	private int age;
	private List<Person> persons;

	//getters and setters

	@PostConstruct
	public void initialize() {
		persons = new ArrayList<Person>();
		persons.add(new Person("name1", "surname1", 'M', 20));
		persons.add(new Person("name2", "surname2", 'F', 25));
		persons.add(new Person("name3", "surname3", 'F', 15));
		persons.add(new Person("name4", "surname4", 'M', 30));
		persons.add(new Person("name5", "surname5", 'M', 35));
	}

	public void addPerson() {
		Person person = new Person(name, surname, gender, age);
		persons.add(person);
	}
	
	public void deletePerson(Person person) {
		persons.remove(person);
	}
}

Onceki ornegimizden farkli olarak addPerson ve deletePerson metotlarini ekledik. Kodu inceleyecek olursak List<Person> ‘a yeni bir eleman ekliyoruz/cikartiyoruz.

Person.java

package _34.dataTable;


public class Person {

	private String name;
	private String surname;
	private char gender;
	private int age;

	public Person() {
		super();
	}

	public Person(String name, String surname, char gender, int age) {
		super();
		this.name = name;
		this.surname = surname;
		this.gender = gender;
		this.age = age;
	}
       //getters and setters
}

addAndDelete.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
<h:head>
	<h:outputStylesheet library="css" name="table-style.css" />
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Data Table</title>
</h:head>
<h:body>

	<h:form id="form">

		<h:dataTable id="dataTable" value="#{personDataBean.persons}" var="myPerson"
			captionStyle="font-size:20px" styleClass="person-table"
			headerClass="person-table-header"
			rowClasses="person-table-oddRow,person-table-evenRow">

			<f:facet name="caption">Person List</f:facet>

			<h:column>
				<f:facet name="header">Name</f:facet>
				#{myPerson.name}
			</h:column>

			<h:column>
				<f:facet name="header">Surname</f:facet>
				#{myPerson.surname}		
			</h:column>

			<h:column>
				<f:facet name="header">Gender</f:facet>
				#{myPerson.gender}
			</h:column>

			<h:column>
				<f:facet name="header">Age</f:facet>
				#{myPerson.age}
			</h:column>
			
			<h:column>
				<f:facet name="header">Delete</f:facet>				
				<h:commandLink value="Delete" action="#{personDataBean.deletePerson(myPerson)}" />
			</h:column>

		</h:dataTable>

		<h3>Add Person</h3>
		<table id="myTable">
			<tr>
				<td>Name:</td>
				<td><h:inputText id="name" value="#{personDataBean.name}" /></td>
			</tr>
			<tr>
				<td>Surname:</td>
				<td><h:inputText  id="surname" value="#{personDataBean.surname}" /></td>
			</tr>
			<tr>
				<td>Gender:</td>
				<td><h:inputText value="#{personDataBean.gender}" /></td>
			</tr>
			<tr>
				<td>Age:</td>
				<td><h:inputText value="#{personDataBean.age}" /></td>
			</tr>
		</table>
		<p>
			<h:commandButton value="Add" action="#{personDataBean.addPerson}"/>				
		</p>
	</h:form>

</h:body>
</html>

xhtml dosyamizi inceleyecek olursak onceki ornegimizce oldugu gibi h:dataTable tagini ve h:column tagini kullaniyoruz. Burada fazladan birtane h:column daha ekledik

<h:column>
	<f:facet name="header">Delete</f:facet>				
	<h:commandLink value="Delete" action="#{personDataBean.deletePerson(myPerson)}" />
</h:column>

Link’e tikladigimizda deletePerson metodu calisacaktir. Bu metoda arguman olarak ilgili Person objesini geciyoruz.

Kodun devamini inceleyecek olursak input alanlarini yazacagimiz h:inputText componentleri ekledigimizi gorebiliriz. h:commandButton ile addPerson metodunu cagiriyoruz.

Ornegimizi calistirdigimizda;

jsf datatable

Ornegin 2.kaydi silip yeni bir kayit ekledigimde ;

jsf datatable2

Kaynak kodlar : Injavawetrust.jsf
Yazimi burada sonlandiriyorum.
Herkese bol Javali gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *