引导表数据URL [英] Bootstrap Table data-url
问题描述
我正在使用Bootstrap表( http://wenzhixin.net.cn /p/bootstrap-table/docs/examples.html ),而我在使用data-url将数据绑定"到表时遇到了麻烦.
Im using Bootstrap Table (http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html) and Im having trouble "binding" my data to the table using 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数组:
getEmployee calls my rest webservice which will return a json array which looks like this:
[
{"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看起来像这样:
getEmployee looks like this:
// 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']);
}
请注意,我先解码响应,然后再次编码,仅选择雇员嵌套数组.
Please note that Im decoding the response and then encoding it again selecting only the employees nested array.
我尝试回显" json_encode($ decoded ['employees'])并创建数据文件,并将其放入名为data.json的文件中,然后将其插入到我的data-url中.效果很好.
I tried "echoing" json_encode($decoded['employees']) out and create a file of the data and put that into a file called data.json and insert that into my data-url. That worked perfectly..
推荐答案
与表的开发人员联系后,我发现我试图通过将data-url设置为php函数将数据插入表中的尝试是不是使用此属性"的正确方法.
After beeing in touch with the developer of the table, i found that my attempt to insert data into the table by setting the data-url to my php function is not the correct way to use this "property".
我需要将函数放入单独的php文件中,以使其正常工作,正如我在上面的评论中提到的那样.
I need to put my function into a seperate php file in order to get it working, which i did as mentioned in my comment above.
因此解决方案是:
<table data-toggle="table" data-side-pagination="server" data-url="getEmployees.php?id=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.php将向我的Web服务发出请求,并返回结果,然后将其应用于表.
getEmployee.php will be making the request to my webservice and return the result which is then applied to the table.
这篇关于引导表数据URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!