由于某些功能,如 Excel 和 PDF 下載,我想在我的網站上實作一些腳本。我在 bootstrap-table.com 上找到了一個解決方案,我無法檢索表資料,因為它們是腳本中的 json 源(data-url)。我的表資料在 SQL 資料庫中可用。我試圖創建一個查詢并以 json 格式創建一個輸出,但我失敗了。我在這一點上尋求幫助。
這是我發現的基本代碼:
<link href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/tableExport.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/libs/jsPDF/jspdf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/libs/jsPDF-AutoTable/jspdf.plugin.autotable.js"></script>
<script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/extensions/export/bootstrap-table-export.min.js"></script>
<style>
#toolbar {
margin: 0;
}
</style>
<div id="toolbar" class="select">
<select class="form-control">
<option value="">Export Basic</option>
<option value="all">Export All</option>
<option value="selected">Export Selected</option>
</select>
</div>
<table id="table"
data-show-export="true"
data-pagination="true"
data-side-pagination="server"
data-click-to-select="true"
data-toolbar="#toolbar"
data-show-toggle="true"
data-show-columns="true"
data-url="https://examples.wenzhixin.net.cn/examples/bootstrap_table/data">
</table>
<script>
var $table = $('#table')
$(function() {
$('#toolbar').find('select').change(function () {
$table.bootstrapTable('destroy').bootstrapTable({
exportDataType: $(this).val(),
exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel', 'pdf'],
columns: [
{
field: 'state',
checkbox: true,
visible: $(this).val() === 'selected'
},
{
field: 'id',
title: 'ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}
]
})
}).trigger('change')
})
</script>
我的問題是我的資料在我的 SQL 資料庫中可用。所以我檢索我的資料,如:
$statement = $pdo->prepare("SELECT id, vorname, rolle FROM users WHERE cid = :cid");
$result = $statement->execute(array(':cid' => $user['cid']));
$users = $statement->fetch();
我想將 data-url 從這里更改data-url="https://examples.wenzhixin.net.cn/examples/bootstrap_table/data"為 thisdata-url="<?=json_encode($users)?>會起作用,但我的表中沒有顯示任何資料。
有什么想法可以通過使用我的 PDO 查詢使其正常作業嗎?
uj5u.com熱心網友回復:
我相信這會解決您的問題,首先將頁面的標題設定為 json,這應該出現在頁面上的任何輸出之前:
header('Content-type: application/json; charset=utf-8');
然后像往常一樣獲取用戶。
$statement = $pdo->prepare("SELECT id, vorname, rolle FROM users WHERE cid = :cid");
$result = $statement->execute(array(':cid' => $user['cid']));
$users = $statement->fetch();
并最終回應該結果:
echo $users
總而言之,您的腳本應如下所示:
<?php
header('Content-type: application/json; charset=utf-8');
$statement = $pdo->prepare("SELECT id, vorname, rolle FROM users WHERE cid = :cid");
$result = $statement->execute(array(':cid' => $user['cid']));
$users = $statement->fetch();
echo json_encode($users);
?>
最終編輯
使用上面代碼中的內容創建一個新的 PHP 檔案,我不知道您的 URL 的結構如何,但是您應該能夠通過 url 訪問此檔案,例如http://yoursite.com/users/jsondata,如果您需要傳遞引數來更改結果,您可以例如使用查詢引數http://yoursite.com/users/jsondata?minprice=2&maxprice=10,并相應地更改您的資料。當您訪問此 url 時,您應該能夠看到您的資料結構與您在此處的結構相似:https : //examples.wenzhixin.net.cn/examples/bootstrap_table/data
然后在你的表中你可以有:
<table id="table"
...
data-url="http://yoursite.com/users/jsondata<?=($_GET['minprice'] ? '?minprice=' . $_GET['minprice'] : '').($_GET['maxprice'] ? '&maxprice=' . $_GET['maxprice'] : '')?>">
</table>
實際上,您可以做更多的事情來使您的代碼更加優雅,但這應該會給您一個良好的開端。
編輯
查看您提供的示例鏈接后,我不得不想出這個。
$users = $statement->fetch();
$data = [
'total' => count($users),
'totalNotFiltered' => count($users),
'rows' => $users,
];
echo json_encode($data, JSON_FORCE_OBJECT);
我相信這應該有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/385803.html
上一篇:我如何將資料放入Json檔案中以獲得建構式屬性?我收到了Name,Instructor,Credit無法訪問的錯誤
下一篇:如何處理多個單獨的json回應?
