- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title></title>
- <link href="DataTables/css/jquery.dataTables.min.css" rel="stylesheet" />
- </head>
- <body>
- <script src="DataTables/js/jquery.js"></script>
- <script src="DataTables/js/jquery.dataTables.min.js"></script>
- <table id="example">
- </table>
- <script>
- Date.prototype.Format = function (fmt) { //author: meizz
- var o = {
- "M+": this.getMonth() + 1, //月份
- "d+": this.getDate(), //日
- "h+": this.getHours(), //小时
- "m+": this.getMinutes(), //分
- "s+": this.getSeconds(), //秒
- "q+": Math.floor((this.getMonth() + 3) / 3), //季度
- "S": this.getMilliseconds() //毫秒
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
- }
- for (var k in o) {
- if (new RegExp("(" + k + ")").test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
- }
- }
- return fmt;
- }
- var id = "";
- var oTable = $("#example").DataTable({
- "serverSide": true,
- //分页,取数据等等的都放到服务端去
- "deferRender": true,
- //当处理大数据时,延迟渲染数据,有效提高Datatables处理能力
- "destory": true,
- "ajax": {
- "dataType": 'json',
- "type": "POST",
- "url": "/GridSheet/dataGridData",
- "data": function(d) {
- d.id = id;
- },
- "async": false
- },
- "columns": [{
- "data": "id",
- "width": "100px",
- "title": "id"
- },
- {
- "data": "name",
- "width": "150px",
- "title": "姓名"
- },
- {
- "data": "birthday",
- "width": "200px",
- "title": "生日",
- render: function(data, type, row, meta) {
- //先讲 时间格式化
- //这类问题主要给大家讲逻辑,因为都是类似的问题,类似的解决方案
- //最基础的解决方案: 一、直接在数据源就格式化为常见的格式(sql或者后台代码格式化);二、在dt里面格式化;
- //在js格式化时间的三种方式,我这里示范一种
- //具体方法的链接:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html
- return (new Date(data)).Format("yyyy-MM-dd hh:mm:ss"); //date的格式 Thu Apr 26 2016 00:00:00 GMT+0800
- }
- },
- {
- "data": "a",
- "width": "350px",
- "title": "a",
- render: function(data, type, row, meta) {
- //然后是 内容太多用。。。。表示、内容不换行,鼠标移上去显示详情
- //这类问题最简单的肯定是使用css解决
- // //1. table-layout: fixed 由于table-layout的默认值是auto,
- //即table的宽高将取决于其内容的多寡,如果内容的体积无法估测,那么最终表格的呈现形式也无法保证了,
- //fixed一下就好了。(注意:此样式是关键)
- // //2. white-space: nowrap 是为了保证无论单元格(TD)中文本内容有多少,
- //都不会自动换行,此时多余的内容会在水平方向撑破单元格。
- // //3. overflow: hidden 隐藏超出单元格的部分。
- // //4. text-overflow: ellipsis 将被隐藏的那部分用省略号代替。
- //这4个css样式都可以百度出来的,第一个是写在table标签里面的,2、3、4写在td里面
- //假设这个tomuchcontent 就是本列的值 data
- var tomuchcontent = "asdasdaddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
- //我这里做个简单的示范
- return '<div id="a" style="width:100px;height:100px;overflow: hidden;text-overflow: ellipsis;" '+
- 'title="‘+tomuchcontent +’">‘+tomuchcontent +’</div>';
- }
- },
- {
- "data": null,
- "width": "350px",
- "title": "b",
- render: function(data, type, row, meta) {
- //最后一个 同时显示多个列的内容
- //我对这个问题的理解是 比方我有两个字段需要同时显示在一个td里面,或者一个td里面有两个按钮
- //这个columns.data就完全可以给null了,相应的columns.render().data这里就也是null
- //按钮的就是自己在里面拼click事件以及对应的按钮样式了
- //return '<a href="javascript:void(0);" onclick="del("'+row.id+'")">' + 删除
- // + '</a><a href="javascript:void(0);" onclick="mod("'+row.id+'","'+row.name+'")">' + 编辑 + '</a>';
- return '<label>' + row.id + '</label> <label>' + row.name + '</label>';
- }
- }]
- });
- </script>
- </body>
- </html>