【问题标题】:How to do offline sorting in bootgrid?如何在 bootgrid 中进行离线排序?
【发布时间】:2018-07-03 03:42:13
【问题描述】:

假设我从 api 获取数据并且在排序过程中,不想点击 api,如果在 bootgrid 中可以提供帮助,例如数据表。

我有这个用于 bootgrid 加载的功能,请帮助解决这个问题。

function generateBootGrid(pd) {
$(pd.gridId).bootgrid({
    ajax: true,
    rowSelect: true,
    navigation: 0,
    sort: true,
    search: false,
    post: function ()
    {
        /* To accumulate custom parameter with the request object */
        return getCustomPramas();
    },
    url: baseUrl + pd.fireUrl+'?get_of_year='+$('#get_of_year').val(),
    templates: {
        search: ""
    },
    responseHandler: function (data) {
        console.log(data);
        $(pd.totalPriceId).html(data.totalCost);
        return data;
    },
    formatters: {
        "commands": function (column, row)
        {
            return "<button type=\"button\" class=\"btn btn-xs btn-default " + pd.editButtonClass + "\" data-row-id=\"" + row.id + "\"><span class=\"glyphicon glyphicon-edit\"></span></button> ";
        }
    }
})

requiredBootGridParms = {
    gridId: "#educational-expenses",
    fireUrl: "/pedagogical-action/get-educational-expenses/",
    totalPriceId: "#totalEduCost",
    editButtonClass: "command-edit",
};

generateBootGrid(requiredBootGridParms);

这是这个网格的 html

 <table id="educational-expenses" class="table table-condensed table-hover table-striped" width="60%" cellspacing="0" >
        <thead>
            <tr>
                <th data-column-id="account_number" data-type="numeric" data-identifier="true">Account Number</th>
                <th data-column-id="account_name"  data-order="desc">Account Name</th>
                <th data-column-id="amount">Amount</th>
            </tr>
        </thead>
    </table>
    <button type="button" class="btn btn-xs btn-primary" id="command-add" data-row-id="0">
        <span class="glyphicon glyphicon-plus"></span></button>
    <span class="text-danger float-right">Total educational costs - A:  <span class="text-danger" id="totalEduCost">00.00</span></span> 

谢谢

【问题讨论】:

    标签: jquery jquery-bootgrid


    【解决方案1】:

    这是可能的。你需要自己做一个 ajax 请求,然后手动填充 bootgrid。您还需要将 bootgrid 配置为不使用 ajax。

    function generateBootGrid(pd) {
    
        var grid = $(pd.gridId).bootgrid({
            ajax: false,
            rowSelect: true,
            navigation: 0,
            sort: true,
            search: false,
            templates: {
                search: ""
            },
            formatters: {
                "commands": function (column, row)
                {
                    return "<button type=\"button\" class=\"btn btn-xs btn-default " + pd.editButtonClass + "\" data-row-id=\"" + row.id + "\"><span class=\"glyphicon glyphicon-edit\"></span></button> ";
                }
            }
        });
    
        return grid;
    
    }
    
    function loadData(pd, grid) {
        var url = baseUrl + pd.fireUrl+'?get_of_year='+$('#get_of_year').val();
        var data = getCustomPramas();
        $.post(url, data)
        .done(function (data) {
            console.log(data);
            data = JSON.parse(data);
            $(pd.totalPriceId).html(data.totalCost);
    
            grid.bootgrid('clear');
            grid.bootgrid('append', data.rows);
        });
    }
    
    requiredBootGridParms = {
        gridId: "#educational-expenses",
        fireUrl: "/pedagogical-action/get-educational-expenses/",
        totalPriceId: "#totalEduCost",
        editButtonClass: "command-edit",
    };
    
    var grid = generateBootGrid(requiredBootGridParms);
    loadData(requiredBootGridParms, grid);
    

    API 方法应返回如下内容:

    public IHttpActionResult Get()
    {
        var model = new ExpensesViewModel();
        model.totalCost = 1000.53;
        model.rows = new List<ItemViewModel>();
        model.rows.Add(new User("John"));
        model.rows.Add(new User("Darin"));
        model.rows.Add(new User("BalusC"));
        return Ok(model);
    }
    

    我以 C# Web.API 为例,但您可以随意使用任何后端。

    ...您的 API 返回的 data 应该是这样的 json:

    {
        totalCost: 1000.53,
        rows: [
            { id: 1, cost: 50 },
            { id: 2, cost: 130 },
            { id: 13 cost: 25 }
        ]
    }
    

    通过这种方法,bootgrid 将在客户端进行排序、分页和搜索,而无需自动向 API 发送请求。

    但是,请注意,这种方法只有在 API 返回网格中您需要的所有数据时才有效。因此,如果您有 100 行,您的 API 应该返回这 100 行一次(同样适用于 DataTable)。

    【讨论】:

    • @UmashankarSaw 我编辑了我的答案。让我知道它是否对您有帮助。
    • @Alisson 如何在loadData函数中获取grid变量?
    • @UmashankarSaw 查看我编辑的代码。现在,generateBootGrid 返回网格。在我们调用generateBootGrid() 的地方,我们得到函数返回的网格,并将其作为参数传递给loadData 函数。这是解决问题的一种方法。
    • @UmashankarSaw 这是console.log(data);的输出???请编辑您的答案并分享您的表格的 HTML。
    • 我已经编辑了这个问题。是的,这个 json 在 console.log(data) { "current": 1, "rowCount": 1, "total": 1, "rows": [{ "account_number": "234231", "account_name": " testa", "amount": "1520" }, { "account_number": "234232", "account_name": "dsfads", "amount": "234" }], "totalCost": "1520.00" }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多