【问题标题】:How to add anchor tag to column for each row Bootstrap-Datatable如何为每一行 Bootstrap-Datatable 的列添加锚标记
【发布时间】:2014-05-23 09:43:08
【问题描述】:

我有一个页面,其中数据被某些搜索事件刷新。

数据由Bootstrap-Datatable wia ajax 渲染,返回json response。

这里是渲染表的小代码:

function renderTable(url, table, query) {
    $.ajax({url: url,
        data: query,
        success: function(data) {
        $(table).dataTable({
            aaData: data.aaData,
            aoColumns: data.aoColumns,
            bProcessing: true,
            iDisplayLength: 50,
            bDestroy: true
        });
        }
       });
}

我希望所有Name 列都应该是一个锚标记,其中包含指向带有名称参数和值的某个 url(显示配置文件)的链接。喜欢-

http://url.com/profile?name=Airi%20satau

【问题讨论】:

    标签: javascript jquery twitter-bootstrap datatable


    【解决方案1】:

    我遇到了同样的情况,我必须在 Datatable Columns 中显示锚标记来代替原始文本。下面给出了对我有用的解决方案

    $(document).ready(function () {
            $('#example').DataTable({
                "ajax": yourDataURL,
                "columns": [ // Defines column for the output table
                        { "data": "InventoryId" }, // Attribute of item in collection
                        { "data": "InventoryName" },
                        { "data": "InventoryManager" },
                        { "data": "InventoryActive1",
                            "orderable": false,
                            "searchable": false,
                            "render": function(data,type,row,meta) { // render event defines the markup of the cell text 
                                var a = '<a><i class="fa fa-edit"></i>  ' + row.InventoryName +'</a>'; // row object contains the row data
                                return a;
                            }
                        }
                ]
            });
        });
    

    希望这对陷入相同情况的人有所帮助

    【讨论】:

    • 像魅力一样工作...感谢 Mayank 的精彩提示 :)
    【解决方案2】:

    这是一个老问题,但我在 Google 上偶然发现了它,所以我想我会发布一个答案,以防其他人在这里发生。

    根据您的路由,您可以让 jQuery 添加一个按名称修改的单击事件。假设“name” td 标签被赋予了 .name 类:

      $(".name").click(function() {
            var suffix = $(this).text().split(' ').join('%20'); //replace the space in the name with %20
            window.document.location = "http://url.com/profile?name=" + $(this).text(); //append to the url string and change document location
      });
    

    这样做的诀窍是确保它适用于您的路由,根据示例,它应该。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2020-04-05
      • 2020-01-15
      • 2023-03-10
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多