Datatables根据条件更改列的值

Datatables根据条件更改列的值

本文介绍了jQuery Datatables根据条件更改列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有工作正常的数据表.但是我想稍微调整一下.如您所见,我有这种情况:

I have Datatables which is working fine. However I want to tweak it a bit. As you can see I have this condition:

if ( data.status == 0 )

这意味着,如果status等于零,我将使文本颜色变为红色,否则将颜色变为绿色.但是我也想更改data.status Coz的文本,它是01.如何使文本显示为Pending(如果为零)和Approved(如果为1).

Which means that if the status is equal to zero I will make the text color red and green otherwise. However I also want to change the text of the data.status Coz it is 0 or 1. How can I make the text appear Pending if zero and Approved if 1.

<script>
    $(document).ready(function(){
        $('#LeaveList').DataTable({
            processing: true,
            serverSide: true,
            ajax: 'leave-list',
             "createdRow": function ( row, data, index ) {
                if ( data.status == 0 ) {
                    $('td', row).eq(6).addClass('text-danger');
                }
                else
                {
                    $('td', row).eq(6).addClass('text-success');
                }
            },
            columns: [
                {data: 'id', name: 'id'},
                {data: 'employee_name', name: 'employee_name'},
                {data: 'employee_id', name: 'employee_id'},
                {data: 'from_date', name: 'from_date'},
                {data: 'to_date', name: 'to_date'},
                {data: 'leave_type', name: 'leave_type'},
               // {data: 'department', name: 'department'},
                {data: 'status', name: 'status'},
                {data: 'created_at', name: 'created_at'},
                {data: 'action', name: 'action', orderable: true, searchable: true}
            ]
        });

    });
    </script>

我想在这个jquery部分而不是在数据查询中

I want to this in this jquery part not in the query of data

推荐答案

您可以使用fnCreateCell修改数据.

You can use fnCreateCell to modify your data.

    $(document).ready(function(){
    $('#LeaveList').DataTable({
        processing: true,
        serverSide: true,
        ajax: 'leave-list',
        columns: [
            {data: 'id', name: 'id'},
            {data: 'employee_name', name: 'employee_name'},
            {data: 'employee_id', name: 'employee_id'},
            {data: 'from_date', name: 'from_date'},
            {data: 'to_date', name: 'to_date'},
            {data: 'leave_type', name: 'leave_type'},
           // {data: 'department', name: 'department'},
            {data: 'status', name: 'status',
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {

                   if (sData) {
                         $(nTd).addClass('text-success');
                   }else{
                         $(nTd).addClass('text-danger');
                   }
                }
            },
            {data: 'created_at', name: 'created_at'},
            {data: 'action', name: 'action', orderable: true, searchable: true}
        ]
    });

});

这篇关于jQuery Datatables根据条件更改列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:04