【问题标题】:To get the row number of the selected primefaces datatable row获取所选primefaces数据表行的行号
【发布时间】:2014-01-23 15:44:09
【问题描述】:

我有一个 primefaces 数据表,我需要在 JSF 页面中显示(选定的行号)(总行数)。我可以使用 rowIndexVar 属性获取显示在其中一列中的行号,但我没有得到任何想法在行选择的输入文本中分别显示相同的数字。

我需要在 JSF 页面或托管 bean 中做什么才能获得选定的行号。

请在这方面帮助我。

下面是我的 JSF 页面

<p:dataTable id="workSpaceList" var="data"
            value="#{workSpaceBean.lpInfoList}" widgetVar="multiSelection"
            selection="#{workSpaceBean.selectedRows}" resizableColumns="true"
            liveScroll="true" scrollRows="55" scrollWidth="85%"
            scrollHeight="81%" styleClass="datatable" 
            scrollable="true" rowIndexVar="rowIndex"
            filteredValue="#{workSpaceBean.filteredWorkSpaceItems}">

            <p:column selectionMode="multiple" style="width:3%" />
            <p:column headerText="#" style="width:3%">
                #{rowIndex+1}
            </p:column>
            <p:column headerText="Insured" filterBy="#{data.insuredName}"
                sortBy="#{data.insuredName}" style="width:24%">
                <h:outputText value="#{data.insuredName}" />
                <!--   style="width:250px" -->
            </p:column>

            <p:column headerText="City" filterBy="#{data.custAddress_City}"
                sortBy="#{data.custAddress_City}" style="width:12%">
                <h:outputText value="#{data.custAddress_City}" />
            </p:column>
            .
            .
            .
            .

        </p:dataTable>

【问题讨论】:

  • @alpha.. 你是否设法同时使用实时滚动和延迟加载?如果是的话,请告诉我你是怎么做到的,这对我来说是一个表演上衣。
  • @arvin_codeHunk:我已经实现了,但它并没有提高我的应用程序的性能,这就是我选择同时拥有这两个功能的原因。到目前为止,我已经停止了这方面的工作,我会接受几天后这个应用程序编码。
  • 好的...请查看此帖子,stackoverflow.com/questions/23195131/…

标签: jsf-2 primefaces datatable


【解决方案1】:

我相信没有直接的方法可以做到这一点。虽然使用两个 ajax 请求并不漂亮,但至少可以达到使用普通 PrimeFaces 时所期望的结果。如果您将 p:ajax 替换为 PrimeFaces 扩展 pe:javascript,则可以将其减少为一次调用,该扩展不会与服务器进行往返

数据表呈现的每一行 (tr) 都有一个名为 data-rk 的属性和你的 rowKey 和另一个名为 data-ri 的属性和你的 rowIndexVar 值。

你可以通过dtWidgetVar.selection获取data-rk属性(dtWidgetVar是你数据表中widgetVar的名字)。

您现在可以使用 remoteCommand 将 indexRow 发送到您的模型

这是我用来测试它的代码:

观点

<p:remoteCommand name="displayIndex" process="@this" update="index" actionListener="#{viewMBean.displayRowIndex}"/>

<p:dataTable id="dt" var="data"
             value="#{viewMBean.dataModel}" 
             selection="#{viewMBean.selectedRow}"
             selectionMode="single"
             widgetVar="dtVar"
             rowIndexVar="index">
    <p:ajax event="rowSelect" 
            oncomplete="displayIndex([{name:'index', value:jQuery('tr[data-rk=' + dtVar.selection + ']').attr('data-ri')}])" process="@this" />
    <p:column headerText="#">
        #{index + 1}
    </p:column>
    <p:column headerText="Dados">
        #{data.name}
    </p:column>
</p:dataTable>
<br />
Row Index: <p:inputText id="index" value="#{viewMBean.index}" />

托管 Bean

public void displayRowIndex() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map map = context.getExternalContext().getRequestParameterMap();
    String pIndex = (String) map.get("index");
    index = Integer.parseInt(pIndex);
}

如果您使用复选框选择,您可以像这样检索选定的索引:

function beforeDisplayingIndexes(){
    var indexes = "";
    jQuery("tbody .ui-chkbox-box").each(function(){
      if (jQuery(this).hasClass("ui-state-active")){
        indexes = indexes + (indexes === "" ? "" : ",") + jQuery(this).closest("tr").attr("data-ri");
      }
    });
    //for debuging only
    console.log(indexes);
    displayIndex([{name:'index', value:indexes}])
}

您现在应该能够对您的代码进行适当的修改以利用它。

【讨论】:

  • 感谢 Daniel 的回复。我遵循了您的建议,但在选择任何行时它只显示零 :(。我使用您的建议开发了一个小项目,请在此链接中查看 pastebin.com/embed_js.php?i=hJcsvwi4跨度>
  • 我更新了我的答案。请注意,在我的例子中,我设置了属性 rowIndexVar。尝试将其添加到您的代码中。
  • 另一件事,我有一个要求,在选择复选框时也必须包含此功能。它工作正常,但如果选择多个复选框,它会抛出 javascript 错误“语法错误,无法识别的表达式: tr[data-rk=4022,4021]"。请建议我如何解决它。我对 jquery 一无所知。
  • 你在使用复选框选择吗?
  • 事实上,我需要在行选择和复选框选择时都执行此操作。
【解决方案2】:

我对另一个答案中的复杂解决方案感到惊讶。假设您在进行“选择”时拥有#{workSpaceBean.lpInfoList} 服务器端,您可以轻松做到(根据 PrimeFaces 展示的代码,根据您的需要进行调整)

<p:ajax event="rowSelect" listener="#{dtSelectionView.onRowSelect}" update="..." />
public void onRowSelect(SelectEvent event) {
    int rownum = cars.indexOf((Car)event.getObject());

}

【讨论】:

  • 复杂的答案也适用于惰性数据模型,但您的解决方案也可以适用于惰性数据模型页面。
  • @JasperdeVries:请做 ;-)
  • 基本上将cars列表替换为load方法返回的当前页面列表。
猜你喜欢
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多