<table ... >
<contents>
<!-- the first column stamp is a text node -->
<column>
<contents>
<text text="SampleText"/>
</contents>
</column>
</contents>
</table>
<dataScope xmlns="http://xmlns.oracle.com/uix/ui">
<provider>
<!-- all the data used by our table demo -->
<data name="demoTableData">
<inline>
<!-- all the row DataObjects used by our table for tableData -->
<demoRowData/>
<demoRowData/>
<demoRowData/>
</inline>
</data>
</provider>
<contents>
<table tableData="${uix.data.demoTableData.demoRowData}">
<contents>
<column>
<contents>
<!-- the first column stamp is a text node -->
<text text="SampleText"/>
</contents>
</column>
</contents>
</table>
</contents>
</dataScope>
<dataScope ... >
<provider>
<!-- all the data used by our table demo -->
<data name="demoTableData">
<inline>
<!-- all the row DataObjects used by our table for tableData -->
<demoRowData/>
<demoRowData/>
<demoRowData/>
<demoRowData/>
</inline>
</data>
</provider>
<contents>
...
</contents>
</dataScope>
<dataScope ... >
<provider>
<!-- all the data used by our table demo -->
<data name="demoTableData">
<inline>
<!-- all the row DataObjects used by our table for tableData -->
<demoRowData someText="First row"/>
<demoRowData someText="Second row"/>
<demoRowData someText="Third row"/>
<demoRowData someText="Fourth row"/>
</inline>
</data>
</provider>
<contents>
<table tableData="${uix.data.demoTableData.demoRowData}">
<contents>
<column>
<contents>
<!-- the first column stamp, a text node -->
<text text="${uix.current.someText}"/>
</contents>
</column>
<column>
<contents>
<!-- the second column stamp, a button -->
<button text="${uix.current.someText}" destination="http://www.oracle.com"/>
<contents>
</column>
</contents>
</table>
</contents>
</dataScope>
package test;
public class MyTable
{
public static DataObject getDirectoryData(RenderingContext context,
String namespace,
String name)
{
// Make sure this directory exists on your file system
return new DirDataObjectList(new File("/home/user/"));
}
private static final class DirDataObjectList
implements DataObjectList, DataObject
{
public DirDataObjectList(File dir)
{
_files = dir.listFiles();
}
public int getLength()
{
return _files.length;
}
public DataObject getItem(int index)
{
// in a more prudent implementation, we would be caching these
// DataObjects, rather than creating new ones each time.
return new FileDataObject(_files[index]);
}
public Object selectValue(RenderingContext context, Object key)
{
// we don't support any properties on this DataObject, since this is
// primarily a list of DataObjects.
return null;
}
private final File[] _files;
}
private static final class FileDataObject implements DataObject
{
public FileDataObject(File file)
{
_file = file;
}
/**
* This DataObject recognizes two keys: name which gives the
* file name, and length which gives the file length.
*/
public Object selectValue(RenderingContext context, Object key)
{
if ("name".equals(key))
return _file.getName();
else if ("length".equals(key))
return new Long(_file.length());
return null;
}
private final File _file;
}
}
<dataScope ... >
<provider>
<!-- all the data used by our table demo -->
<data name="demoTableData">
<inline>
<!-- all the row DataObjects used by our table for tableData -->
<demoRowData firstColumnText="First row" secondColumnText="Button #1"/>
<demoRowData firstColumnText="Second row" secondColumnText="Button #2"/>
<demoRowData firstColumnText="Third row" secondColumnText="Button #3"/>
<demoRowData firstColumnText="Fourth row" secondColumnText="Button #4"/>
</inline>
</data>
</provider>
<contents>
<table tableData="${uix.data.demoTableData.demoRowData}">
<contents>
<!-- the first column stamp, a text node -->
<column>
<!-- add a columnHeader named child for the column header -->
<columnHeader>First Header</columnHeader>
<contents>
<text text="${uix.current.firstColumnText}"/>
</contents>
</column>
<!-- the second column stamp, a button -->
<column>
<!-- add a columnHeader named child for the column header -->
<columnHeader>Second Header</columnHeader>
<contents>
<button text="${uix.current.secondColumnText}" destination="http://www.oracle.com"/>
</contents>
</column>
</contents>
</table>
</contents>
</dataScope>
public static EventResult doSubmitEvent(BajaContext bc, Page page,
PageEvent event)
{
// create a new FlattenedDataSet for the table "myTestTable"
DataSet tableInputs = new PageEventFlattenedDataSet(event,
"myTestTable");
StringBuffer s = new StringBuffer(40);
// this would be the number of rows in the table
int sz = tableInputs.getLength();
for(int i=0; i<sz ;i++)
{
// get the DataObject representing all the input elements on the current
// table row.
DataObject row = tableInputs.getItem(i);
// get the value of the input element named "foo". we can safely use
// null for the RenderingContext here:
Object value = row.selectValue(null, "foo");
s.append(value);
}
EventResult result = new EventResult(page);
result.setProperty("case", "submit");
// store the concatenation of the values of all the "foo" elements on the
// EventResult
result.setProperty("result", s);
return result;
}
public class TableDemo {
public static EventResult doGotoEvent(BajaContext bc, Page page,
PageEvent event)
{
// if this is a "goto" event, then we need to get the "value" parameter to
// figure out what our start index is. If this is not a "goto" event, then
// we want to start at index "1"
String valueParam = ((event!=null) &&
UIConstants.GOTO_EVENT.equals(event.getName()))
? event.getParameter(UIConstants.VALUE_PARAM)
: "1";
// the "value" parameter starts at "1"; however, our data is zero based,
// so adjust the offset
int value = Integer.parseInt(valueParam)-1;
DataObjectList tableData = new PagedDataObjectList(_TABLE_DATA,
_BLOCK_SIZE.intValue(),
value); //start index
// in a more efficient implementation, we would not use DictionaryData;
// instead, we would implement our own DataObject
DictionaryData data = new DictionaryData();
// we need to add one here, since our data is zero based, but the table
// start index must start at 1
data.put("value", new Integer(value+1));
data.put("size", _BLOCK_SIZE);
data.put("maxValue", new Integer(_TABLE_DATA.getLength()));
data.put("current", tableData);
EventResult result = new EventResult(page);
result.setProperty("tableData", data);
return result;
}
// we want to render at most 30 rows on a single page
private static final Integer _BLOCK_SIZE = new Integer(30);
// create some dummy data with 998 rows:
private static final DataObjectList _TABLE_DATA = new DummyData(998);
private static final class DummyData implements DataObjectList
{
public DummyData(int size)
{
_size = size;
}
public int getLength()
{
return _size;
}
public DataObject getItem(final int index)
{
return new DataObject()
{
public Object selectValue(RenderingContext rc, Object key)
{
return "Test Data "+(index+1);
}
};
}
private final int _size;
}
}
<dataScope ... >
<provider>
<!-- all the data used by our table demo -->
<data name="demoTableData">
<inline>
<!-- no rows in this example! -->
...
</inline>
</data>
</provider>
<contents>
<table tableData="${uix.data.demoTableData.demoRowData}"
...
alternateText="(No search results were found)">
...
</table>
</contents>
</dataScope>
public static EventResult doSortEvent(BajaContext bc, Page page,
PageEvent event)
{
EventResult result = new EventResult(page);
result.setProperty(UIConstants.VALUE_PARAM,
event.getParameter(UIConstants.VALUE_PARAM));
// if we are already sorting in ascending order, then we want to sort in
// descending order. Otherwise, sort in ascending order
Object state = event.getParameter(UIConstants.STATE_PARAM);
result.setProperty(UIConstants.STATE_PARAM,
UIConstants.SORTABLE_ASCENDING.equals(state)
? UIConstants.SORTABLE_DESCENDING
: UIConstants.SORTABLE_ASCENDING);
return result;
}
private static final DataObject _SORT_COLUMN_HEADER = new DataObject() {
public Object selectValue(RenderingContext rc, Object key)
{
BajaContext bc = BajaRenderingContext.getBajaContext(rc);
EventResult er = EventResult.getEventResult(bc);
if (er!=null)
{
// check to see if it is this column that has been sorted. We assume
// that "key" is the "value" attribute of this column header.
if (key.equals(er.getProperty(UIConstants.VALUE_PARAM)))
return er.getProperty(UIConstants.STATE_PARAM);
}
return null;
}
};
<table ... >
<column>
<columnHeader>
<!-- first try to get the value from the sortColumnHeader
dataObject. if the boundValue returns null, use sortable=yes-->
<sortableHeader text="Name"
value="name"
sortable="${ui:defaulting(uix.data.sortColumnHeader.name, 'yes')}">
</sortableHeader>
</columnHeader>
<contents>
<text text="${uix.current.name}"/>
</contents>
</column>
<column>
<columnHeader>
<!-- first try to get the value from the sortColumnHeader
dataObject. if the boundValue returns null, use sortable=yes--><sortableHeader text="Age"
value="age"
sortable="${ui:defaulting(uix.data.sortColumnHeader.age, 'yes')}">
</sortableHeader>
</columnHeader>
<contents>
<text text="${uix.current.age}"/>
</contents>
</column>
<column>
<columnHeader>
<!-- first try to get the value from the sortColumnHeader
dataObject. if the boundValue returns null, use sortable=yes--><sortableHeader text="Blood Group"
value="blood"
sortable="${ui:defaulting(uix.data.sortColumnHeader.blood, 'yes')}">
</sortableHeader>
</columnHeader>
<contents>
<text text="${uix.current.blood}"/>
</contents>
</column>
<column>
<columnHeader>
Phone
</columnHeader>
<contents>
<text text="${uix.current.phone}"/>
</contents>
</column>
...
</table>
public static DataObject getSortedTableData(RenderingContext rc,
String namespace, String name)
{
DataObject[] data;
BajaContext bc = BajaRenderingContext.getBajaContext(rc);
EventResult er = EventResult.getEventResult(bc);
if (er!=null)
{
// we need to clone because we are going to mutate the array:
data = (DataObject[]) _TABLE_DATA.clone();
Object state = er.getProperty(UIConstants.STATE_PARAM);
Object key = er.getProperty(UIConstants.VALUE_PARAM);
Comparator comp = new DataObjectComparator(
rc,
key, // this is the key to sort the DataObjects on
UIConstants.SORTABLE_ASCENDING.equals(state));
Arrays.sort(data, comp);
}
else
data = _TABLE_DATA;
return new ArrayDataSet(data);
}
public static EventResult doSelectionEvent(BajaContext bc, Page page,
PageEvent event)
{
DataObject tableRows = new PageEventFlattenedDataSet(event, "table1");
int index = SelectionUtils.getSelectedIndex(tableRows);
String name = "Nothing Selected";
// make sure that something was selected:
if (index>=0)
{
DataObject row = _TABLE_DATA.getItem(index);
name = row.selectValue(null, "name").toString();
}
EventResult result = new EventResult(page);
result.setProperty("action", event.getName());
result.setProperty("name", name);
return result;
}
<multipleSelection selected="${uix.data.selectedKey}" ...>
<selection>
<!-- create a dataObjectList, each dataObject has a selectedKey
whose value is either true or false -->
<row selectedKey="true"/> <!-- select the first row -->
<row selectedKey="false"/>
<row selectedKey="true"/> <!-- select the third row -->
...
</selection>
...
</multipleSelection>
private static final class SelectedList implements DataObjectList
{
public SelectedList(DataObjectList data, int[] selectedIndices)
{
_data = data;
_indices = selectedIndices;
}
// returns the number of selected rows
public int getLength()
{
return _indices.length;
}
// gets the selected row at the given index.
public DataObject getItem(int index)
{
return _data.getItem(_indices[index]);
}
private final DataObjectList _data;
private final int[] _indices;
}
public static EventResult doHideShowEvent(BajaContext bc, Page page,
PageEvent event)
{
PageEventFlattenedDataSet tableRows =
new PageEventFlattenedDataSet(event, "table1");
// this is the row that must be (un)disclosed:
int row = Integer.parseInt(event.getParameter(UIConstants.VALUE_PARAM));
// decide whether we want to disclose or undisclose depending on the name
// of the event
boolean disclose = UIConstants.SHOW_EVENT.equals(event.getName());
DataObjectList detailData = new DetailData(tableRows, row, disclose);
EventResult result = new EventResult(page);
result.setProperty("detailData", detailData);
return result;
}
private static final class DetailData implements DataObjectList
{
/**
* @param pageEvent contains the current disclosure state of the table
* @param index the index of the row that must have its disclosure state
* changed
* @param disclosure the new disclosure state for the row
*/
public DetailData(DataObjectList pageEvent, int index, boolean disclose)
{
_pageEvent = pageEvent;
// initially, none of the table rows will be disclosed, so there will be
// no pageEvent data and this length would be zero:
_length = pageEvent.getLength();
_index = index;
_disclose = disclose;
}
public int getLength()
{
// make sure that the length we return is sufficiently large enough that
// we reach the index we want to change
return (_index >= _length) ? _index+1 : _length;
}
public DataObject getItem(int index)
{
boolean disclose;
if (index==_index)
{
// this is the index that we want to change.
disclose = _disclose;
}
else if (index < _length)
{
// this index can safely be pulled from the pageEvent
DataObject row = _pageEvent.getItem(index);
// if there was a "disclosed" form element on this row then we
// consider the row disclosed:
disclose = (row.selectValue(null, "disclosed") != null);
}
else
disclose = false;
return disclose ? _DISCLOSE_TRUE : _DISCLOSE_FALSE;
}
private final DataObjectList _pageEvent;
private final int _index, _length;
private final boolean _disclose;
private static final DataObject _DISCLOSE_TRUE = new DataObject() {
public Object selectValue(RenderingContext rc, Object key)
{
return Boolean.TRUE;
}
};
private static final DataObject _DISCLOSE_FALSE = new DataObject() {
public Object selectValue(RenderingContext rc, Object key)
{
return Boolean.FALSE;
}
};
}
<table ... >
<contents>
<column>
<columnHeader>First Header</columnHeader>
<contents>
<!-- the first column stamp, a text node -->
<textInput name="foo" text="${uix.current.firstColumnText}"/>
</contents>
</column>
<column>
<!-- column footer is a textInput. This is used with totalRow -->
<footer>
<textInput columns="5" name="total" text="42"/>
</footer>
<columnHeader>Second Header</columnHeader>
<!-- the second column stamp, a button -->
<contents>
<button destination="http://www.oracle.com"
text="${uix.current.secondColumnText}"/>
</contents>
</column>
</contents>
<!-- table footer is a totalRow -->
<footer>
<tableFooter>
<total>
<totalRow/>
</total>
</tableFooter>
</footer>
</table>
<table ... >
<contents>
<column>
<columnHeader>First Header</columnHeader>
<contents>
<!-- the first column stamp, a text node -->
<textInput name="foo" text="${uix.current.firstColumnText}"/>
</contents>
</column>
<column>
<!-- column footer is a textInput. This will be used with totalRow -->
<footer>
<textInput columns="5" name="total" text="42"/>
</footer>
<columnHeader>Second Header</columnHeader>
<!-- the second column stamp, a button -->
<contents>
<button destination="http://www.oracle.com"
text="${uix.current.secondColumnText}"/>
</contents>
</column>
</contents>
<!-- table footer contains both addTableRow and totalRow -->
<footer>
<tableFooter>
<total>
<totalRow/>
</total>
<contents>
<addTableRow/>
</contents>
</tableFooter>
</footer>
</table>
function updateTotal()
{
var proxy = new TableProxy('table1');
var rowCount = proxy.getLength();
var total = 0;
for(var i=0; i<rowCount; i++)
{
var currTextField = proxy.getFormElement('cost', i);
// the minus zero here is necessary to convert the string value
// to a number
total += currTextField.value - 0;
}
document.form1.total.value = total;
}
function hire()
{
var proxy = new TableProxy('table1');
var row = proxy.getSelectedRow();
// check to make sure that something was selected
if (row < 0)
{
alert('You have not chosen anyone!');
}
else
{
var name = proxy.getFormElement('theName', row).value;
alert('You have chosen to hire '+name);
}
}
function recruit()
{
var proxy = new TableProxy('table1');
var rows = proxy.getSelectedRows();
var length = rows.length;
// make sure that something was selected
if (length > 0)
{
var list = "";
// loop through each selected row and concatenate the name
for(var i=0; i < length; i++)
{
// get the next selected row index
var row = rows[i];
// get the selected row (from the index) and pull out the name
var name = proxy.getFormElement('theName', row).value;
list += '\n'+name;
}
alert("You have chosen to recruit "+list);
}
else
{
alert("You have not chosen anyone to recruit!");
}
}