简体   繁体   中英

How to handle database null values in jquery datatable

I have a jquery datatable with the data coming from database,fetched from java servlet.Few columns have null values.Because of this i am getting warning like

DataTables warning: table id=lplist - Requested unknown parameter 'FeeCompany' for row 9. For more information about this error, please see http://datatables.net/tn/4

I want those null values to be replaced by empty string.Can someone please guide how to achieve this.

My code snippet is as below

    <script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script
    src="http://cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>

<script
    src="http://cdn.datatables.net/scroller/1.2.2/js/dataTables.scroller.min.js"></script>
<link
    href="http://cdn.datatables.net/scroller/1.2.2/css/dataTables.scroller.css"
    rel="stylesheet" type="text/css" />

<link href="http://cdn.datatables.net/1.10.4/css/jquery.dataTables.css"
    rel="stylesheet" type="text/css" />
<title>Insert title here</title>
<script type="text/javascript">
            $(document).ready(function () {
                $("#lplist").dataTable({
                    "serverSide": true,
                    "sAjaxSource": "/JQueryDataTablesAll/CompanyGsonObjects",

                    dom: "rtiS",
                    scrollY: 450,
                    scrollX:true,
                    "processing": true,                     
                    "aoColumns": [
                                  { "mData": "InsuredName" },
                                  { "mData": "CustAddress_City" },
                                  { "mData": "CustAddress_State" },
                                  { "mData": "CustAddress_Zip" },
                                  { "mData": "CustSurvey_Location" },
                                  { "mData": "PolicyNo" },
                                  { "mData": "ProfitCenter" },
                                  { "mData": "FeeCompany" }, 

                              ]



                });
            });
        </script>
</head>
<body id="dt_example">
    <div id="container">
        <div id="links">
            Server-side processing with object source <br />
        </div>
        <div id="demo_jui">
            <table id="lplist" class="display">
                <thead>
                    <tr>
                        <th>Insured Name</th>
                        <th>City</th>
                        <th>State</th>
                        <th>Zip</th>
                        <th>Survey Location</th>
                        <th>PolicyNo</th>
                        <th>Profit Center</th>
                        <th>Fee Company</th>                        

                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>

    </div>

Add defaultContent in the options while initializing the data table. For more details

columns.defaultContent

Example from the official documentation:

$('#example').dataTable( {
  "columns": [
    null,
    null,
    null,
    {
      "data": "first_name", // can be null or undefined
      "defaultContent": "<i>Not set</i>"
    }
  ]
} );

You can use mRender to specify the display in the event of a null value:

{ 
    "mData": "FeeCompany" 
    'mRender': function (data, type, full) {
        if (full[7] !== null) {
             return full[7]; 
         }else{
             return '';
         }
},
{

  "data": function(data, type, dataToSet) {

    return data.CourseEducator1 ? ? " " + "<br/>" + data.CourseEducator2 ? ? ";

  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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