js超簡(jiǎn)潔表格


完整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    //必須   默認(rèn) 10
});
cs.init(0,9);

對(duì)象方法


init(begin,end)

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

refresh()

刷新表格

setOption(option)

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

setWidth(width)

設(shè)置寬度

setHeight(height)

設(shè)置高度

源碼


/**
 * 表格對(duì)象
 * @param options
 */
function AJRD_table(tableId,options){
    this._tableId=tableId;
    this._options=options;
    /**
     * 抽象化表格
     */
    function abstractTable(){
        // ---------內(nèi)容屬性
        this.id = null;         // 每個(gè)表格都有唯一的一個(gè)id
        this.tableobj = null;  //表格對(duì)象
        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)前點(diǎn)擊的行數(shù)據(jù)
        // --- 通過(guò)表頭來(lái)獲得這張表的列數(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(){};
    
        // --------- 分頁(yè)功能屬性
        this.allDataNum = 0;  // 總數(shù)據(jù)條數(shù)
        this.displayNum = 10; // 每頁(yè)顯示條數(shù)
        this.maxPageNum = 0;  // 最大頁(yè)碼值
        this.currentPageNum =1;// 當(dāng)前頁(yè)碼值
        //tfoot分頁(yè)組
        this.groupDataNum = 10;  //每組顯示10頁(yè)
        this.groupNum = 1;       //當(dāng)前組
    
        // -------- 分頁(yè)功能行為
        this.paginationFromBeginToEnd = function(begin,end){}
        this.first =  function(){}//首頁(yè)
        this.last = function(){}//最后一頁(yè)
        this.prev = function(){}//上一頁(yè)
        this.next = function(){}//下一頁(yè)
        this.goto = function(){} //跳到某頁(yè)
    
        // ----------- 表格初始化
        this.init = function(begin,end){}
    }

    /*
     表格對(duì)象模板
     */
    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);
    //得到表格對(duì)象
    this.tableobj = $("#"+this._tableId);
    //清空表格內(nèi)容
    this.clearTable = function(){
        this.tableobj.html(" ");
    }
    // 實(shí)現(xiàn)分頁(yè)行為
    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)建列  取得對(duì)象中的鍵
                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);
    }
    //表格分頁(yè)
    this.pagination = function(tdCell){
        var $td= typeof(tdCell) == "object" ? tdCell : $("#" + tdCell);
        //首頁(yè)
        var oA = $("<a/>");
        oA.attr("href","#1");
        oA.html("首頁(yè)");
        $td.append(oA);
        //上一頁(yè)
        if(this.currentPageNum>=2){
            var oA = $("<a/>");
            oA.attr("href","#"+(this.currentPageNum - 1));
            oA.html("上一頁(yè)");
            $td.append(oA);
        }
        //普通顯示格式
        if(this.maxPageNum <= this.groupDataNum){  // 10頁(yè)以內(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{//超過(guò)10頁(yè)以后(也就是第一組后)
             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);
                 }
             }
        }
        //下一頁(yè)
        if( (this.maxPageNum - this.currentPageNum) >= 1 ){
            var oA = $("<a/>");
            oA.attr("href","#" + (this.currentPageNum + 1));
            oA.html("下一頁(yè)");
            $td.append(oA);
        }
        //尾頁(yè)
        var oA = $("<a/>");
        oA.attr("href","#" + this.maxPageNum);
        oA.html("尾頁(yè)");
        $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){//直接點(diǎn)擊尾頁(yè)
                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)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末露懒,一起剝皮案震驚了整個(gè)濱河市甸箱,隨后出現(xiàn)的幾起案子效拭,更是在濱河造成了極大的恐慌按灶,老刑警劉巖婴谱,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件佩耳,死亡現(xiàn)場(chǎng)離奇詭異润绎,居然都是意外死亡匙铡,警方通過(guò)查閱死者的電腦和手機(jī)拧咳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門伯顶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人骆膝,你說(shuō)我怎么就攤上這事祭衩。” “怎么了阅签?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵掐暮,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我政钟,道長(zhǎng)路克,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任养交,我火速辦了婚禮精算,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘碎连。我一直安慰自己灰羽,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著廉嚼,像睡著了一般玫镐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上怠噪,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天摘悴,我揣著相機(jī)與錄音,去河邊找鬼舰绘。 笑死蹂喻,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的捂寿。 我是一名探鬼主播口四,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼秦陋!你這毒婦竟也來(lái)了蔓彩?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤驳概,失蹤者是張志新(化名)和其女友劉穎赤嚼,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顺又,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡更卒,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了稚照。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蹂空。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖果录,靈堂內(nèi)的尸體忽然破棺而出上枕,到底是詐尸還是另有隱情,我是刑警寧澤弱恒,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布辨萍,位于F島的核電站,受9級(jí)特大地震影響返弹,放射性物質(zhì)發(fā)生泄漏锈玉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一琉苇、第九天 我趴在偏房一處隱蔽的房頂上張望嘲玫。 院中可真熱鬧,春花似錦并扇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)土陪。三九已至,卻和暖如春肴熏,著一層夾襖步出監(jiān)牢的瞬間鬼雀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工蛙吏, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留源哩,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓鸦做,卻偏偏與公主長(zhǎng)得像励烦,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子泼诱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果坛掠,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌治筒。在這里你可以看...
    F麥子閱讀 5,113評(píng)論 5 13
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果屉栓,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌耸袜。在這里你可以看...
    每天刷兩次牙閱讀 8,495評(píng)論 6 30
  • 一:canvas簡(jiǎn)介 1.1什么是canvas友多? ①:canvas是HTML5提供的一種新標(biāo)簽 ②:HTML5 ...
    GreenHand1閱讀 4,685評(píng)論 2 32
  • 你生病了胰锌,然后要去醫(yī)院骗绕,想著前幾天才因?yàn)榧依镉惺抡?qǐng)過(guò)假,繼續(xù)請(qǐng)假不大好意思资昧,所以早點(diǎn)去醫(yī)院好了酬土,說(shuō)不定還趕得及九...
    半桶閱讀 174評(píng)論 0 1
  • 很奇怪的,在每一段時(shí)光里我都有一個(gè)可以說(shuō)很多無(wú)節(jié)操話題的女閨蜜格带。其實(shí)把她們單獨(dú)寫一回就足以來(lái)上一個(gè)專題撤缴,今天我主要...
    劉生輝xavier閱讀 507評(píng)論 0 1