Bootstrap表data-url

时间:2014-12-25 23:01:05

标签: php json twitter-bootstrap-3

我使用Bootstrap Table(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调用我的其余webservice,它将返回一个如下所示的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($ decoding ['employees'])并创建数据文件并将其放入名为data.json的文件中并将其插入到我的data-url中。这很完美..

1 个答案:

答案 0 :(得分:0)

在与桌面的开发人员联系之后,我发现通过将data-url设置为我的php函数来尝试将数据插入到表中是不正确的方法来使用这个&#34;属性&#34 ;

我需要将我的函数放入一个单独的php文件中才能使其正常工作,我在上面的评论中提到过。

所以解决方案是:

<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将向我的webservice发出请求并返回结果,然后将结果应用于表。