简体   繁体   English

在特定列数据上应用条件 - jquery DataTable

[英]Apply a condition on specific column data - jquery DataTable

Firstly, I have the following table: 首先,我有下表:

The column which enclosed by red color display 2 types of account, the value 1 = Free and value 2 = paid (free, paid accounts). 红色包围的栏显示2种账户,价值1 = Free ,价值2 = paid (免费,付费账户)。

I want before rendering the data, apply a condition to change the 1 to free and 2 to paid . 我想在渲染数据之前,应用条件将1更改为free ,将2更改为paid

that's it. 而已。

Table initialization: 表初始化:

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'
    }
});

Use a column renderer : 使用列渲染器

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

The render function will return 'free' if the column value is 1, otherwise 'paid' . 如果列值为1,则渲染函数将返回'free' ,否则将'paid' You could use a switch if you have more values, or for example need to return a 'N/A' too. 如果您有更多值,可以使用switch ,或者例如也需要返回'N/A'


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM