【发布时间】:2015-02-23 10:15:58
【问题描述】:
我正在使用引导表 (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 调用我的 rest 网络服务,它将返回一个 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 中。效果很好..
【问题讨论】:
-
到目前为止,我通过将我的 getEmplooyees 函数放入一个单独的文件 (getEmployee.php) 来解决我的问题,该文件与我之前的函数完全一样,除了不是一个 php 函数。这最终起作用了,但我仍然有点困惑之间的区别是什么,在 data-url 中调用一个函数并放置一个指向相同文件的链接??? ..
标签: php json twitter-bootstrap-3