无法使用对象数组显示表,以便我能够使用DT_RowId标记行

时间:2013-05-20 15:15:50

标签: java javascript jquery html

我试图通过传入一个对象数组而不仅仅是一个数组来使用jQuery来显示一个控制表。我想这样做,以便我能够使用DT_RowId标签标记表的行。我需要这样做,以便我可以将删除功能添加到用户可用的表中。以下是我想要做的几个例子:

http://datatables.net/release-datatables/examples/server_side/object_data.html

http://datatables.net/release-datatables/examples/server_side/ids.html

我尝试使用以下代码以这两个示例中显示的方式格式化数据:

searchWsConfigurations.jsp

<link rel="stylesheet" type="text/css" href="<c:url value='/styles/data_table.css'/>" />
<link rel="stylesheet" type="text/css" href="<c:url value='/styles/data_table_proper.css'/>" />
<link rel="stylesheet" type="text/css" href="<c:url value='/styles/demo_table_jui.css'/>" />
<script src="<c:url value='/javascript/jquery.dataTables.min.js' />" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function() {

//Datatable transformations
$('#wsConfigurationTable').dataTable( {
    "bJQueryUI": true,
    "bPaginate": true,
    "bDeferRender": true,
    "bProcessing" : true,
    "bServerSide" : true,
    "sAjaxSource" : "<c:url value='/maintenance/searchWsConfigurationsServerSideProcessingReport.htm'/>",
    "iDeferLoading" : 10,
    "bLengthChange": true,
    "iDisplayLength": 10,
    "bFilter": false,
    "bSort": true,
    "aoColumns": [
                   { "mData": "testerId" },
                   { "mData": "date" },
                   { "mData": "hostName" },
                   { "mData": "ipAddress" },
                   { "mData": "alias" },
                   { "mData": "wsModel" },
                   { "mData": "operatingSystem" },
                   { "mData": "stRevisions" }],

    "bInfo": true,
    "bAutoWidth": true,
    "sPaginationType": "full_numbers"
});

}); 

</script>

<br>
<h2><tiles:getAsString name="heading" ignore="true"/></h2>
<br>

<a href="addWsConfiguration.htm"><img src="/vpdConsole/images/icons/add-icon-button_32x32.png" class="undecorated" style="float:right"/></a>

<table id="wsConfigurationTable" class="datatable">
    <thead>
        <tr>
        <th>Tester ID</th>
        <th>Entry Date</th>
        <th>Hostname</th>
        <th>IP</th>
        <th>Alias</th>
        <th>Model</th>
        <th>OS</th>
        <th>ST Rev</th>
    </tr>
</thead>
<tbody>
</tbody>
</table>

即使服务器正在返回我期望的内容,我仍然会收到有关列数的错误。以下是我的回应:

{"sEcho":1,"iTotalRecords":13105,"iTotalDisplayRecords":13105,"aaData":[{"0":"Tester 1","1":"2013-05-14","2":"Host 1","3":"192.168.1.43","4":"Alias 1","5":"HP Proliant Server","6":"Red Hat Linux 5","7":"2.1.9","DT_RowId":"row_1","DT_RowClass":"visibleClass"},{"0":"Tester 1","1":"2013-05-14","2":"Host 1","3":"192.168.1.96","4":"Alias 1","5":"HP Proliant Server","6":"Red Hat Linux 5","7":"2.1.9","DT_RowId":"row_2","DT_RowClass":"visibleClass"},{"0":"Tester 1","1":"2013-05-15","2":"Host 1","3":"192.168.1.14","4":"Alias 1","5":"HP Proliant Server","6":"Red Hat Linux 5","7":"2.1.9","DT_RowId":"row_3","DT_RowClass":"visibleClass"},{"0":"Tester 10","1":"2013-05-14","2":"Host 10","3":"192.168.1.18","4":"Alias 10","5":"Sun Server X200","6":"Windows 7 Ultimate","7":"1.5.7","DT_RowId":"row_4","DT_RowClass":"visibleClass"},{"0":"Tester 10","1":"2013-05-14","2":"Host 10","3":"192.168.1.34","4":"Alias 10","5":"Sun Server X200","6":"Windows 7 Ultimate","7":"1.5.7","DT_RowId":"row_5","DT_RowClass":"visibleClass"},{"0":"Tester 10","1":"2013-05-15","2":"Host 10","3":"192.168.1.9","4":"Alias 10","5":"Sun Server X200","6":"Windows 7 Ultimate","7":"1.5.7","DT_RowId":"row_6","DT_RowClass":"visibleClass"},{"0":"Tester 100","1":"2013-05-14","2":"Host 100","3":"192.168.1.63","4":"Alias 100","5":"Sun Server X200","6":"Windows 7 Ultimate","7":"1.5.7","DT_RowId":"row_7","DT_RowClass":"visibleClass"},{"0":"Tester 100","1":"2013-05-14","2":"Host 100","3":"192.168.1.9","4":"Alias 100","5":"Sun Server X200","6":"Windows 7 Ultimate","7":"1.5.7","DT_RowId":"row_8","DT_RowClass":"visibleClass"},{"0":"Tester 100","1":"2013-05-15","2":"Host 100","3":"192.168.1.63","4":"Alias 100","5":"Sun Server X200","6":"Windows 7 Ultimate","7":"1.5.7","DT_RowId":"row_9","DT_RowClass":"visibleClass"},{"0":"Tester 1000","1":"2013-05-14","2":"Host 1000","3":"192.168.1.15","4":"Alias 1000","5":"Sun Server X200","6":"Windows 7 Ultimate","7":"1.5.7","DT_RowId":"row_10","DT_RowClass":"visibleClass"}]}

我不确定我需要做些什么来完成这项工作。我可以在使用常规数组时显示表,但是当我将其切换到对象数组时,错误声称数据未定义大小且与已知列数不匹配。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

请参阅此jsfiddle:http://jsfiddle.net/GqFcW/

双击任何行,您将收到包含行ID的警报。我修改了你的代码以使用一个对象数组,对象使用你提供的mData名称而不是“0”,“1”等。这是第一行的例子。

{"testerId":"Tester 1","date":"2013-05-14","hostName":"Host 1","ipAddress":"192.168.1.43","alias":"Alias 1","wsModel":"HP Proliant Server","operatingSystem":"Red Hat Linux 5","stRevisions":"2.1.9","DT_RowId":"row_1","DT_RowClass":"visibleClass"}

或者,您可以保留对象数组与您拥有的对象相同,但将mData修改为

"aoColumns": [
                   { "mData": "0" },
                   { "mData": "1" },

等等。

因此取消注释"bServerSide" : true行,并添加回你的ajax调用。这应该工作。如果没有,您可能需要验证您的JSON是否按照您认为的那样返回。