How to convert null values to empty String in Datatable
Posted By : Shiv Kumar | 21-Apr-2014
In my case,I have a datatable named "Item" with structure like
|Style# | ItemNumber | Size | Color | Quantity |
Size for any item in above table may be null.So in this situation I have to display empty string for null "size" in datatable.
I have solved this problem using mRender property of datatable which provide the facility to process the data before rendering on view.
Here is the way to use mRender property in gsp:
<html> <head> <script> $( document ).ready(function() { var oTable = $('#items').dataTable({ "aoColumnDefs":[{ "aTargets":[ 2 ] // Here we have to give the target column for which value conversion(manipulation) takes place ,"mRender": function( data, type, row ) { // "data" refers to the data of the cell // "type" refers data requested - this will be 'filter', 'display', 'type' or 'sort' // "row" referes to complete row data return (row[2] != "null") ? row[2]: "" ; } }], "sPaginationType": "full_numbers", "sAjaxSource": '${createLink (action:'items',controller:'admin')}', // From this action data is provided in JSON format }); }); </script> </head> <body> <div> <table id="items" width="100%"> <thead> <tr> <th>Style #</th> <th>ItemNumber</th> <th>Size</th> <th>Color</th> <th>Quantity</th> </tr> </thead> <tbody></tbody> </table> </div> <body> </html>
Same thing can be done for multiple columns at a time.
Thanks.
Shiv Kumar
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shiv Kumar
Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.