根据if条件添加额外的defaulContent列

时间:2019-02-05 20:21:28

标签: jquery datatables

下午好,我用从wcf服务检索到的数据填充了数据表(jQuery的表插件),它的defaultContent列具有链接并且运行良好,但是现在我需要创建另一个defaultContent列链接取决于if条件,如下所示:

if (tipo_evento == 3213) { ... add a defaultContent...}

我尝试将if添加到下面的代码中,但是这是不可能的,您能告诉我如何添加额外的列吗?这是我表的代码

function cargarTabla() {
            $('#myTable').DataTable({
                searching: false,
                paging: false,
                responsive: true,
                ordering: false,
                bInfo: true,
                bLengthChange: false,
                processing: true,
                info: true,
                deferRender: true,
                orderMulti: false,
                bAutoWidth: false,
                "ajax": {
                    "url": "/Home/CargarTabla?id=" + noDocumento + "&grupo=" + grupoDT,
                    "type": "GET",
                    "datatype": "json"
                },
                "columns": [
                        { "data": "nombres", "autoWidth": false, "orderable": false, "sClass": "alignRight", "sWidth": "18%" },
                        { "data": "apellidos", "autoWidth": false, "orderable": false, "sClass": "alignRight", "sWidth": "18%" },
                        { "data": "dui", "autoWidth": false, "orderable": false, "sClass": "alignRight", "sWidth": "5%" },
                        { "data": "numero_isss", "autoWidth": false, "orderable": false, "sClass": "alignRight", "sWidth": "5%" },
                        { "data": "cargo_participante", "autoWidth": false, "orderable": false, "sClass": "alignRight", "sWidth": "20%" },
                        { "data": "genero", "autoWidth": false, "orderable": false, "visible": false },
                        { "data": "nivel_puesto", "autoWidth": false, "orderable": false, "visible": false },
                        { "data": "grupo", "autoWidth": false, "orderable": false, "visible": false },
                        { "defaultContent": " <a href='#' id='select'>Sustituir</a>  ", "autoWidth": true, "orderable": false, "sClass": "alignRight", "sWidth": "5%" }
                ],
                "oLanguage": {
                    "sEmptyTable": "No hay registros disponibles",
                    "sInfo": " _TOTAL_ registros. Mostrando de (_START_ a _END_)",
                    "sLoadingRecords": "Por favor espera - Cargando...",
                    "sSearch": "Filtro:",
                    "sLengthMenu": "Mostrar _MENU_",
                    "oPaginate": {
                        "sLast": "Última página",
                        "sFirst": "Primera",
                        "sNext": "Siguiente",
                        "sPrevious": "Anterior"
                    },
                    "oAria": {
                        "sSortAscending": ": Activar para ordenar la columna de manera ascendente",
                        "sSortDescending": ": Activar para ordenar la columna de manera descendente"
                    }
                }
            });
        }

我刚刚从Gyrocode.com添加了此代码,并且工作正常,但是刚添加的列在我的表中占用了太多空间,如何添加“ autoWidth”:true,“ orderable”:false,“ sClass”: “ alignRight”,“ sWidth”:“ 5%”?

"render": function (data, type, full, meta) {
                                if (type === 'display') {
                                    if (tipo_evento == 3213) {
                                        data = " <a href='#' id='Teliminar'>Eliminar</a> " +"|"+ " <a href='#' id='select'>Sustituir</a> ";
                                    } else {
                                        data = " <a href='#' id='select'>Sustituir</a> ";
                                    }
                                }
                                return data;

1 个答案:

答案 0 :(得分:1)

使用columns.render选项为单元格生成内容。

例如,要基于tipo_evento变量的值来生成内容:

{ 
    "render": function(data, type, full, meta){
       if(type === 'display'){
          if(tipo_evento == 3213){
             data = "";
          } else {
             data = " <a href='#' id='select'>Sustituir</a> ";
          }
       }

       return data;
    },
    // ... other options ...
    "autoWidth": true, 
    "orderable": false, 
    "sClass": "alignRight", 
    "sWidth": "5%" 
}