สวัสดีตอนบ่าย ฉันได้เติม 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;