datatable 行定时查询功能

$.namespace("huania.device.rowupdate");
huania.device.rowupdate = function(){
    $table = $("#mapshowid");
    // 定时器
    var myTimer = null;    
    var table = null;
    
    return {
        startInterval : function(){// 定时
            if (myTimer == null)
                myTimer = setInterval("huania.device.rowupdate.updateDev2()", 2000);
        },
        stopInterval : function(){// 关闭定时
            if (myTimer != null) {
                myTimer = clearInterval(myTimer);
            }
        },
        updateDev : function(){// 更新设备状态
            this.updateDev2();
            this.startInterval();
        },
        updateDev2 : function(){
            $.ajax({
                type : "GET",
                url : CTX + "/home/inten/intendev2.json",
                data : null,
                dataType : 'json',
                success : function(data2) {
                    for ( var id in data2) {
                        if(table !=null){
                            var row = table.row( '#'+id ).node();
                            $(row).css("color", 'green');
                            huania.device.baidu.setIcon(id, CTX + "/images/green.png");
                        }else{
                            huania.device.baidu.setIcon(id, CTX + "/images/red.png");
                            $(row).css("color", 'red');
                        }
                    }
                },
                error : function(XMLHttpReqquest, error, errorThrown) {
                }
            });
        },
        detailFormatter : function(index, row){// 更新设备状态
            var html = [];
            $.each(row, function(key, value) {
                html.push('<p><b>' + key + ':</b> ' + value + '</p>');
            });
            return html.join('');
        },
        devNumFormatter : function(value, row, index){
            return value;
        },
        init : function(){
            table = $table.DataTable({
                "language": {
                    "decimal":        "",
                   "emptyTable":     "表中没有可用的数据",
                   "info":           "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )",
                   "infoEmpty":      "无记录",
                   "sInfo" : "当前第 _START_ 到  _END_ 台     总设备数量为 _TOTAL_ 台",     
                   "infoFiltered":   "(从 _MAX_ 条记录过滤)",
                   "infoPostFix":    "",
                   "thousands":      ",",
                   "lengthMenu":     "每页 _MENU_ 条记录",
                   "loadingRecords": "加载中...",
                   "processing":     "查询中...",
                   "search":         "检索:",
                   "zeroRecords":    "没有找到记录",
                   "paginate": {
                       "first":      "第一页",
                       "last":       "最后一页",
                       "next":       "下一页",
                       "previous":   "上一页"
                   }
                },
                   "aLengthMenu" : [18, 40, 80,100,150,200,300,500,1000], //更改显示记录数选项    
                "iDisplayLength" : 18, //默认显示的记录数    
                "bAutoWidth" : true, //是否自适应宽度    
                "bScrollInfinite" : true, //是否启动初始化滚动条    
                "bScrollCollapse" : true, //是否开启DataTables的高度自适应,当数据条数不够分页数据条数的时候,插件高度是否随数据条数而改变    
                "bPaginate" : true, //是否显示(应用)分页器    
                "bInfo" : true, //是否显示页脚信息,DataTables插件左下角显示记录数    
                "sPaginationType" : "full_numbers", //详细分页组,可以支持直接跳转到某页    
                "bSort" : true, //是否启动各个字段的排序功能    
                "aaSorting" : [[1, "asc"]], //默认的排序方式,第2列,升序排列    
                "bFilter" : true, //是否启动过滤、搜索功能    
                "initComplete": function(settings, json) {
                    //筛选功能
                     this.api().columns().every( function () {
                            var column = this;
                            var select = $('<select><option value=""></option></select>')
                                .appendTo( $(column.footer()).empty() )
                                .on( 'change', function () {
                                    var val = $.fn.dataTable.util.escapeRegex(
                                        $(this).val()
                                    );
            
                                    column
                                        .search( val ? '^'+val+'$' : '', true, false )
                                        .draw();
                                } );
            
                            column.data().unique().sort().each( function ( d, j ) {
                                select.append( '<option value="'+d+'">'+d+'</option>' )
                            } );
                        } );
                        
                    // 更新设备状态
                    huania.device.rowupdate.updateDev();
                }
            });
                        
            $('#mapshowid tbody').on( 'dblclick', 'tr', function () {
                if ( $(this).hasClass('selected') ) {
                    $(this).removeClass('selected');
                }
                else {
                        table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
                window.location.href = CTX + "/home/inten/intendevstatus/" + $(this).attr("id");
            } );
        }        
    }
}();

huania.device.rowupdate.init();

猜你喜欢

转载自blog.csdn.net/hehaibo2008/article/details/52317419