我们返回的数据不能保证都是正常的,可能包含 null ,显然这个对于最终用户来说是不友好的,那么我们可以这么处理

先有如下数据格式:

//示例数据

{
data:[
{"id":1,"email":"thxopen@datatables.club","office":"Chengdu","first_name":null}
]
} $('#example').DataTable( {
"columns": [
null,
null,
null,
{
"data": "first_name",
// 为 null 或者 undefined 给出友好的提示, 还没有设置 "defaultContent": "<i>还没有设置</i>"
}
]
} );

或者更简单,粗暴的处理:

$('#example').DataTable( {
"columns": [
null,
null,
null,
{
"data": "office",
// 如果上面数居中office对于的值为 null 或者 undefined 则直接显示为空字符串 "defaultContent": ""
}
]
} );

当然 defaulContent 的用法还可以再强大点,比如给最后一列添加编辑按钮

//使用 columnDefs 指定

$('#example').DataTable( {
"columnDefs": [
{
"data": null,
"defaultContent": "<button>编辑</button>",
"targets": -1 // 这里 -1 代表最后一列 }
]
} ); // 使用 columns 指定 $('#example').dataTable( {
"columns": [
null,
null,
null,
{
"data": null,
"defaultContent": "<button>编辑</button>"
}
]
} );

参考网址:http://datatables.club/manual/daily/2016/05/04/option-columns-defaultContent.html

datatables 给字段设置默认值,屏蔽没有字段的错误的更多相关文章

  1. c#为字段设置默认值,以及构造函数初始化List对象。

    1.为字段设置默认值 /// <summary> /// 默认值 /// </summary> ; ; /// <summary> /// 页的大小 /// < ...

  2. JPA为字段设置默认值

    http://blog.csdn.net/u011983531/article/details/51286839 在使用JPA时,如果需要为属性设置默认值,很自然的,你可能会想到用下面的方式. @Co ...

  3. Django的model中日期字段设置默认值的问题

    之前写过这样一个model: class MonthlyFeeMember(models.Model): worker = models.ForeignKey(Student, verbose_nam ...

  4. MySQL 日期类型及默认设置 (除timestamp类型外,系统不支持其它时间类型字段设置默认值)

    MySQL 日期类型及默认设置 之前在用 MySQL 新建 table,创建日期类型列时遇到了一些问题,现在整理下来以供参考. MySQL 的日期类型如何设置当前时间为其默认值? 答:请使用 time ...

  5. SharePoint 多行文本字段设置默认值

    前言 最近有这样一个需求,创建一个表单,里面有多行文本字段,但是要求内容默认带一个表格,这样用户新建项目的时候,就可以直接填表格了. 好吧,这样的需求我们可以通过JavaScript实现. 1.默认的 ...

  6. sqlite数据库中为字段设置默认值为当前时间

    开始 `creation_time` NUMERIC DEFAULT (datetime('now','localtime')), `update_time` NUMERIC DEFAULT (dat ...

  7. 查询多表集合(union)、查询时建临时字段、查询时给字段设置默认值

    () UNION () UNION (select i.create_time as time,i.investment_amount as amount,i.invest_state as stat ...

  8. es新增字段,并设置默认值

    重新设置mapping 添加新的字段. 设置es允许脚本执行:elasticsearch.yml script.inline: true 然后执行脚本 POST linewell_assets_mgt ...

  9. mysql使用default来设置字段的默认值

    mysql创建表时,我们可以使用default来设置表字段的默认值,这样当向表中插入或添加数据时,如果没有为此字段设置任何值,则使用default默认值来填充该字段的值 在使用create table ...

随机推荐

  1. $.ajax、$.post[转]

    jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求 参数: url (String) : 发送请求的URL地址. data ...

  2. Java反射破坏单例模式

    今天电话面试的时候问到了,Google了一下 原出处: http://blog.csdn.net/lws332969674/article/details/8125893 一. Java中的反射技术可 ...

  3. Snipaste的详细安装和使用

    Snipaste安装和使用 1:snipaste的安装 步骤一: https://zh.snipaste.com/  ,去此官网下载. 步骤二:由于此是个绿色软件,直接解压即可. 步骤三:使用,见官网 ...

  4. 常用CSS样式设置

    文字 我们以div标签举例,来设置常见的文字样式 <div>今天天气真晴朗!</div> div { /* 文字大小为14像素 */ font-size: 14px; /* 文 ...

  5. tween.js 插件

    1.是什么? jQueryTween是一款轻量级的jQuery补间动画工具库插件.使用jQueryTween可以制作出各种平滑的动画过渡效果.该插件基于tween.js,旨在简化各种补间动画操作,提供 ...

  6. libmysqlclient.so.16未找到方法

    用mysql命令登录的时候报错: [root@iZ www]# mysql -uroot -p mysql: error while loading shared libraries: libmysq ...

  7. maven struts2工程StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter

    maven搭建struts2工程时报错 严重: Exception starting filter struts2java.lang.ClassCastException: org.apache.st ...

  8. selenium中Alter等弹出对话框的处理

    昨天使用selenium做自动化测试,发现部分页面会弹出alert对话框,找了写资料,大概的意思就是要给弹出的对话框做出相应,不然,后续的处理会失败. _driver.SwitchTo().Alert ...

  9. selenium Element not found in the cache - perhaps the page has changed since it was looked up接解决

    selenium Element not found in the cache - perhaps the page has changed since it was looked up.这个问题爆出 ...

  10. mybatis返回boolean值时数据库返回null

    Servlet.service() for servlet [springDispatcherServlet] in context with path [/ms] threw exception [ ...