对特定列数据应用条件 - jquery DataTable [英] Apply a condition on specific column data - jquery DataTable

查看:18
本文介绍了对特定列数据应用条件 - jquery DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我有下表:

红色围起来的一栏显示2种账户,值1= Free和值2 = paid(免费、付费帐户).

The column which enclosed by red color display 2 types of account, the value 1= Free and value 2 = paid (free, paid accounts).

我想在渲染数据之前,应用条件将 1 更改为 free 并将 2 更改为 paid>.

I want before rendering the data, apply a condition to change the 1 to free and 2 to paid.

就是这样.

表初始化:

var dataTableY = $('#table').DataTable({
    serverSide: true,
    ajax: {
        url: 'directory/class/method'
    },
    processing: true,
    scrollY: 400,
    paging: true,
    info: true,
    select: {
        style: 'os'
    },
    pagingType: 'full_numbers',
    language: {
        url: 'DataTables/lang/english.json'
    }
});

推荐答案

使用列渲染器:

var table = $('#example').dataTable({
    //...
    columnDefs : [
        { targets : [4],
          render : function (data, type, row) {
             return data == '1' ? 'free' : 'paid'
          }
        }
   ]
})   

如果列值为1,渲染函数将返回'free',否则返回'paid'.如果您有更多值,或者例如还需要返回 'N/A',您可以使用 switch.

The render function will return 'free' if the column value is 1, otherwise 'paid'. You could use a switch if you have more values, or for example need to return a 'N/A' too.

    columnDefs : [
        { targets : [4],
          render : function (data, type, row) {
            switch(data) {
               case '1' : return 'free'; break;
               case '2' : return 'paid'; break;
               default  : return 'N/A';
            }
          }
        }
   ]

这篇关于对特定列数据应用条件 - jquery DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆