繁体   English   中英

无法使用对象数组显示表,因此我可以使用DT_RowId标记行

[英]Unable to display table using an array of objects so that I am able to label rows using DT_RowId

我试图通过传递对象而不是数组来显示使用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"}]}

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

请参阅以下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是否按预期返回。

暂无
暂无

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

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