获取所选原始数据行的行号 [英] To get the row number of the selected primefaces datatable row

查看:187
本文介绍了获取所选原始数据行的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

请帮助我在这方面。



以下是我的JSF页面

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

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

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





< / p:dataTable>


解决方案

我相信没有一个直接的方法所以。虽然使用两个ajax请求不太漂亮,但您可以至少达到预期的结果。



您的datatable渲染的每一行(tr)都有一个名为 data-rk 与您的rowKey和另一个名为 data-ri 的属性与您的rowIndexVar值。



您可以通过 dtWidgetVar.selection 获取 data-rk 属性(dtWidgetVar是widgetVar的名称您的datatable)。



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



这是代码我曾经测试过:



视图



 < p:remoteCommand name =displayIndexprocess =@ thisupdate =indexactionListener =#{viewMBean.displayRowIndex} /> 

< p:dataTable id =dtvar =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 />
行索引:< p:inputText id =indexvalue =#{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)){
索引=索引+(索引===?:,)+ jQuery(this).closest(tr)。attr(data-ri);
}
});
//仅用于调试
console.log(indexes);
displayIndex([{name:'index',value:indexes}])
}

您现在应该可以对您的代码进行适当的修改,以便利用这一点。


I have a primefaces datatable i need to display (selected row number) of (total number of rows) in the JSF page.I could get the row numbers displayed in one of the columns using rowIndexVar attribute but i am not getting any idea to display the same numbers separately in the input text on row select.

What should i need to do in JSF page or managed bean to get selected row number.

Please help me in this regard.

Below is my JSF page

<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>

解决方案

I believe that there's not a straight forward way to do so. Although using two ajax requests is not pretty, you can at least achieve the result you expect.

Every row (tr) rendered by your datatable has a attribute called data-rk with your rowKey and another attribute called data-ri with your rowIndexVar value.

You can get the data-rk attribute through dtWidgetVar.selection (dtWidgetVar is the name of the widgetVar in your datatable).

You can now send the indexRow to your model using a remoteCommand

Here is the code I used to test it:

The View

<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}" />

Managed Bean

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

In case you are using checkbox selection, you can retrieve the selected indexes like this:

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}])
}

You should now be able to make the proper modification to your code to make use of that.

这篇关于获取所选原始数据行的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆