Javascript 如何处理jquery数据表中的数据库空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27502267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:13:23  来源:igfitidea点击:

How to handle database null values in jquery datatable

javascriptjquerydatatablejquery-datatables

提问by rks

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

我有一个 jquery 数据表,其中的数据来自数据库,从 java servlet 中获取。很少有列有空值。因此,我收到了类似的警告

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

DataTables 警告:table id=lplist - 为第 9 行请求未知参数“FeeCompany”。有关此错误的更多信息,请参阅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>

回答by shailb

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

在初始化数据表时在选项中添加 defaultContent。更多细节

columns.defaultContent

列.默认内容

回答by markpsmith

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

您可以使用mRender来指定出现空值时的显示:

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