2016-06-10 41 views
0

我正在使用datatables服务器端,并且当我想要使用列筛选器搜索服务器端数据时出现问题,因为渲染值与数据库字段值不匹配。例如:在我的数据库中,状态列中有0和1。在我的数据表中,我显示状态列和渲染来获得这个:if status = 0,然后我将显示禁用,如果我有1,我会显示启用。使用渲染Jquery数据表格筛选

这里是我的代码片段:

$(document).ready(function() { 

/* ----------------------------------------------------- */ 
/* ----------------- DATATABLES HISTORY ---------------- */ 
/* ----------------------------------------------------- */ 

$('#historyTable').DataTable({ 
    dom: "t<'col-sm-5'i><'col-sm-7'p>", 
    autoWidth: true, 
    aaSorting: [[1, 'asc']], 
    serverSide: true, 
    lengthChange: false, 
    ajax: { 
     url: 'history', 
     method: 'POST' 
    } 
    columns: [ 
     {data: "id"}, 
     {data: "name", orderData: [ 1, 0 ]}, 
     {data: "status", render: renderStatus, orderData: [ 2, 0 ]} 
    ] 
}); 

var historyTable = $('#historyTable').DataTable(); 

// Render status 
function renderStatus(data, type, dataToSet){ 
    if(dataToSet.status == '1'){ 
     return "enabled"; 
    }else{ 
     return "disabled"; 
    } 
} 

// Global search 
$('#historySearch').on('keyup', function(){ 
    historyTable.search($(this).val()).draw(); 
}); 

// Length menu 
$('#historyMenu').on('change', function(){ 
    historyTable.page.len($(this).val()).draw(); 
}); 

// Setup - add a text input to each footer cell 
$('#historyTable tfoot td').each(function() { 
    var title = $('#historyTable tfoot td').eq($(this).index()).text(); 

    $(this).html('<input type="text" />'); 
}); 

// Apply the search 
historyTable.columns().eq(0).each(function (colIdx) { 
    $('input', historyTable.column(colIdx).footer()).on('keyup change', function() { 
     historyTable 
     .column(colIdx) 
     .search(this.value) 
     .draw(); 
    }); 
}); 

}); 

你知不知道我该怎么做,以配合服务器端值呈现值?

回答

0

您可以通过列的名称服务器:

$('#historyTable').DataTable({ 
    dom: "t<'col-sm-5'i><'col-sm-7'p>", 
    autoWidth: true, 
    aaSorting: [[1, 'asc']], 
    serverSide: true, 
    lengthChange: false, 
    ajax: { 
     url: 'history', 
     method: 'POST' 
    } 
    columns: [ 
     {data: "id", name: "column1_name"}, 
     {data: "name", orderData: [ 1, 0 ], name: "column2_name"}, 
     {data: "status", render: renderStatus, orderData: [ 2, 0 ], name: "column3_name"} 
    ] 
}); 

我不知道如何动态设置的名称。

+0

为什么要将列名传递给服务器? – John