p:仪表板模型动态更新的问题



我正在尝试开发一个特定的仪表板。我想创建一个初始的空仪表板,并用元素动态填充它。

首先,我创建一个空的仪表板: XHTML --->

<p:dashboard id="dash" model ="#{homeReceptionDashboardBck.model}">
<c:forEach 
items="#{homeReceptionDashboardBck.widgets}" 
var="widgetsElement">
<p:panel id = "widgetsElement.idWidget" header="widgetsElement.name">
<c:forEach 
items="#{homeReceptionDashboardBck.uploadBooking(widgetsElement)" 
var="bookings">
<h:outputText value="#{booking.owner.ragioneSociale}"> 
</h:outputText>
</c:forEach>
</p:panel>
</c:forEach>
</p:dashboard>

这是我用来使用 动态填充和创建每个面板的代码。

用于验证用户插入模型的小
  1. 部件数量
  2. 第二个foreach用称为"预订"的不同元素填充每个面板。

我创建了一个空模型,该模型在应用程序启动时加载:

for(i = 0;i<officesList.size();i++) {
DashboardColumn columnTmp = new DefaultDashboardColumn();
model.addColumn(columnTmp);
columnDashboard.add(columnTmp);
List<MeetingRoom> tempMeetingRoom = new ArrayList<>();
tempMeetingRoom = meetingmr.getAllByOffice(
officesList.get(i).getId(), 
true, 
JSFUtil.getActiveUser());
for(x = 0;x<tempMeetingRoom.size();x++)
{
//popolo la lista che mi serve all'inizio per la lista dei Panel da inserire
meetingRoomByOffice.add(tempMeetingRoom.get(x));
}
officePositionAndColumn[i][0] = (int) (long) officesList.get(i).getId();
officePositionAndColumn[i][1] = i;
}

在代码的这一部分中,我正在做很多事情,但重要的是"创建"并添加新的空列。我创建了一个要在java代码中使用的DashboardColumn列表,称为columnDashboard


我创建了一个特定的菜单来在模型内添加一个小部件: XHTML --->

<smifn:actionsMenu id="tableAddMeetingRoom" styleClass="col-xs-8 col-md-12 text-right"
title="#{msg['booking.add_meeting_room']}">
<c:forEach items="#{homeReceptionDashboardBck.meetingRoomByOffice}" 
var="meetingRoomElement">
<p:menuitem id="#{homeReceptionDashboardBck.getMenuItemId(meetingRoomElement)}" 
actionListener="#{homeReceptionDashboardBck.onAddPanel(meetingRoomElement)}" 
process="@this" partialSubmit="true"
value="#{meetingRoomElement.name}" 
icon="icon-add-3" 
iconPos="left"
>
<p:ajax process="widgetBookingReception" update="widgetBookingReception"/>
</p:menuitem>       <!--  oncomplete="PF('bookingSelect').show()"-->
</c:forEach>
</smifn:actionsMenu>

当有人决定添加一个小部件时,我用方法修改模型onAddPanel

爪哇---->

public void onAddPanel(MeetingRoom panelSelected) {
int i;
//splitto il nome per creare l'id
String [] nameMeetingRoom = panelSelected.getName().split(" ");
String idWidget = nameMeetingRoom[0]+"_"+nameMeetingRoom[1];
//oggetto di tipo DashboardWidget
DashboardWidget tmp = new DashboardWidget();
//imposto l'id, nome della stanza e l'ufficio relativo
tmp.setIdWidget(idWidget);
tmp.setName(panelSelected.getName());
tmp.setOffice(panelSelected.getOffice());
//aggiungo alla lista dei widget l'oggetto relativo
widgets.add(tmp);
//stringa per la posizione della colonna
int columnPosition = -1;
//faccio un for per passare tutte le colonne relative
for(i = 0;i<officePositionAndColumn.length;i++)
{
//se l'id che era stato impostato è uguale a quello passato 
if(officePositionAndColumn[i] [0] == panelSelected.getOffice().getId())
//posizione della colonna che va da 0 a n.
columnPosition = officePositionAndColumn[i][1];
}
//se è stato trovata la posizione della colonna
if(columnPosition>=0) {
//mi ricavo la colonna 
DashboardColumn columnSelected = new DefaultDashboardColumn();
columnSelected = columnDashboard.get(columnPosition);
//imposto l'id al widget
columnSelected.addWidget(idWidget);
//sovrascrivo la colonna con quella modificata
columnDashboard.set(columnPosition, columnSelected);
//reimposto il modello e ricarico le colonne
setModel(new DefaultDashboardModel());
for(i = 0;i<columnDashboard.size();i++)
{
this.model.addColumn(columnDashboard.get(i));
}
for(i = 0;i<meetingRoomByOffice.size();i++)
{
if(meetingRoomByOffice.get(i).equals(panelSelected))
{
meetingRoomByOffice.remove(i);
}
}}

我已经验证了这种方法,但我无法可视化结果。 如何更新仪表板的模型? 我已经尝试过了,但我认为这应该不起作用;(

public void createDashboardDinamally(( {

int i;
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
dashboard = (Dashboard) app.createComponent(
fc, 
"org.primefaces.component.Dashboard",
"org.primefaces.component.DashboardRenderer");
dashboard.setId("dash");
dashboard.setModel(model);
Panel panel1 = (Panel) app.createComponent(
fc, 
"org.primefaces.component.Panel", 
"org.primefaces.component.PanelRenderer");
panel1.setId("Sala_Demo");
panel1.setToggleable(true);
panel1.setClosable(true);
panel1.setHeader("Sala Demo");
for(i = 0;i<widgets.size();i++)
{
Panel panel = (Panel) app.createComponent(
fc, 
"org.primefaces.component.Panel", 
"org.primefaces.component.PanelRenderer");
panel.setId(widgets.get(i).getIdWidget());
panel.setToggleable(true);
panel.setClosable(true);
panel.setHeader(widgets.get(i).getName());
getDashboard().getChildren().add(panel);
}

有人可以帮助我吗?如果它有效,我将共享整个代码以动态填充PrimeFaces Dashboard。非常感谢伙计们,我被困住了:(

我找到了这个解决方案:

对于任何会发现我在创建动态仪表板时遇到的相同困难的人来说,这是一个示例(一切都是动态的(。

我的问题问题

该项目是: 显示一个空的仪表板,用户将在表单的右侧可视化一个菜单,并提供会议室。单击一个会议室后,模型将更新,面板将在右列中可视化(预先分配其ID(。 我动态分配每个ID,列数,面板数和每个面板内可视化的元素数。


首先,用空列填充一个 Java DashboardModel

private DashboardModel model;
private List<DashboardColumn> dashboardColumn;
DashboardColumn columnTmp = new DefaultDashboardColumn();
model.addColumn(columnTmp);
columnDashboard.add(columnTmp);

从逻辑上讲,您必须为模型创建gettersetter。 我创建了一个仪表板列列表,以便轻松管理每一列。

填充名为 meetingRoomByOffice 的会议室对象列表。 此列表对于actionMenu在菜单中创建动态元素是必需的。

for(x = 0;x<tempMeetingRoom.size();x++)
{
meetingRoomByOffice.add(tempMeetingRoom.get(x));
}

我填充一个二维数组以保存每列的 ID( 列 1 = 1 办公室 ID(与列表中的实际位置(ID : 450 - 列表内的位置 0 等(

for(i = 0;i<officesList.size();i++) {
officePositionAndColumn[i][0] = (int) (long) officesList.get(i).getId();
officePositionAndColumn[i][1] = i;
}

在此之后,在 XHTML中创建元素第一个是动作菜单

注意我有一个个性化的,没有特别的修改

<smifn:actionsMenu id="tableAddMeetingRoom" styleClass="col-xs-8 col-md-12 text-right"
onclick="@this.update()"
title="#{msg['booking.add_meeting_room']}">
<c:forEach items="#{homeReceptionDashboardBck.meetingRoomByOffice}" var="meetingRoomElement">
<p:menuitem id="#{homeReceptionDashboardBck.getMenuItemId(meetingRoomElement)}" 
action="#{homeReceptionDashboardBck.onAddPanel(meetingRoomElement)}" 
process="@this"
value="#{meetingRoomElement.name}" 
icon="icon-add-3" 
iconPos="left"
update ="widgetBookingReception">
</p:menuitem>       <!--  oncomplete="PF('bookingSelect').show()"-->
</c:forEach>
</smifn:actionsMenu>
  1. 首先,--->获取元素列表,并使用 meetingRoomElement 的"名称"逐个浏览它们
  2. 为了获取 ID,我做了一个特定的方法调用 getMenuItemId,它生成一个带有会议室名称的 ID,拆分。像这样的东西:名称:萨拉演示 --> ID :Sala__Demo
  3. action调用main方法来修改模型,我在这个列表后面解释一下。
  4. update ="widgetBookingReception">更新整个表单很重要

方法添加面板

我有一个特殊的类,有两个主要对象:

  • idWidget它是一个字符串
  • 会议室
  • 是对象会议室

首先,我创建一个带有下划线的 ID

String [] nameMeetingRoom = panelSelected.getName().split(" ");
String idWidget = nameMeetingRoom[0]+"_"+nameMeetingRoom[1];

创建并将其添加到TMP对象中,然后将"小部件"插入到用户在模型中插入的小部件列表中。这对于避免丢失小部件非常重要。

DashboardWidget tmp = new DashboardWidget();
tmp.setIdWidget(idWidget);
tmp.setName(panelSelected.getName());
tmp.setOffice(panelSelected.getOffice());
widgets.add(tmp);

现在,我将在列表中找到位置,更新模型和菜单中可用的会议室列表。

for(i = 0;i<officePositionAndColumn.length;i++)
{
//se l'id che era stato impostato è uguale a quello passato 
if(officePositionAndColumn[i] [0] == panelSelected.getOffice().getId())
//posizione della colonna che va da 0 a n.
columnPosition = officePositionAndColumn[i][1];
}
//se è stato trovata la posizione della colonna
if(columnPosition>=0) {
//mi ricavo la colonna 
DashboardColumn columnSelected = new DefaultDashboardColumn();
columnSelected = columnDashboard.get(columnPosition);
//imposto l'id al widget
columnSelected.addWidget(idWidget);
//sovrascrivo la colonna con quella modificata
columnDashboard.set(columnPosition, columnSelected);
//reimposto il modello e ricarico le colonne
setModel(new DefaultDashboardModel());
for(i = 0;i<columnDashboard.size();i++)
{
this.model.addColumn(columnDashboard.get(i));
}
for(i = 0;i<meetingRoomByOffice.size();i++)
{
if(meetingRoomByOffice.get(i).equals(panelSelected))
{
meetingRoomByOffice.remove(i);
}
}

这就是我更新整个模型的方式。 但是现在我们必须将其更新到XHTML中。

我创建了一个完整的动态仪表板,这是代码:

<p:dashboard widgetVar="dash" model ="#{homeReceptionDashboardBck.model}">
<c:when test="not empty #{homeReceptionDashboardBck.widgets}">
<c:forEach items="#{homeReceptionDashboardBck.widgets}" var="Element">
<p:panel id="#{Element.idWidget}" header="#{Element.name}">
<c:when test="not empty #{homeReceptionDashboardBck.uploadBooking(Element)}">
<c:forEach items="#{homeReceptionDashboardBck.uploadBooking(Element)}" var="row">
<h:outputText value="#{homeReceptionDashboardBck.getEventGeneralInfo(row)}"> </h:outputText>
<h:outputText> <br/></h:outputText>
</c:forEach>
</c:when>
<c:otherwise>
<c:when test="#{homeReceptionDashboardBck.uploadBooking(Element)}">
<h:outputText value="Nessun evento prenotato"> </h:outputText>
<h:outputText> <br/></h:outputText>
</c:when>
</c:otherwise>
</p:panel>
</c:forEach>
</c:when>
</p:dashboard>

请注意,在foreach内部,您不能传递空列表或空列表。我使用WHEN来防止这种情况。

如果您有任何问题,我很乐意回答并分享我的部分代码。再见:D

最新更新