p:dashboard 模型动态更新的问题

Problem with p:dashboard model dynamically update

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

首先,我创建一个空的仪表板: 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. Fist foreach 验证用户向模型中插入了多少个小部件
  2. 第二个 foreach 使用名为 "booking" 的不同元素填充每个面板。

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

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;

}

在这部分代码中,我做了很多事情,但重要的是 "create" 并添加了一个新的空列。我创建了一个 DashboardColumn 列表以在 java 代码中使用,称为 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>

当有人决定添加一个 Widget 时,我使用 onAddPanel

方法修改模型

JAVA ---->

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 createDashboardDinamically() {

    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 仪表板。非常感谢大家,我被卡住了:(

我找到了这个解决方案:

这是一个例子,任何人都会发现我在创建 Dynamic Dashboard(一切都是动态)。

My problem question

项目是: 显示一个空的仪表板,用户将在 [=73= 的右侧可视化一个菜单]FORM 有一个可用的会议室列表。 单击一个会议室后,模型将更新,面板将在右栏中可视化(预分配了其 ID)。 我动态分配每个 ID、列数、面板数和每个面板内可视化的元素数。


首先,使用空列填充 Java DashboardModel

    private DashboardModel model;
    private List<DashboardColumn> dashboardColumn;

    DashboardColumn columnTmp = new DefaultDashboardColumn();
    model.addColumn(columnTmp);
    columnDashboard.add(columnTmp);

逻辑上你必须创建 gettersetter为模型。 我创建了一个 DashboardColumn 列表,以便于管理每一列。

填充名为 meetingRoomByOffice 的会议室列表 object。 actionMenu 需要此列表才能在菜单中创建动态元素。

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

我填充一个二维数组以保存每列的 ID(column1 = 1 office 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 中创建 元素 第一个是 actionMenu

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

<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. 首先foreach --->获取一个元素列表,然后用meetingRoomElement"name"一个一个浏览它们
  2. 为了获取 ID,我执行了一个特定的方法调用 getMenuItemId,它生成一个 ID,其中包含会议室的名称,已拆分。像这样的东西:名称:Sala Demo --> ID:Sala__Demo
  3. action调用了修改模型的main方法,我会在这个列表后面说明。
  4. update ="widgetBookingReception">重要更新整个FORM

方法onAddPanel

我有一个特殊的 class 有两个主要 object :

  • idWidget是字符串
  • meetingRoom这是一个objectMeetingRoom

首先,我用一个下划线创建ID

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

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

    DashboardWidget tmp = new DashboardWidget();

    tmp.setIdWidget(idWidget);
    tmp.setName(panelSelected.getName());
    tmp.setOffice(panelSelected.getOffice());

    widgets.add(tmp);

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

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 中不能传递 null List 或空 List。我使用 WHEN 来防止这种情况。

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