Create columns dynamically and render their style and other attributes from backing beans.
Also demonstrate how to customize the Handsontable instance using "updateSettings" and "extender" Javascript.
Source
<pe:sheet id="sheet" value="#{sheetDynamicController.sheetRows}" var="row" height="400" rowKey="#{row.id}"
showRowHeaders="true" sortBy="#{row.id}" sortOrder="ascending" width="1000"
widgetVar="sheetWidget" filteredValue="#{sheetDynamicController.filteredSheetRows}"
extender="sheetExtender">
<f:facet name="header">
<h:outputText value="Dynamic Columns"/>
</f:facet>
<pe:sheetcolumn headerText="Id (readOnly)" readOnly="true" value="#{row.id}" colWidth="150"/>
<c:forEach items="#{sheetDynamicController.hoursOfDay}" var="hourOfDay" varStatus="status">
<pe:sheetcolumn styleClass="htRight #{row.cells[status.index].style}"
headerText="#{hourOfDay}"
value="#{row.cells[status.index].value}"
readonlyCell="#{row.readOnly}"
colType="numeric">
<f:converter converterId="javax.faces.Integer"/>
</pe:sheetcolumn>
</c:forEach>
<f:facet name="footer">
<h:outputText value="#{sheetDynamicController.filteredSheetRows.size()} Records"/>
</f:facet>
</pe:sheet>
<style type="text/css">
.cell-blue {
color: blue
}
.cell-orange {
color: darkorange
}
</style>
<script type="text/javascript">
function sheetExtender() {
// this = widget
// this.cfg = JSON configuration
this.cfg.trimWhitespace = true;
};
$(document).ready(function () {
var $hot = PF('sheetWidget').ht;
$hot.updateSettings({
contextMenu: {
callback: function (key, options) {
if (key === 'about') {
setTimeout(function () {
// timeout is used to make sure the menu collapsed before alert is shown
alert("PrimeFaces Extensions Rocks! This is a context menu with default and custom options mixed");
}, 100);
}
},
items: {
"row_above": {
disabled: function () {
// if first row, disable this option
return $hot.getSelected()[0] === 0;
}
},
"row_below": {},
"hsep1": "---------",
"remove_row": {
name: 'Remove this row, ok?',
disabled: function () {
// if first row, disable this option
return $hot.getSelected()[0] === 0
}
},
"hsep2": "---------",
"about": {name: 'About...'}
}
}
});
});
</script>
@Named
@ViewScoped
public class SheetDynamicController implements Serializable {
private static final long serialVersionUID = 20120224L;
private List<DynaSheetRow> sheetRows = new ArrayList<>();
private List<DynaSheetRow> filteredSheetRows = new ArrayList<>();
private List<Integer> hoursOfDay = new ArrayList<>();
public SheetDynamicController() {
addRows(24);
}
private void addRows(final int count) {
for (int i = 0; i < count; i++) {
final DynaSheetRow row = new DynaSheetRow();
row.setId(RandomUtils.nextLong());
row.setReadOnly(false);
final Integer hourOfDay = Integer.valueOf(i);
hoursOfDay.add(hourOfDay);
for (int j = 0; j < count; j++) {
row.getCells().add(DynaSheetCell.create(Integer.valueOf(j), RandomUtils.nextInt(1, 1000)));
}
getSheetRows().add(row);
}
}
public List<DynaSheetRow> getSheetRows() {
return sheetRows;
}
public void setSheetRows(final List<DynaSheetRow> sheetRows) {
this.sheetRows = sheetRows;
}
public List<Integer> getHoursOfDay() {
return hoursOfDay;
}
public void setHoursOfDay(final List<Integer> hoursOfDay) {
this.hoursOfDay = hoursOfDay;
}
public List<DynaSheetRow> getFilteredSheetRows() {
return filteredSheetRows;
}
public void setFilteredSheetRows(final List<DynaSheetRow> filteredSheetRows) {
this.filteredSheetRows = filteredSheetRows;
}
}
public class DynaSheetRow implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private List<DynaSheetCell> cells = new ArrayList<>();
private boolean readOnly;
/**
* Gets the {@link #id}.
*
* @return Returns the {@link #id}.
*/
public Long getId() {
return id;
}
/**
* Sets the {@link #id}.
*
* @param id The {@link #id} to set.
*/
public void setId(final Long id) {
this.id = id;
}
/**
* Gets the {@link #cells}.
*
* @return Returns the {@link #cells}.
*/
public List<DynaSheetCell> getCells() {
return cells;
}
/**
* Sets the {@link #cells}.
*
* @param cells The {@link #cells} to set.
*/
public void setCells(final List<DynaSheetCell> cells) {
this.cells = cells;
}
/**
* Gets the {@link #readOnly}.
*
* @return Returns the {@link #readOnly}.
*/
public boolean isReadOnly() {
return readOnly;
}
/**
* Sets the {@link #readOnly}.
*
* @param readOnly The {@link #readOnly} to set.
*/
public void setReadOnly(final boolean readOnly) {
this.readOnly = readOnly;
}
}
public class DynaSheetCell implements Comparable<DynaSheetCell>, Serializable {
private static final long serialVersionUID = 1L;
private Integer value;
private String style;
private Integer hourOfDay;
public static DynaSheetCell create(final Integer hourOfDay, final Integer value) {
final DynaSheetCell cell = new DynaSheetCell();
cell.setHourOfDay(hourOfDay);
cell.setValue(value);
cell.setStyle(StringUtils.EMPTY);
if (hourOfDay == 2) {
cell.setStyle("cell-blue");
}
if (hourOfDay == 3) {
cell.setStyle("cell-orange");
}
return cell;
}
/**
* Gets the {@link #hourOfDay}.
*
* @return Returns the {@link #hourOfDay}.
*/
public Integer getHourOfDay() {
return hourOfDay;
}
/**
* Sets the {@link #hourOfDay}.
*
* @param hourOfDay The {@link #hourOfDay} to set.
*/
public void setHourOfDay(final Integer hourOfDay) {
this.hourOfDay = hourOfDay;
}
/**
* Gets the {@link #value}.
*
* @return Returns the {@link #value}.
*/
public Integer getValue() {
return value;
}
/**
* Sets the {@link #value}.
*
* @param value The {@link #value} to set.
*/
public void setValue(final Integer value) {
this.value = value;
}
/**
* Gets the {@link #style}.
*
* @return Returns the {@link #style}.
*/
public String getStyle() {
return style;
}
/**
* Sets the {@link #style}.
*
* @param style The {@link #style} to set.
*/
public void setStyle(final String style) {
this.style = style;
}
/**
* {@inheritDoc}
*/
@Override
public int compareTo(final DynaSheetCell other) {
return new CompareToBuilder().append(value, other.value).toComparison();
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(final Object other) {
if (!(other instanceof DynaSheetCell)) {
return false;
}
final DynaSheetCell castOther = (DynaSheetCell) other;
return new EqualsBuilder().append(getValue(), castOther.getValue())
.append(getHourOfDay(), castOther.getHourOfDay()).isEquals();
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return new HashCodeBuilder().append(getValue()).append(getHourOfDay())
.toHashCode();
}
}