简体   繁体   English

显示数据表列的数据

[英]displaying data of datatable column

I am using the jQuery DataTables plugin on a JSF <h:dataTable> . 我在JSF <h:dataTable>上使用jQuery DataTables插件 In this page I have 86 records. 在此页面中,我有86条记录。

+++++++++++++++++++++++++++++++++++++
+ SN. +    Name    +   Email        +
+++++++++++++++++++++++++++++++++++++
+  1  +   Name 1   +  Email 1       +
+  2  +   Name 2   +  Email 2       +
+  3  +   Name 3   +  Email 3       +
+........
+  4  +   Name 4   +  Email 4       +
+++++++++++++++++++++++++++++++++++++
+                 1.2.3.4..... Next +
+++++++++++++++++++++++++++++++++++++

What I want is display the second column data in alert format ie display names that will be there on datable. 我想要的是以警报格式显示第二列数据,即显示将在datable上显示的名称。 I have 5 set of records per table. 每个表有5组记录。 SO when I click 1, I should get alert of first 5 records. 因此,当我单击1时,我应该收到前5条记录的警报。 Once I click 2, I should get names of 6-10 records. 单击2后,我应该得到6-10条记录的名称。

I tried using fnPagingInfo from this link , but this don't give the info that I am looking for (it gives page number, total page numbers, etc) . 我尝试通过此链接使用fnPagingInfo ,但这没有提供我要查找的信息(它提供了页码,总页码等)。

Any idea to get this done? 有什么想法可以完成吗?


I tried with below. 我尝试了以下。

var cells = [];
var rows = $("#userList").dataTable().fnGetNodes();
for(var i=0;i &lt; rows.length;i++)
{
    cells.push($(rows[i]).find("td:eq(0)").html()); 
}
alert(cells);

This gives me alert as Name 1, Name 2, Name 3, ... 这给我警报, Name 1, Name 2, Name 3, ...

This works perfectly, but the problem occurs when I sort the data... :( 这可以正常工作,但是当我对数据进行排序时会出现问题... :(

When I sort the data (sort the serial number as 4,3,2,1), I still get alert as Name 1, Name 2, Name 3, ... . 当我对数据进行排序(将序列号排序为4,3,2,1)时,仍然会收到警报, Name 1, Name 2, Name 3, ...

Alert should say Name 4, Name 3, Name 2, ... . 警报应显示Name 4, Name 3, Name 2, ...

Same is happening with below code also. 下面的代码也发生了同样的情况。

var secondCellArray = [];
$.each(oTable.fnGetData(), function(i, row) {
    secondCellArray.push(row[0]);
});

Well, this is what I did... 好吧,这就是我所做的...

<script type="text/javascript">
    $(document).ready(function(){
        var currData = [];
        var myFinalString = "";

        $('#userList').dataTable( {
            "bPaginate": true,
            "bSort": true,
            "sPaginationType": "full_numbers",
            "bJQueryUI": true,
            "bRetrieve": true,
            "fnPreDrawCallback": function(oSettings) {
                /* reset currData before each draw*/
                currData = [];
                myFinalString= "";
            },
            "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                   /* push this row of data to currData array*/
                   currData.push(aData);
                   myFinalString = myFinalString + aData + "xxxyyyzzz";
            },
            "fnDrawCallback": function () {

                var myTextBox2 = document.getElementById('myHiddenValForId');
                myTextBox2.value = myFinalString;
                myTextBox2.onchange();

                var myTextBox = document.getElementById('myHiddenValForInc');
                myTextBox.value = this.fnPagingInfo().iStart + "," + this.fnPagingInfo().iEnd;
                myTextBox.onchange();

                var cells = [];
                var cells2 = [];
                oTable = $('#userList').dataTable();

                var rows = $("#userList").dataTable().fnGetNodes();

                for(var i=0;i &lt; rows.length;i++) {
                    var data = oTable.fnGetData( i,0 );
                    cells.push($(rows[i]).find("td:eq(0)").html()); 
                    cells2.push(data); 
                }

                var secondCellArray = [];
                $.each(oTable.fnGetData(), function(i, row) {
                    secondCellArray.push(row[0]);
                });
            }
        });
    });
    $.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
    {
        return {
            "iStart":         oSettings._iDisplayStart,
            "iEnd":           oSettings.fnDisplayEnd(),
            "iLength":        oSettings._iDisplayLength,
            "iTotal":         oSettings.fnRecordsTotal(),
            "iFilteredTotal": oSettings.fnRecordsDisplay(),
            "iPage":          oSettings._iDisplayLength === -1 ?
                0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
            "iTotalPages":    oSettings._iDisplayLength === -1 ?
                0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
        };
    };
</script>

Pass those values through f:ajax 通过f:ajax传递这些值

<h:form prependId="false">
    <h:inputText id="myHiddenValForInc" value="#{SearchBean.hiddenVal001}" style="display: none;">
        <f:ajax listener="#{SearchBean.printMyHiddenDetails()}" event="valueChange" />
    </h:inputText>

    <h:inputText id="myHiddenValForId" value="#{SearchBean.hiddenVal002}" style="display: none;" >
        <f:ajax listener="#{SearchBean.printMyHiddenDetails2()}" event="valueChange" />
    </h:inputText>
</h:form>

.java .java

`printMyHiddenDetails()` have all stuff for finding names & `printMyHiddenDetails2()` don't have any code


public void printMyHiddenDetails() {
    // my code
}


public void printMyHiddenDetails2() {
    // noting here... this is just for passing data...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM