繁体   English   中英

如何将 JavaScript 数据传递给 jQuery DataTables

[英]How to pass JavaScript data to jQuery DataTables

尝试在表中插入和显示 JSON 时,我感到非常沮丧。 我正在使用 jQuery DataTable 来做到这一点。

我有以下 jQuery 和 HTML 代码,但没有成功:

<table id="sectorsTable">
    <thead>
        <tr>
            <th><b>Numero</b></th>
            <th><b>Nombre</b></th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<script>
var jsonArray = { layersSelected: temporaryArraySectors };
var jsonString = JSON.stringify(jsonArray, null, 2);

$('#sectorsTable').dataTable({ 
    'ajax': jsonString
});
</script>

顺便说一下,vars的内容是:

temporaryArraySectors = [ [19,"Cordillera"], [10,"Guaiquillo"], [34,"Zapallar"], [27,"Rural Poniente"], [1,"Aguas Negras"], [24,"La Obra"], [28,"Rural Sur"] ];
jsonString = '{"layersSelected": [ [19,"Cordillera"], [10,"Guaiquillo"], [34,"Zapallar"], [27,"Rural Poniente"], [1,"Aguas Negras"], [24,"La Obra"], [28,"Rural Sur"] ] }';

怎么了?

你并不需要创建JSON字符串JSON.stringify ,只需设置数据选项等于temporaryArraySectors

有关代码和演示,请参见下面的示例。

 $(document).ready(function() { var temporaryArraySectors = [ [19,"Cordillera"], [10,"Guaiquillo"], [34,"Zapallar"], [27,"Rural Poniente"], [1,"Aguas Negras"], [24,"La Obra"], [28,"Rural Sur"] ]; var table = $('#sectorsTable').DataTable({ data: temporaryArraySectors }); });
 <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script> <script src="http://vitalets.github.com/x-editable/assets/mockjax/jquery.mockjax.js"></script> </head> <body> <table id="sectorsTable" class="display"> <thead> <tr> <th><b>Numero</b></th> <th><b>Nombre</b></th> </tr> </thead> <tbody> </tbody> </table> </body> </html>

暂无
暂无

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

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