陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. web
  3. 正文

怎样用JS写一个下拉加载数据的table

2017年5月19日 1060点热度 0人点赞 0条评论

效果和方法完全由自己写js控制实现,这边写下流程和原理。

STEP1:

表格采用bootstrap效果样式实现。

STEP2:

写好后台调用数据的接口,可以使前台ajax可以通过调用接口来获取后台的分页数据

STEP3:

前台调用代码示例:

$.ajax(
         {
             url: '@Url.Action("XXXFlightPartial", "XXXBSAttention")',
             type: 'POST',
             dataType: 'text',
             cache: false,
             data: { .............},
             timeout: 50000,
             error: function () {
                 alert("Time Out!");
             },
             success: function (result, status, xmlHttp) {

                 if (result.indexOf('/Account/Login') > -1) {
                     window.location.href = "@Url.Action("Login", "Account")";
                 }
                 else {
                     $('#loadFlight').empty();
                     $('#loadFlight').html(result);
                 }
             }
         });
   }

返回result的接口由自己控制,可以是直接的html代码,也可以是json结果集,通过js处理后绑定到table中。

 

STEP4:

这边最重要的讲解是,监控滚动条,滚动到底部的时候触发ajax向后台请求新数据,格式化后append到table的后部,在这边加载的时候放一个延时加载中的样式动图和说明,比如:正在加载中... 。

在append数据前再删除正在加载的样式和说明。

相关代码如下:

<script type="text/javascript">
    //美化内部滚动条
    $('#tScroll').niceScroll({
        cursorcolor: "#ccc",//#CC0071 光标颜色
        cursoropacitymax: 1, //改变不透明度非常光标处于活动状态(scrollabar“可见”状态),范围从1到0
        touchbehavior: false, //使光标拖动滚动像在台式电脑触摸设备
        cursorwidth: "5px", //像素光标的宽度
        cursorborder: "0", // 游标边框css定义
        cursorborderradius: "5px",//以像素为光标边界半径
        autohidemode: false //是否隐藏滚动条
    });

    //初始化排序
    $("#dynamic-table").tablesorter();


    //排序的方向
    $(".thclass").click(function(){
        if($(this).hasClass("sorting"))
        {
            $(".thclass").removeClass("sorting_asc").addClass("sorting");
            $(".thclass").removeClass("sorting_desc").addClass("sorting");
            $(this).removeClass("sorting").addClass("sorting_asc");
        }
        else if($(this).hasClass("sorting_asc"))
        {
            $(".thclass").removeClass("sorting_asc").addClass("sorting");
            $(".thclass").removeClass("sorting_desc").addClass("sorting");
            $(this).removeClass("sorting").addClass("sorting_desc");
        }
        else
        {
            $(".thclass").removeClass("sorting_asc").addClass("sorting");
            $(".thclass").removeClass("sorting_desc").addClass("sorting");
            $(this).removeClass("sorting").addClass("sorting_asc");
        }
    });

    var timer;
    //滚动条状态
    var stop = false;
    //滚动条监控
    $(document).ready(function () {
        var clientHeight = 550;
        //距下边界长度/单位px
        var range = 0;
        //设置加载最多次数
        var maxnum = @(totalRecord/20);
        var num = 1;
        var totalheight = 0;
        //水平滚动条值
        var lastLeftScroll=0;
        //垂直滚动条值
        var lastRightScroll=0
        //主体元素
        var main = $("#dynamic-table tbody");
        $(".tScroll").scroll(function () {
            if (stop)
                return;
            //判断是不是左右滚动条
            var scLeft= $(".tScroll").scrollLeft();
            if(scLeft!=lastLeftScroll)
            {
                lastLeftScroll=scLeft;
                return;
            }
            //滚动条距顶部距离
            var srollPos = $(".tScroll").scrollTop();

            //滚动方向判断
            var directionSroll;
            if(srollPos<lastRightScroll) {
                directionSroll=false;   //向上滚动
            } else {
                directionSroll=true;
            }
            lastRightScroll=srollPos;

            //监听鼠标滚动结束事件
            $("#flowThead").css({"display":"none"});
            if(timer) {
                clearTimeout(timer);
            }
            timer = setTimeout(function(){
                if(srollPos>40)
                {
                    $("#flowThead").css({"display":"block"});
                    $("#flowThead").css({"top":srollPos-1,"position":"absolute"});
                    $("#dynamic-table").css({"top":0,"position":"absolute"});
                }
                else
                {
                    $("#flowThead").css({"display":"block"});
                    $("#flowThead").css({"top":0,"position":"absolute"});
                    $("#dynamic-table").css({"top":44,"position":"absolute"});
                }
            },400);


            totalheight = parseFloat(clientHeight) + parseFloat(srollPos);
            if (($("#dynamic-table").height() - range) <= totalheight && num != maxnum) {
                stop = true;
                @{
                    int HeaderCount = columnHeaders.Split(',').Count()+6;
                }
                main.append("<tr id='loadimg'> <td colspan='@(HeaderCount)'><img src='../Resource/BSResource/front-style/images/input-spinner.gif' />loading data...</td></tr>")
                $('#tScroll').niceScroll().resize();
                setTimeout(loadData, 1000);
                num++;
            }
        });
    });

    // 下拉加载最新的数据
    function loadData() {
        var main = $("#dynamic-table tbody");
        $("#loadimg").remove();
        $.ajax(
        {
            url: '/BSAttention/FlightPartialByPage',
            type: 'POST',
            dataType: 'text',
            cache: false,
            data: { "DepartCode": "@(departCode)", "DescentCode": "@(descentCode)", "PageIndex": @(pageindex+1), "SelectMode": "@(selectMode)", "Sort": "sortflight,asc", "ColumnHeaders": "@(columnHeaders)", "IsFirst": 1 },
            timeout: 50000,
            error: function () {
                alert("Time Out!");
            },
            success: function (result, status, xmlHttp) {
                main.append(result);
                $('#tScroll').niceScroll().resize();
                $("#dynamic-table").trigger("update");
            }
        });
        stop = false;
    }

    //判断返回值是否是空
    var isArray = function(obj) {
        return (Object.prototype.toString.call(obj) === '[object Array]' && obj.length>0);
    }

    //获取弹出框View
    function GetFlowControls(id,callsign)
    {
        $.ajax(
      {
          url: '/AjaxService/vffcHandler.ashx',
          type: 'GET',
          cache: false,
          dataType:"json",
          data:{stype:"vf",sid:id},
          timeout: 50000,
          error: function () {
              alert("Time Out!");
              console.log(result);
          },
          success: function (result, status, xmlHttp) {
              $("#viewModalLabel").html("FLOW CONTROL INFO-"+callsign);
              if(!isArray(result)){
                  $('#viewModalDiv').css({"width":600});
                  $('#viewContent').html("There have no flow control information about this flight.");
              }
              else
              {
                  var tableValue="<table class=\"table\" style='table-layout:fixed ; width:100%;'><tr><th style='width:150px'>Source</th><th style='width:200px'>Start Time</th><th style='width:200px'>End Time</th><th class=\"thfixed\">Description</th><th style='width:200px'>Record Time</th></tr><tbody>";

                  var index=-1;
                  $.each(result,function(i,o)
                  {
                      index=i;
                      tableValue+="<tr><td><a class =\"linkB\" target=\"_blank\" href=\"/FlowControl/RealTimeFlowControl/" + o.Id +"\" title=\"see the detail\">"+o.Source+"</a></td><td style='width:200px'>"+o.StartTime+"</td><td  style='width:200px'>"+o.EndTime+"</td><td>"+o.DescriptionEnglish+"</td><td  style='width:200px'>"+o.RecordTime+"</td></tr>";
                  });
                  tableValue+="</tbody></table>";
                  $('#viewModalDiv').css({"width":1200});
                  $('#viewContent').html(tableValue);
              }
          }
      });
    }

</script>

 

标签: jquery js
最后更新:2021年4月2日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
centos python nginx linux C# winform DevExpress js
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
python裁剪pdf中的图片 fastapi做后台的跨域官方BUG以及修复方式 DevExpress控件-GridControl控件相关属性中文详解 C#获取音乐相关信息和相关问题解决 DreamSkin自定义美化控件-RoundButton圆角矩形按钮控件 python opencv剪切视频成多张图片

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS