如何使用数据表服务器处理格式化/自定义表列中的数据?

use*_*816 5 html jquery datatables

问题非常简单.使用datatables服务器处理的基本初始化时,页面上仅显示纯文本.相反,它完全将数据库列中的内容拉入表列,而不需要额外的HTML格式.

示例:这是一个img的文本HTMLCSS格式文本 - http://i.imgur.com/li2UMI7.png.表格的每一列都有自己的样式/格式.现在,当datatables服务器处理向服务器/数据库发出请求时,结果就像我说的那样,就像它们在数据库中一样.因此,为了获得如上所示的这种格式,我必须将HTML放在数据库中.IE:

<span class="label label-danger">Tag</span>
Run Code Online (Sandbox Code Playgroud)

要么

<span class="label bg-color-greenDark">Category Label</span>
Run Code Online (Sandbox Code Playgroud)

如何格式化从数据库中拉出的结果到页面上的表格列?我宁愿只在标签列中放置TAG而不是整个标签HTML.

有没有办法在点击页面之前拦截结果,格式化它们,然后发布到页面?

码:

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "scripts/server_processing.php",

          "columnDefs": [ {
            "data": "firstname", //this name should exist in you response JSON
            "render": function ( data, type, full, meta ) {
              return '<span class="label label-danger">'+data+'</span>';
            }
          } ]

    } );
} );
Run Code Online (Sandbox Code Playgroud)

Cas*_*Roy 9

使用render列中的选项,如下所示:

$('#example').dataTable( {
  "columnDefs": [ {
    "targets": 0,//index of column starting from 0
    "data": "column_name", //this name should exist in your JSON response
    "render": function ( data, type, full, meta ) {
      return '<span class="label label-danger">'+data+'</span>';
    }
  } ]
} );
Run Code Online (Sandbox Code Playgroud)

检查这里的文档:DataTables columns.render.
使用data以匹配源对象/阵列的属性/键.阅读文档
使用targets,如果你不使用任何键.阅读文档