table 列排序&行变色&按上下键光标在行上移动&上下移动行并保存DB

 /*单击TR变色 */
    var preTR = null;//全局变量
    function TRbackgroundClick(ctrl) {
        if (preTR != null) {
            var preCells = preTR.getElementsByTagName("td");
            for (var i = 0; i < preCells.length; i++) {
                preCells[i].style.backgroundColor = "";
            }
        }
        var cells = ctrl.getElementsByTagName("td");
         for (var i = 0; i < cells.length; i++) {
            cells[i].setAttribute("style", "background-color:#ecfbd4;");
          }
        preTR = ctrl;      
    }

///table 列排序
    function getDate(strDate) {
        strDate = strDate.replace(/-/g, "/");
        var date = new Date(strDate);
        return date;
    }

    $(function () {

        $('table.myTable').each(function () {
            var $table = $(this);                       //将table存储为一个jquery对象

  $("tbody tr td", $table).click(function () {//单击单元格行变色                 
               
                $('td').not(this).removeClass("active");
                $(this).addClass("active").siblings("td").addClass("active");
            })

            $('th', $table).each(function (column) {
                var findSortKey;
                if ($(this).is('.sort-alpha')) {       //按字母排序
                    findSortKey = function ($cell) {
                        return $cell.find('sort-key').text().toUpperCase() + '' + $cell.text().toUpperCase();
                    };
                } else if ($(this).is('.sort-numeric')) {       //按数字排序
                    findSortKey = function ($cell) {
                        var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''));
                        return isNaN(key) ? 0 : key;
                    };
                } else if ($(this).is('.sort-date')) {          //按日期排序
                    findSortKey = function ($cell) {
                        return getDate($cell.text());                     
                    };
                }

                if (findSortKey) {
                    $(this).addClass('clickable').hover(function () { $(this).addClass('hover'); }, function () { $(this).removeClass('hover'); }).click(function () {
                        //反向排序状态声明
                        var newDirection = 1;
                        if ($(this).is('.sorted-asc')) {
                            newDirection = -1;
                        }
                        var rows = $table.find('tbody>tr').get(); //将数据行转换为数组
                        $.each(rows, function (index, row) {
                            row.sortKey = findSortKey($(row).children('td').eq(column));
                        });
                        rows.sort(function (a, b) {
                            if (a.sortKey < b.sortKey) return -newDirection;
                            if (a.sortKey > b.sortKey) return newDirection;
                            return 0;
                        });
                        $.each(rows, function (index, row) {
                            $table.children('tbody').append(row);
                            row.sortKey = null;
                        });

                        $table.find('th').removeClass('sorted-asc').removeClass('sorted-desc');
                        var $sortHead = $table.find('th').filter(':nth-child(' + (column + 1) + ')');
                        //实现反向排序
                        if (newDirection == 1) {
                            $sortHead.addClass('sorted-asc');
                        } else {
                            $sortHead.addClass('sorted-desc');
                        }

                        //移除已排序的列的样式,添加样式到当前列
                        $table.find('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
                        $table.trigger('repaginate');
                    });
                }
            });
        });
    });
   
</script>
 <style type="text/css">
 .clickable
        {
            text-decoration: underline;
        }
  .hover
         {
            background-color: #5dd354;
        }
        .sorted
        {
            background-color: #FFF8DC;
        }     
        .active
        {         
            background-color:#ecfbd4;
            } 

</style>

///按上下键光标在行上移动
//前台
<script type="text/javascript" language="javascript">
        var currentRowId = 0;
        function SelectRow(ev, strGvName) {
            var e = window.event || ev;
            var keyCode = -1;
            if (e.which == null)
                keyCode = e.keyCode;    // IE  
            else
                if (e.which > 0)
                    keyCode = e.which;    // All others 
            if (keyCode == 40)
                MarkRow(currentRowId + 1, strGvName);
            if (keyCode == 38)
                MarkRow(currentRowId - 1, strGvName); 1
        }
        function MarkRow(rowId, strGvName) {
            if (document.getElementById(strGvName + rowId) == null)
                return;
            if (document.getElementById(strGvName + currentRowId) != null)
                document.getElementById(strGvName + currentRowId).style.backgroundColor = '#ffffff';
            currentRowId = rowId;
            document.getElementById(strGvName + rowId).style.backgroundColor = '#E6F5FA';
            var obj = document.getElementById(strGvName);
            obj.rows[rowId].cells[0].focus();
        }   
    </script>
//后台cs
  private int _i=0;
        protected void grid_RowBound(object sender, GridViewRowEventArgs e) 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    {
        string strGvName = "_ctl0_cph_body_GridView1"; 
        e.Row.Attributes.Add("id", strGvName + _i.ToString()); 
        e.Row.Attributes.Add("onKeyDown", "SelectRow(event,'" + strGvName + "');");
        e.Row.Attributes.Add("onClick", "MarkRow(" + _i.ToString() + ",'" + strGvName + "');"); 
        e.Row.Attributes.Add("tabindex", "0"); 
        _i++; 
    } 
 
 <asp:GridView runat="server" ID="GridView1" OnRowDataBound="grid_RowBound">
                            </asp:GridView>
 ///上下移动行并保存DB
<head>
    <title>表格数据上下行互换位置</title>
    <script src="jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
        //上移
            $("input.upbtn").each(function () {
                $(this).click(function () {
                    var $tr = $(this).parents("tr");
                    if ($tr.index() != 0) {
                        $tr.prev().before($tr);
                    }
                var txt = $("#flowSorted tr td:first-child").text();
                    txt=txt.replace(/[ ]/g, ","); //替换空格
                    // var $tab = $tr.parent().parent().attr("id");//get table's id
                    var json_data = { "UserConfig": txt};
                    $.ajax({
                        type: "get",
                        url: "/Main/HandlerMain.ashx",
                        data: json_data,
                        beforeSend: function (XMLHttpRequest, fk_mapExt) {
                          
                        },
                        success: function (data, textStatus) {
                        
                        },
                        complete: function (XMLHttpRequest, textStatus) {
                        
                        },
                        error: function () {
                            alert('error when load data.');
                            //请求出错处理
                        }
                    });
                    //ajax end
                });
            });
            //下移
            var trLength = $("input.downbtn").length;          
            $("input.downbtn").each(function () {
                $(this).click(function () {
                    var $tr = $(this).parents("tr");                 
                    if ($tr.index() != trLength - 1) {                     
                        $tr.next().after($tr);
                    }
                });
            });
        });
   
    </script>
</head>
<body>
<table border="1" cellpadding=0 cellspacing=0>
    <tr>
        <td>4</td>
        <td>xxx44xxxx</td>
        <td>2013-5-24</td>
        <td><input type="button" value="上移" class="upbtn"  /><input type="button" value="下移" class="downbtn" /></td>
    </tr>
    <tr>
        <td>5</td>
        <td>xxxx55xxx</td>
        <td>2013-5-25</td>
        <td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
    <tr>
        <td>6</td>
        <td>xxxx66xxx</td>
        <td>2013-5-26</td>
        <td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
    </tr>
    <tr>
        <td>7</td>
        <td>xxxx77xxx</td>
        <td>2013-5-27</td>
        <td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
    </tr>
    <tr>
        <td>8</td>
        <td>xxx88xxxx</td>
        <td>2013-5-28</td>
        <td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
    </tr>
</table>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值