如何将 jQuery SlickGrid 与 PHP / MySQL 一起使用(加载服务器数据并保存更改)

发布于 2024-09-24 18:13:31 字数 491 浏览 3 评论 0原文

请在 SlickGrid 代码中找到的所有示例中,数据数组是随机生成的客户端。

获取:我需要知道如何使用 PHP 从 MySQL 数据库获取此信息,并使用 jQuery / AJAX 将其返回到 SlickGrid。

保存:我已经在 StackOverflow 上找到了一个链接,用于使用隐藏输入保存网格中的数据 (在 SlickGrid 中保存更改),但目前还不清楚我应该如何在访问 PHP 脚本时处理这些数据。

一些详细的帮助和/或指示将不胜感激,我是一个菜鸟,我没有找到关于这个很棒的插件的足够文档。

Please, in all the examples found in the SlickGrid code, the data array was randomly generated on the client side.

Getting: I need to know how to use PHP to fetch this information from a MySQL Database and return it using jQuery / AJAX to the SlickGrid.

Saving: I already found a link on StackOverflow for saving data from the grid using a hidden input (Saving changes in SlickGrid) but it's not really clear how I ought to handle this data on getting to the PHP script.

Some detailed help and/or pointers will be appreciated, I'm rather a noob and I did not find adequate documentation on this awesome plugin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2024-10-01 18:13:31

SlickGrid 需要一组数据才能填充表。您可以在 PHP 中将其创建为字符串,并在创建 SlickGrid 时在 JavaScript 中使用它。

请注意;这是快速、肮脏且未经测试的!

PHP

$data = '';
$i = 0;

$query = "
    SELECT
        `title`, `duration`, `percentComplete`, `start`, `finish`, `effortDriven`
    FROM
        `myTable`
";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $data .= '
        data['.$i.'] = {
            title: "'.$row['title'].'",
            duration: "'.$row['duration'].'",
            percentComplete: "'.$row['percentComplete'].'",
            start: "'.$row['start'].'",
            finish: "'.$row['finish'].'",
            effortDriven: "'.$row['percentComplete'].'"
        };
    ';

    $i++;
}

JavaScript

<script type="text/javascript">
    var grid;

    var columns = [
        {id:"title", name:"Title", field:"title"},
        {id:"duration", name:"Duration", field:"duration"},
        {id:"%", name:"% Complete", field:"percentComplete"},
        {id:"start", name:"Start", field:"start"},
        {id:"finish", name:"Finish", field:"finish"},
        {id:"effort-driven", name:"Effort Driven", field:"effortDriven"}
    ];

    var options = {
        enableCellNavigation: false,
        enableColumnReorder: false
    };

    $(function() {
        var data = [];
        <?php echo $data; ?> //This is where we echo the PHP variable $data which contains our JavaScript array as a string.

        grid = new Slick.Grid($("#myGrid"), data, columns, options);
    })
</script>

SlickGrid needs an array of data in order to populate the table. You can create this as a string in PHP and use that in your JavaScript when you create your SlickGrid.

Please note; this is quick, dirty and untested!

PHP

$data = '';
$i = 0;

$query = "
    SELECT
        `title`, `duration`, `percentComplete`, `start`, `finish`, `effortDriven`
    FROM
        `myTable`
";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $data .= '
        data['.$i.'] = {
            title: "'.$row['title'].'",
            duration: "'.$row['duration'].'",
            percentComplete: "'.$row['percentComplete'].'",
            start: "'.$row['start'].'",
            finish: "'.$row['finish'].'",
            effortDriven: "'.$row['percentComplete'].'"
        };
    ';

    $i++;
}

JavaScript

<script type="text/javascript">
    var grid;

    var columns = [
        {id:"title", name:"Title", field:"title"},
        {id:"duration", name:"Duration", field:"duration"},
        {id:"%", name:"% Complete", field:"percentComplete"},
        {id:"start", name:"Start", field:"start"},
        {id:"finish", name:"Finish", field:"finish"},
        {id:"effort-driven", name:"Effort Driven", field:"effortDriven"}
    ];

    var options = {
        enableCellNavigation: false,
        enableColumnReorder: false
    };

    $(function() {
        var data = [];
        <?php echo $data; ?> //This is where we echo the PHP variable $data which contains our JavaScript array as a string.

        grid = new Slick.Grid($("#myGrid"), data, columns, options);
    })
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文