Jquery DataTable Dynamic Hidden column (depending on if there is no Value)

Source: Internet
Author: User

a scene :

The front end invokes data back when it is initialized with a datatable, and the requirement is to dynamically determine whether a column is displayed based on the value of a returned flag bit.

This is the case where the current return value has an active offer range

This is a case of no event offer

You can see that the "campaign offer range" is Hidden.

second, the realization method and principle

Difficulty: The DataTable is the JS dynamic generation result set (<tr><td>), that is, we can not manually add the HTML tags in the ID or class

Implementation Process: in the "columns": [] in the current column as {orderable:false,sclass: "hiddencol", data: ' name '} in the name column, add sclass: "hiddencol", This attribute will add this class to the TD at the forefront and to the column names we defined earlier

After adding the class, we can now display the CSS of the current class or Not.

We need to include in the initial parameters of the DataTable

  dataTable: {
    function (settings, json) {                      },"columns": [{.... }]
}

InitComplete is the method that is executed after the DataTable initializes the sink, and passes in Settings and JSON (all the data returned by the server can be viewed in Console.log (JSON))

In this method, you can determine whether the value returned by the server is active, then the class to control the display, the following to pay my way, I deleted a lot of my business logic code, so more intuitive

dataTable: {"ajax": {                        "url": "", Type:' GET ',                        "dataType": ' JSON '                    },                    "initcomplete":function(settings, Json) {vardata =json.data; if(data.length>0){                            varName = Data[0].name; if(name== "" | | name==Undefined) {                                $(". hiddencol"). CSS ("display", "none"); }                        }Else{                            $(". hiddencol"). CSS ("display", "none"); }                    },                    "columns": [                        {                           .....                        } , {orderable:false, sclass: "hiddencol", data: ' name '}                                              ]          }                   

Jquery DataTable Dynamic Hidden column (depending on if there is no Value)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.