javascript - 如何使用 ajax 在 jquery 数据表中显示附加标签?

标签 javascript jquery ajax datatables

这是我的js代码:

$(document).ready(function () {
        var table = $('#users-data-table').DataTable({
            stateSave : true,
            processing : true,
            serverSide: true,
            ajax: {
                url: '@(Url.Action("GetFilterUsers", "Users", new { area = "Admin" }))',
                type: "GET"
            },
              columns: [
                { "data": "Name", "orderable" : false },
                { "data": "DateCreated", "orderable": false },
                { "data": "TotalBroadcasts", "orderable": false },
                { "data": "TotalViews", "orderable": false },
                { "data": "City", "orderable": false }
                { "data": "Country", "orderable": false }
            ]
        });
    });

HTML:

<div class="dataTable_wrapper">
                <table class="table table-striped table-bordered table-hover" id="users-data-table">
                    <thead>
                    <th>Name</th>
                    <th>DateCreated</th>
                    <th>Total broadcasts</th>
                    <th>Viewers</th>
                    <th>Location</th>
                    </thead>

                </table>
            </div>

我想在一列中显示“城市”和“国家/地区”,并且我想在每一行中显示附加标签(例如:<i class="fa fa-flag"></i>)。我该怎么做?

最佳答案

您可以使用 columns.render为列定义自定义渲染器的选项。

$(document).ready(function () {
    var table = $('#users-data-table').DataTable({
        stateSave : true,
        processing : true,
        serverSide: true,
        ajax: {
            url: '@(Url.Action("GetFilterUsers", "Users", new { area = "Admin" }))',
            type: "GET"
        },
          columns: [
            { "data": "Name", "orderable" : false },
            { "data": "DateCreated", "orderable": false },
            { "data": "TotalBroadcasts", "orderable": false },
            { "data": "TotalViews", "orderable": false },
            { "data": "City", "orderable": false },
            { 
               "data": null,
               "orderable": false,
               "render": function(data, type, full, meta){
                  return '<i class="fa fa-flag"></i>' + full["City"] + ', ' + full["Country"];
               }
            }
        ]
    });
});

关于javascript - 如何使用 ajax 在 jquery 数据表中显示附加标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187382/

相关文章:

javascript - 如何从 MongoDB 数组中删除重复的对象?

javascript - 我该怎么做才能针对 IE7/IE8 优化我的 ajax 应用程序以避免出现 "Stop running this script"?

jquery - 获取 div 子元素的宽度以创建水平 slider

javascript - 从 <ul>、<li> 树格式获取值

javascript - jQuery AJAX : prevent the delay caused due to set interval function

jquery - rails : s3_direct_upload - How do I save/use the data response?

javascript - 如何在更改事件上添加 fadeIn Jquery

javascript - 有什么方法可以访问 Next.js 中的 `__filename` 吗?

javascript - WebWorker 中运行的代码是否在不同的领域中运行?

javascript - jquery动态过滤列表