使用os3grid操作Html 表格

       os3grid 开源,目前version 为0.6,功能和界面都还可以,扩展也较方便,平台通用性很强。主要使用javascript脚本语言操纵Grid。

 

Demo实例:

<html>
<head>
    <link type="text/css" rel="stylesheet" href="os3grid.css" />
    <script src="os3grid.js" type="text/javascript"></script>
    <script src="form_validators.js" type="text/javascript"></script>
</head>
<body >
    我的os3grid测试<br>
    <div id="grid"></div>
   
<script type="text/javascript">
               function val_changed ( grid, x, y, new_val )
    {
     alert ( "On grid: " + grid.id + " x: " + x + " y: " + y + " - value changed to: " + new_val );
    }
    
    //txt: ”Plain” cell content to be manipulated before output.
    function bold_render( txt )
    {
     return '<span style="font-weight: bold; color: red;">' + txt + '<//span>';
    }
    
     // Create an OS3Grid instance
    var g = new OS3Grid ();

    // Grid Headers are the grid column names
    g.set_headers ( 'feat.no', 'Name', 'Descr', 'Importance' );

    // If contents is bigger than container, Grid will automatically show scrollbars
    g.set_scrollbars ( true );

    // The grid will have a solid border (these are CSS attributes)
    g.set_border ( 1, "solid", "blue" );

    // Now, we add some rows to the grid
    g.add_row ( 1, 'Layout', 'OS3Grid looks like a standard spreadsheet grid', 10 );
    g.add_row ( 2, 'Row Colors', 'You can change the colors of every singular row in the grid', 5 );
    g.add_row ( 3, 'Sorting', 'Rows can be sorted ascending or descending by clicking on column names', 15 );

    // Enable sortable rows
    g.set_sortable ( true );

    // Enable highlight of rows with the mouse
    g.set_highlight ( true );
    
    //第1列居中,第4列居右
    g.set_col_align ( 0, "center" );
    g.set_col_align ( 3, "right" );
    
    g.set_col_editable ( 1, "txt" );
    g.set_col_editable ( 2, "txt" );
    g.set_col_editable ( 3, "txt" );
    
    /*
    OS3Grid cell data can be manipulated before being output to the browser. To do
    so, you simply have to define a column renderer, a JavaScript funtion that will
    be called by the OS3Grid itself each time it needs to render some data inside a
    given cell of a specific column.
    */
    g.set_col_render ( 1, bold_render );    //第1列,bold_render为js函数
    
    //on_change callback
    g.onchange = val_changed;
    
    //chars: A string containing all the chars that the user can input inside the column edit box.
    g.set_col_valid_chars ( 3, "0123456789" );   //第4列只能输入0123456789几个字符的子集
    
    g.set_col_validation ( 3, check_integer );   //判断第4列的输入是否合法,check_integer为js函数
    
    g.resize_cols = true;

    g.sort_on_edit = true;
    
    // Show the grid replacing the original HTML object with the "grid" ID.
    g.render ( 'grid' );
    
    
    //取得表格中的数据,这样我们可以将表格中的数据序列化或写入持久化(xml or 数据库)
     var i, data;

                 for ( i= 0; i < g.length (); i ++ )
                 {
                     data = g.get_row (i );
                     alert(data[0]);
                  }
</script>
</body>
</html>

下载: http://os3grid.sourceforge.net,将几个css和js文件拷到对应目录下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值