js超簡潔表格


完整demo下載


顯示效果

表格樣式

使用示例


html

<div id = "cs_table_box" class = "AJRD_datatable_box" >
     <table id= "cs_table" class= "AJRD_datatable" ></table >
</div>

js

var _data=[
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'},
    {a:'adrian',b:'simon',c:'jack',d:'yxkk'}
]

var cs = new AJRD_table('cs_table',{
    "headers":["1","2","3","4"],   //必須
    "data":_data,        //必須
    "displayNum": 20    //必須   默認 10
});
cs.init(0,9);

對象方法


init(begin,end)

  • begin 顯示起始位置
  • end 顯示截止位置
    初始化表格

refresh()

刷新表格

setOption(option)

重設(shè)參數(shù)

setWidth(width)

設(shè)置寬度

setHeight(height)

設(shè)置高度

源碼


/**
 * 表格對象
 * @param options
 */
function AJRD_table(tableId,options){
    this._tableId=tableId;
    this._options=options;
    /**
     * 抽象化表格
     */
    function abstractTable(){
        // ---------內(nèi)容屬性
        this.id = null;         // 每個表格都有唯一的一個id
        this.tableobj = null;  //表格對象
        this.rowNum = 0;       //行數(shù)
        this.colNum = 0;      //列數(shù)
        this.header = [];     //表頭數(shù)據(jù)
        this.content = [];   //body數(shù)據(jù)
        // ----------提供外部使用獲得表格內(nèi)部數(shù)據(jù)
        this.currentClickRowID = 0;   //當(dāng)前點擊的行數(shù)據(jù)
        // --- 通過表頭來獲得這張表的列數(shù)
        this.getColNum = function(){
            this.colNum = this.header.length;
            return   this.colNum;
        }
        // -----------  表格自我構(gòu)建行為
        this.clearTable = function(){};
        this.showHeader = function(){};
        this.showContent = function(begin,end){};
        this.showFoot = function(){};
    
        // --------- 分頁功能屬性
        this.allDataNum = 0;  // 總數(shù)據(jù)條數(shù)
        this.displayNum = 10; // 每頁顯示條數(shù)
        this.maxPageNum = 0;  // 最大頁碼值
        this.currentPageNum =1;// 當(dāng)前頁碼值
        //tfoot分頁組
        this.groupDataNum = 10;  //每組顯示10頁
        this.groupNum = 1;       //當(dāng)前組
    
        // -------- 分頁功能行為
        this.paginationFromBeginToEnd = function(begin,end){}
        this.first =  function(){}//首頁
        this.last = function(){}//最后一頁
        this.prev = function(){}//上一頁
        this.next = function(){}//下一頁
        this.goto = function(){} //跳到某頁
    
        // ----------- 表格初始化
        this.init = function(begin,end){}
    }

    /*
     表格對象模板
     */
    function tableTemplet(table_id){
        abstractTable.call(this);
        this.id = table_id;
    }
    
    var _self=this;
    
    if(!options){return;}
    if(!$.isPlainObject(options)){return;}

    tableTemplet.call(this,_self._tableId);
    console.log(this._tableId);
    //得到表格對象
    this.tableobj = $("#"+this._tableId);
    //清空表格內(nèi)容
    this.clearTable = function(){
        this.tableobj.html(" ");
    }
    // 實現(xiàn)分頁行為
    this.paginationFromBeginToEnd= function(x,y){
        this.maxPageNum = Math.ceil(this.allDataNum/this.displayNum);
        var arrPage = [];
        for(var i= x;i<y;i++){
            arrPage.push(this.content[i]);
        }
        return arrPage;
    }

    this.showHeader = function(){
        if(this.header != null){
           var  $thead = $("<thead>"),
                $tr = $("<tr>"),
                $th;
            for(var i=0;i<this.colNum;i++){
                $th = $("<th>").html(this.header[i]);
                $th.appendTo($tr);
            }
            $tr.appendTo($thead);
            $thead.appendTo(this.tableobj)
        }
    }
    //初始化tbody
    this.showContent = function(begin,end){
        if(this.content != null){
            var $tbody = $("<tbody>"),
                $tr,
                $td;
            var tempDaTa = this.paginationFromBeginToEnd(begin,end),
                len = tempDaTa.length;
            // 循環(huán)創(chuàng)建行
            for(var i=0;i<len;i++){
                $tr = $("<tr>").appendTo($tbody);
                if(i%2==1){
                    $tr.addClass("evenrow");
                }
                // 循環(huán)創(chuàng)建列  取得對象中的鍵
                for(var key in tempDaTa[i]){
                    $td = $("<td>").html(tempDaTa[i][key]).appendTo($tr);
                }
            }
            this.tableobj.append($tbody);
        }
    }
    //初始化tfoot
    this.showFoot = function(){
       var $tfoot = $("<tfoot>"),
           $tr = $("<tr>"),
           $td = $("<td>").attr("colspan",this.colNum).addClass("paging");
           $tr.append($td);
           $tfoot.append($tr);
           this.tableobj.append($tfoot);
           this.pagination($td);
    }
    //表格分頁
    this.pagination = function(tdCell){
        var $td= typeof(tdCell) == "object" ? tdCell : $("#" + tdCell);
        //首頁
        var oA = $("<a/>");
        oA.attr("href","#1");
        oA.html("首頁");
        $td.append(oA);
        //上一頁
        if(this.currentPageNum>=2){
            var oA = $("<a/>");
            oA.attr("href","#"+(this.currentPageNum - 1));
            oA.html("上一頁");
            $td.append(oA);
        }
        //普通顯示格式
        if(this.maxPageNum <= this.groupDataNum){  // 10頁以內(nèi) 為一組
            for(var i = 1;i <= this.maxPageNum ;i++){
                var oA = $("<a/>");
                oA.attr("href","#"+i);
                if(this.currentPageNum == i){
                    oA.attr("class","current");
                }
                oA.html(i);
                $td.append(oA);
            }
        }else{//超過10頁以后(也就是第一組后)
             if(this.groupNum<=1){//第一組顯示
                 for(var j = 1;j <= this.groupDataNum ;j++){
                     var oA = $("<a/>");
                     oA.attr("href","#"+j);
                     if(this.currentPageNum == j){
                         oA.attr("class","current");
                     }
                     oA.html(j);
                     $td.append(oA);

                 }
             }else{//第二組后面的顯示
                 var begin = (this.groupDataNum*(this.groupNum-1))+ 1,
                      end ,
                     maxGroupNum = Math.ceil(this.maxPageNum/this.groupDataNum);
                 if(this.maxPageNum%this.groupDataNum!=0&&this.groupNum==maxGroupNum){
                     end = this.groupDataNum*(this.groupNum-1)+this.maxPageNum%this.groupDataNum
                 }else{
                     end = this.groupDataNum*(this.groupNum);
                 }

                 for(var j = begin;j <= end ;j++){
                     var oA = $("<a/>");
                     oA.attr("href","#"+j);
                     if(this.currentPageNum == j){
                         oA.attr("class","current");
                     }
                     oA.html(j);
                     $td.append(oA);
                 }
             }
        }
        //下一頁
        if( (this.maxPageNum - this.currentPageNum) >= 1 ){
            var oA = $("<a/>");
            oA.attr("href","#" + (this.currentPageNum + 1));
            oA.html("下一頁");
            $td.append(oA);
        }
        //尾頁
        var oA = $("<a/>");
        oA.attr("href","#" + this.maxPageNum);
        oA.html("尾頁");
        $td.append(oA);

        var page_a = $td.find('a');
        var tempThis = this;

        page_a.unbind("click").bind("click",function(){
            var nowNum =  parseInt($(this).attr('href').substring(1));

            if(nowNum>tempThis.currentPageNum){//下一組
                if(tempThis.currentPageNum%tempThis.groupDataNum==0){
                    tempThis.groupNum += 1;

                    var maxGroupNum = Math.ceil(tempThis.maxPageNum/tempThis.groupDataNum);
                    if(tempThis.groupNum>=maxGroupNum){
                        tempThis.groupNum = maxGroupNum;
                    }
                }
            }
            if(nowNum<tempThis.currentPageNum){//上一組
                if((tempThis.currentPageNum-1)%tempThis.groupDataNum==0){
                    tempThis.groupNum -= 1;
                    if(tempThis.groupNum<=1){
                        tempThis.groupNum = 1;
                    }
                }
            }
            if(nowNum==tempThis.maxPageNum){//直接點擊尾頁
                var maxGroupNum = Math.ceil(tempThis.maxPageNum/tempThis.groupDataNum);
                tempThis.groupNum = maxGroupNum;
            }
            if(nowNum==1){
                var maxGroupNum = Math.ceil(tempThis.maxPageNum/tempThis.groupDataNum);
                tempThis.groupNum = 1;
            }
            tempThis.currentPageNum = nowNum;


            tempThis.init((tempThis.currentPageNum-1)*tempThis.displayNum,
                tempThis.currentPageNum*tempThis.displayNum);
            return false;
        });
    }
    //初始化
    this.init = function(begin,end){
        this.header = this._options.headers;
        this.colNum = this.header.length;
        this.content = this._options.data;
        this.allDataNum = this.content.length;
        if(this._options.displayNum){
            this.displayNum = this._options.displayNum;
        }
        if(this._options.groupDataNum){
            this.groupDataNum = this._options.groupDataNum;
        }
        this.clearTable();
        this.showHeader();
        this.showContent(begin,end);
        this.showFoot();
    }
    this.refresh=function(){
        this.init(0,this._options.displayNum);
    }
    this.setOption=function(option){
        this._options=option;
    }
    this.setWidth=function(w){
        $('#'+this._tableId+'_box').width(w);
    }
    this.setHeight=function(h){
        $('#'+this._tableId+'_box').height(h);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,271評論 5 13
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,697評論 6 30
  • 一:canvas簡介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標簽 ②:HTML5 ...
    GreenHand1閱讀 4,882評論 2 32
  • 你生病了,然后要去醫(yī)院,想著前幾天才因為家里有事請過假,繼續(xù)請假不大好意思,所以早點去醫(yī)院好了,說不定還趕得及九...
    半桶閱讀 239評論 0 1
  • 很奇怪的,在每一段時光里我都有一個可以說很多無節(jié)操話題的女閨蜜。其實把她們單獨寫一回就足以來上一個專題,今天我主要...
    劉生輝xavier閱讀 581評論 0 1

友情鏈接更多精彩內(nèi)容