如何从 php 获取数据表 jquery 插件的 json 数据
Posted
技术标签:
【中文标题】如何从 php 获取数据表 jquery 插件的 json 数据【英文标题】:How to fetch json data from php for datatables jquery plugin 【发布时间】:2016-02-03 16:52:56 【问题描述】:我对 php 非常陌生,我正在尝试使用 Datatables jQuery 插件。我知道我必须从 .php 文件中获取我的数据,然后在我的数据表中使用它,但我似乎无法通过 ajax 将它传递给数据表。我设法从数据库中获取我的数据,并对它进行 json 编码,但是我不知道如何将它调用到我的 index.php 文件中,我需要在其中显示它。我究竟做错了什么? 这是我的代码:
html(已编辑):
<!-- DataTables CSS -->
< link rel='stylesheet' type='text/css' href='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css'>
< !-- jQuery -->
< script type='text/javascript' charset='utf8' src='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js'></script>
<!-- DataTables -->
< script type='text/javascript' charset='utf8' src='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js'></script>
< script type='text/javascript'>
$( document ).ready(function()
$('#tabela').dataTable(
'bJQueryUI' : true,
'sPaginationType' : 'full_numbers',
'bRetrieve' : true,
'bFilter' : true,
'iDisplayLength': 10,
'bProcessing' : true,
"sAjaxSource": "adminmysqli.php"
);
);
< /script>
来自 fetchdata.php 的数据:
"iTotalRecords":"41","iTotalDisplayRecords":"41","aaData":
[["2","Arya","Stark","612345555","Braavos"],
["3","Jon","Snow","612345655","The"],
["4","Meryn","Trant","612345679","Tirane"],
["5","Sansa","Stone","692345678","Durres"],
["6","Myda","Slate","612345676","Tirane"],
["7","Varys","Bird","612345689","Tirane"],
["9","Stannis","Baratheon","612345678","Tirane"],
["10","Shireen","Burn","612345678","Iron"],
["11","Selyse","Tully","612345678","Tirane"],
["12","Lyanna","Dread","612345678","Tirane"],
["13","Viserys","Targaryen","612345678","Durres"],
["14","Daennerys","Stormborn","612345678","Tirane"],
["15","Khaal","Drogo","612345678","Tirane"],
["16","Jojen","Reed","612345678","Tirane"],
["17","Theon","Greyjoy","612345678","Tirane"],
["18","Osha","Green","612345678","Tirane"],
["19","Oberyn","Martell","612345678","Tirane"],
["20","Ellaria","Sands","612345678","Tirane"],
["22","Tommen","Laster","612345678","Tirane"],
["23","Robert","Baratheon","612345678","Tirane"],
["24","Jamie","Lannister","612345678","Tirane"],
["25","Tywin","Bolt","612345678","Tirane"],
["26","Tyrion","Imp","612345678","Tirane"],
["27","Gregor","Clegane","612345678","Tirane"],
["28","Qyburn","Exper","612345678","Tirane"],
["30","Daniel","Howell","612345678","Tirane"],
["31","Samwell","Tarly","612345678","Tirane"],
["32","Aemon","Maester","61234569","Tirane"],
["33","Jaqen","Hgar","612345678","Tirane"],
["34","Daario","Naharis","612345678","Tirane"],
["35","Jorah","Bear","612345678","Tirane"],
["36","Irri","Mereen","612345678","Tirane"],
["37","Margaery","Tyrell","612345678","Tirane"],
["38","Renly","Baratheon","612345678","Tirane"],
["44","Eddard","Stark","697845123","Winterfell"],
["45","Miranda","Hart","692314256","Durres"],
["46","Stray","Cat","691234567","Here"],
["48","Haley","Dunphy","653746111","Tirana"],
["51","Asli","Felin","666111222","South"],
["55","Alison","Brie","667755333","Saye"],
["58","Marie","Curie","665544333","Radium"]]
编辑: 感谢您的回复,我设法通过删除 mData 属性来获取格式化数据,而且我错误地加载了脚本,阻止我获取数据。
【问题讨论】:
【参考方案1】:将 sAjaxSource 路径更改为 php 文件,然后使用以下代码重试:
<script type='text/javascript'>
$( document ).ready(function()
$('#tabela').dataTable(
'bJQueryUI' : true,
'sPaginationType' : 'full_numbers',
'bRetrieve' : true,
'bFilter' : true,
'iDisplayLength': 10,
'bProcessing' : true,
"sAjaxSource": "fetchdata.php"
);
);
</script>
【讨论】:
以上是关于如何从 php 获取数据表 jquery 插件的 json 数据的主要内容,如果未能解决你的问题,请参考以下文章
jquery select2:从 php-mysql 获取数据时出错
jquery select2:从 php-mysql 获取数据时出错
如何从 Datatables jQuery 插件中提取过滤后的数据?