เพิ่มคอลัมน์ defaulContent เพิ่มเติมตามเงื่อนไข if

สวัสดีตอนบ่าย ฉันได้เติม datatable (ปลั๊กอินตารางสำหรับ jQuery) ด้วยข้อมูลที่ดึงมาจากบริการ wcf และมีคอลัมน์ 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='/th#' 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" , "ความกว้าง": "5%" ?

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

person Pablo Tobar    schedule 05.02.2019    source แหล่งที่มา


คำตอบ (1)


ใช้ตัวเลือก columns.render เพื่อสร้างเนื้อหาสำหรับเซลล์

ตัวอย่างเช่น หากต้องการสร้างเนื้อหาตามค่าของตัวแปร tipo_evento:

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

       return data;
    },
    // ... other options ...
    "autoWidth": true, 
    "orderable": false, 
    "sClass": "alignRight", 
    "sWidth": "5%" 
}
person Gyrocode.com    schedule 05.02.2019
comment
@PabloTobar ใช้คำจำกัดความคอลัมน์ด้านบนแทนคำจำกัดความคอลัมน์ของคุณด้วยตัวเลือก defaultContent - person Gyrocode.com; 05.02.2019
comment
ขอบคุณ ฉันเพิ่มคอลัมน์แล้ว แต่คุณช่วยระบุขนาดได้ไหม เพิ่งแก้ไขคำถามของฉัน โปรดตรวจสอบส่วนท้ายของสิ่งนั้น - person Pablo Tobar; 06.02.2019
comment
@PabloTobar เพียงรวมตัวเลือกอื่นพร้อมกับตัวเลือก render อัปเดตคำตอบของฉัน - person Gyrocode.com; 06.02.2019