引导表(wenzhixin)->资料来自Ajax [英] bootstrap-table (wenzhixin) --> Data by Ajax

查看:44
本文介绍了引导表(wenzhixin)->资料来自Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用wenzhixin( http://bootstrap-table. wenzhixin.net.cn/),但是我的技能似乎还不足以使脚本能够运行.

i want to use the bootstrap-table library from wenzhixin (http://bootstrap-table.wenzhixin.net.cn/) but my skills seem not be as good enough that i can get the script running.

我希望通过ajax为表提供数据.

I want the table to be supplied with data via ajax.

这是有效的代码(来自源页面的示例):

Here's the code which works (example from the source page):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>SL Time</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Table -->
    <link href="css/bootstrap-table.css" rel="stylesheet">

    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/jquery-3.1.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Bootstrap Table -->
    <script src="js/bootstrap-table.js"></script>
    <script src="js/bootstrap-table-de-DE.js"></script>
</head>
<body>
<div class="container">
    <table id="table"
           data-toggle="table"
           data-height="460"
           data-search="true"
           data-ajax="ajaxRequest"
           data-side-pagination="server"
           data-pagination="true">
        <thead>
        <tr>
            <th data-field="nummer">Nummer</th>
            <th data-field="name">Name</th>
        </tr>
        </thead>
    </table>
</div>
<script>
// your custom ajax request here
function ajaxRequest(params) {
    // data you need
    console.log(params.data);
    // just use setTimeout
    setTimeout(function () {
        params.success({
            total: 100,
            rows: [{
                "nummer": 0,
                "name": "Item 0",
            }]
        });
    }, 1000);
}

但是我希望数据来自我的页面"ajax_loader.php",它看起来像这样:

But i want the data coming from my page "ajax_loader.php" which looks like this:

<?php
$data=array();
array_push($data, array('nummer' => '1', 'name' => 'daniel'));
array_push($data, array('nummer' => '2', 'name' => 'thomas'));
echo json_encode($data);
?>

但是我如何获得下面的代码来填充表格(如示例函数那样):

But how do i get the following piece of code get to fill the table (as the sample function does):

$.ajax({
        type: "POST",
        url: "ajax_loader.php",
        data: "user-id=1",
        success: function(data) {
            // At this position my knowledge ends ;-(
        }
    });

有人可以帮助我,让事情正常吗?

Can anyone help me, get the thing working?

最诚挚的问候

丹尼尔

推荐答案

文档是您的朋友^^(此外,还有一个示例:

The documentation is your friend ^^ (moreover with an example : http://issues.wenzhixin.net.cn/bootstrap-table/index.html#options/custom-ajax.html).
Seriously, here is the solution :

HTML:

<div class="container">
    <table id="table"
           data-toggle="table"
           data-height="460"
           data-search="true"
           data-ajax="ajaxRequest"
           data-side-pagination="server"
           data-pagination="true">
        <thead>
        <tr>
            <th data-field="nummer">Nummer</th>
            <th data-field="name">Name</th>
        </tr>
        </thead>
    </table>
</div>

脚本:

// your custom ajax request here
function ajaxRequest(params) {

    // data you may need
    console.log(params.data);

    $.ajax({
        type: "POST",
        url: "ajax_loader.php",
        data: "user-id=1",
// You are expected to receive the generated JSON (json_encode($data))
        dataType: "json",
        success: function (data) {
            params.success({
// By default, Bootstrap table wants a "rows" property with the data
                "rows": data,
// You must provide the total item ; here let's say it is for array length
                "total": data.length
            })
        },
        error: function (er) {
            params.error(er);
        }
    });
}

这篇关于引导表(wenzhixin)->资料来自Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆