显示数据表列的数据

时间:2013-06-03 13:09:50

标签: javascript jquery html jsf-2 datatables

我在JSF <h:dataTable>上使用jQuery DataTables plugin。在这个页面中我有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 +
+++++++++++++++++++++++++++++++++++++

我想要的是以警报格式显示第二列数据,即显示在datable上的名称。我每桌有5组记录。所以当我点击1时,我应该得到前5条记录的警报。点击2后,我应该得到6-10条记录的名称。

我尝试使用this link中的fnPagingInfo,但这并未提供我要查找的信息(它提供了页码,总页数等)。

有任何想法要完成这项工作吗?


我在下面尝试过。

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

这为我提供了警告Name 1, Name 2, Name 3, ...

这很有效,但是当我对数据进行排序时会出现问题... :(

当我对数据进行排序(将序列号排序为4,3,2,1)时,我仍然会收到警告Name 1, Name 2, Name 3, ...

警报应该说Name 4, Name 3, Name 2, ...

同样的情况也发生在以下代码中。

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

1 个答案:

答案 0 :(得分:0)

嗯,这就是我做的......

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

将这些值传递到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...
}