【问题标题】:jQuery Bootgrid Not Working ProperlyjQuery Bootgrid 无法正常工作
【发布时间】:2015-11-13 06:27:22
【问题描述】:

我需要一些帮助。我在使用 ajax 的页面上有一个 jQuery Bootgrid。当数据正确加载并且网格正确渲染时。所有功能似乎都无法正常工作,包括搜索框、排序、刷新等。同样,我没有收到任何 javascript 错误。

我引用以下内容

  • jquery.bootgrid.min.css
  • jquery.bootgrid.min.js
  • jquery.bootgrid.fa.min.js

我的代码非常基础

HTML

<table id="jobGrid" class="table table-condensed table-hover table-striped">
    <thead>
        <tr>
            <th data-column-id="JobNumber" data-identifier="true" data-type="string">Job Number</th>
            <th data-column-id="JobName" data-type="string">Job Name</th>
            <th data-column-id="JobState" data-type="string">Request State</th>
            <th data-column-id="JobStatus" data-type="string">Status</th>
            <th data-column-id="JobRequestor" data-type="string">Requestor</th>
            <th data-column-id="LastModifiedDate" data-type="date" data-order="desc">Last Modified</th>
            <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
        </tr>
    </thead>
</table> 

JavaScript

// Planning Filter
var planningFilter = function () {
    // NOTE: I have multiple types of basic filters. 
    // eg: Planning, Approved, Completed
    $("#jobGrid").bootgrid("destroy");
    var grid = $("#jobGrid").bootgrid({
        ajax: true,
        ajaxSettings: {
            method: "GET",
            cache: false
        },
        url: RestService.GetJobsInPlanningSvr(),
        formatters: {
            "commands": function (column, row) {
                return "<button type=\"button\" class=\"btn btn-xs btn-default command-edit\" data-row-id=\"" + row.JobNumber + "\"><span class=\"fa fa-pencil\"></span>&nbsp;&nbsp;View Details</button>";
            }
        }
    }).on("loaded.rs.jquery.bootgrid", function () {
        /* Executes after data is loaded and rendered */
        grid.find(".command-edit").on("click", function (e) {
            alert("You pressed edit on row: " + $(this).data("row-id"));
        });
    });
}

JSON 结果

{
    "current":1,
    "rowCount":10,
    "rows":[
        {"CustomerID":"88888888-8888-8888-8888-888888888888","JobNumber":"DMPC-2","JobName":"DMPC-2: Transfer 645 Units to Warehouse","JobState":"Request Draft","JobStatus":"In Planning","JobRequestor":"Jim Halpert","LastModifiedUTS":1439768413,"LastModifiedDate":"8/16/2015"},
        {"CustomerID":"88888888-8888-8888-8888-888888888888","JobNumber":"DMPC-1","JobName":"DMPC-1: Scranton Chamber of Commerce Delivery","JobState":"Request Draft","JobStatus":"Pending Approval","JobRequestor":"Dwight Schrute","LastModifiedUTS":1440009361,"LastModifiedDate":"8/19/2015"}
    ],
    "total":2
}

基本上就是这样...您可以看到任何功能都不起作用的任何原因...不是:我没有测试分页但是如果这也不起作用我也不会感到惊讶。

感谢您的帮助

【问题讨论】:

    标签: jquery jquery-bootgrid


    【解决方案1】:

    当您将 ajax 与 bootgrid 一起使用时,搜索和分页等功能预计将在服务器端完成。因此,您应该从表的默认发布结果中获取搜索输入。

    current=1&rowCount=10&sort[sender]=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed

    【讨论】:

      【解决方案2】:

      我认为您的服务器端没有正确响应来自网格的请求。在服务器端放一个断点,我想你会发现它被调用了......

      需要从request.args中提取“current”(currentPage)和“rowCount”,然后返回正确的json...

      【讨论】:

      • 这更像是一个评论而不是一个明确的答案,请确保您在发布之前查看How to Answer
      【解决方案3】:

      问题是,该函数是在页面元素完全加载之前执行的。

      你可以把函数放在$(function() { });里面

      例如

      $(function() {  
        // Planning Filter
        var planningFilter = function () {
            // NOTE: I have multiple types of basic filters. 
            // eg: Planning, Approved, Completed
            $("#jobGrid").bootgrid("destroy");
            var grid = $("#jobGrid").bootgrid({
                ajax: true,
                ajaxSettings: {
                    method: "GET",
                    cache: false
                },
                url: "data.json",
                formatters: {
                    "commands": function (column, row) {
                        return "<button type=\"button\" class=\"btn btn-xs btn-default command-edit\" data-row-id=\"" + row.JobNumber + "\"><span class=\"fa fa-pencil\"></span>&nbsp;&nbsp;View Details</button>";
                    }
                }
            }).on("loaded.rs.jquery.bootgrid", function () {
                /* Executes after data is loaded and rendered */
                grid.find(".command-edit").on("click", function (e) {
                    alert("You pressed edit on row: " + $(this).data("row-id"));
                });
            });
        }
      
        planningFilter();
      });
      

      Preview

      【讨论】:

      • 我其实是在调用planningFilter(); $(document).ready() 方法中的方法。话虽如此-我确实尝试了您的方法,但仍然没有任何功能起作用。它必须在我的代码中,因为然后我注释掉了我的所有代码并尝试了演示脚本并且它起作用了......示例代码和我的代码之间的最大区别是数据被硬编码到 HTML 表中当我从 ajax 调用中拉出来时。我正在使用根据 bootgrid api 的 get 方法。不幸的是没有例子。我是否正确使用它? (参考:ajaxSettings)在我的代码中。
      • @Sean 啊,我明白了,是的,可能是数据。我们的代码之间的区别在于我们获取数据的位置,我使用data.json 而你使用RestService.GetJobsInPlanningSvr()。我建议调试的是您使用示例json 文件并将数据放在那里,而不是使用RestService.GetJobsInPlanningSvr() 使用name_of_file.json
      • 我想我知道问题出在哪里了......基本上,当使用 ajax 时,网格不会像使用静态 json 那样在分页或排序时引用现有的 json 有效负载。它实际上对我应该在那里执行逻辑的服务器进行了新的 ajax 调用。我试图关闭此功能,但似乎没有采取。我不想为分页和排序打开数据库线程,所以我将尝试另一种数据网格解决方案。感谢您的帮助@Robin Carlo Catacutan - 非常感谢...
      • @Sean 太好了!我很乐意提供帮助。
      猜你喜欢
      • 2015-11-13
      • 2011-07-24
      • 1970-01-01
      • 2021-06-16
      • 2015-12-14
      • 2012-07-03
      • 2012-09-12
      • 2016-05-25
      相关资源
      最近更新 更多