displaying data of datatable column

2019-09-19 08:19发布

问题:

I am using the jQuery DataTables plugin on a JSF <h:dataTable>. In this page I have 86 records.

+++++++++++++++++++++++++++++++++++++
+ 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 i.e. display names that will be there on datable. I have 5 set of records per table. SO when I click 1, I should get alert of first 5 records. Once I click 2, I should get names of 6-10 records.

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

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

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

Alert should say 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]);
});

回答1:

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

<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

`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...
}