我使用引导表(http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html)并且我无法使用 data-url 将我的数据“绑定”到表。
这是我的表代码:
<table data-toggle="table" data-side-pagination="server" data-url="<? echo $letServiceHandler->getEmployees($_SESSION['api_key'],2); ?>" data-cache="false" data-height="299">
<thead>
<tr>
<th data-field="id">Item ID</th>
<th data-field="name">Item Name</th>
<th data-field="price">Item Price</th>
</tr>
</thead>
</table>
getEmployee 调用我的其余 Web 服务,它将返回一个 json 数组,如下所示:
[
{"id":"33","name":"5yhjtyjyas 444","active":"0","user_id":"1","no_of_reports":"0"},
{"id":"29","name":"AAA","active":"1","user_id":"1","no_of_reports":"0"},
{"id":"20","name":"aasdasd","active":"1","user_id":"1","no_of_reports":"0"}
]
getEmployee 看起来像这样:
// Gets employees
public function getEmployees($api_key,$active)
{
$headers = array('Content-type: application/json','Authorization: '.$api_key,);
if($active==0)
{
$curl = curl_init(GET_ALL_INACTIVE_EMPLOYEES);
}
else if($active==1)
{
$curl = curl_init(GET_ALL_ACTIVE_EMPLOYEES);
}
else
{
$curl = curl_init(GET_ALL_EMPLOYEES);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_USERPWD, $api_key);
$curl_response = curl_exec($curl);
if ($curl_response === false)
{
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response,true);
return json_encode($decoded['employees']);
}
请注意,我对响应进行解码,然后再次编码,仅选择员工嵌套数组。
我尝试“回显” json_encode($decoded['employees']) 并创建一个数据文件,然后将其放入名为 data.json 的文件中,然后将其插入到我的 data-url 中。那效果很完美..