jQuery插件开发-table插件开发

使用过bootstarp-table插件的人都知道bootstrap给我们提供了一个非常好看的表格渲染插件,我基于此,也模拟开发了一个比较简单的表格渲染插件。

html代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>组件</title>
    <link rel="stylesheet" type="text/css" href="future.table.css"/>
    <script src="./jquery-1.11.0.min.js"></script>
    <script src="future.table.data.js"></script>
    <script src="test.js"></script>
    <style>
        .one{color:red;}
        .two{color:green;}
        .live{color:blue;}
    </style>
</head>
<body class="">
jQuery插件学习
<div id="demo"></div>
</body>
</html>

test.js(插件调用)


//插件调用
$(function(){
    var data = [
        {col1:1111,col2:2222,col3:3333,col4:4444,col5:5555},
        {col1:1111,col2:2222,col3:3333,col4:4444,col5:5555},
        {col1:1111,col2:2222,col3:3333,col4:4444,col5:5555},
        {col1:1111,col2:2222,col3:3333,col4:4444,col5:5555},
        {col1:1111,col2:2222,col3:3333,col4:4444,col5:5555},
        {col1:1111,col2:2222,col3:3333,col4:4444,col5:5555}
    ];
    $("#demo").ftTData({
    data:data,
    cols:[{th:"col1",thName:"col1列名",fColor:"red",fWeight:"bolder"},
          {th:"col2"},
          {th:"col3"},
          {th:"col4"},
          {th:"col5"}]
    });
});

future.table.data.js(渲染数据插件)

(function($){
    $.extend(
        {ftSay:function(option){
            alert(option)}
    });

    $.fn.extend({
        ftDivClear:function(){
            this.html("11111");
            //$(this).html("");
        }
    });

    $.fn.extend({
        ftTData:function(options){
            //要被渲染的对象,this指向调用该函数的dom对象
            $(this).html("");

            var tableCols = options.cols;
            var tableData = options.data;

            var _html = "<table class='fttable_body'>";
            $.each(tableData,function(dataIndex,data){
                _html += "<tr>";
                $.each(tableCols,function(colIndex,col){

                    _html += "<td style = '";
                    if(col.fColor != undefined){
                        _html += "color:" + col.fColor + ";";
                    }
                    if(col.fWeight != undefined){
                        _html += "font-weight:" + col.fWeight + ";";
                    }

                    _html += "'>";
                    _html += data[col.th];
                    _html += "</td>";
                });
                _html += "</tr>";
            });

            _html += "</table>";
            $(this).html(_html);
        }
    });
})(jQuery);

到此为止,运行,结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独行侠_阿涛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值