Bootstrap 表 data-url

2024-01-30

我使用引导表(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 中。那效果很完美..


在与表的开发人员联系后,我发现我尝试通过将 data-url 设置为我的 php 函数来将数据插入表中并不是使用此“属性”的正确方法。

我需要将我的函数放入一个单独的 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 将向我的网络服务发出请求并返回结果,然后将其应用于表。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Bootstrap 表 data-url 的相关文章

随机推荐