테이블 플러그인 포함 (hover 정렬 클릭) 효과

6314 단어 정렬테이블table
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>     </title>
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<style type="text/css">
.tr-hover { background-color: #CCC; }
.tr-selected { background-color: #666; }
.tr-border { border: solid 1px #F00; }
</style>
</head>
<body>
<table width="500" border="1" cellpadding="0" cellspacing="0" id="table">
    <thead>
        <tr>
            <th>  </th>
            <th>  </th>
            <th>  </th>
            <th>  </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Lupo</td>
            <td>
                <select name="">
                  <option value="1"> </option>
                  <option value="0"> </option>
                </select>
            </td>
            <td><input type="text" /></td>
        </tr>
        <tr>
            <td>2</td>
            <td>andy</td>
            <td>
                <select name="">
                  <option value="1"> </option>
                  <option value="0"> </option>
                </select>
            </td>
            <td><input type="text" /></td>
        </tr>
        <tr>
            <td>3</td>
            <td>lilian</td>
            <td>
                <select name="">
                  <option value="1"> </option>
                  <option value="0"> </option>
                </select>
            </td>
            <td><input type="text" /></td>
        </tr>
        <tr>
            <td>4</td>
            <td>jinker</td>
            <td>
                <select name="">
                  <option value="1"> </option>
                  <option value="0"> </option>
                </select>
            </td>
            <td><input type="text" /></td>
        </tr>
    </tbody>
</table>
<script type="text/javascript">
/**
 *          hover            
 *@auther    
 *@time 2013-01-21
 *@encode UTF-8
 *@param {boolen} hover         hover   true|false
 *@param {boolen} selected        selected   true|false
 *@param {boolen} rowMove               true|false
 *@example:
    $("#table").initTable({
        hover:true,
        selected:false,
        rowDrag:true,
        rowMove:true
    })
         thead,tbody2  
    
 */

(function ($) {
    $.fn.extend({
        initTable:function (o) {
            //            
            var it = this,
                hover = o.hover||false,
                selected = o.selected||false,
                rowDrag = o.rowDrag||false;
                rowMove =rowDrag?false:(o.rowMove||false),
                tbody = $(it).children("tbody"),
                tr = $(it).children("tbody").children("tr");
            //               
            it.undelegate();
            //tr       
            if (hover) {
                tbody.delegate("tr", "mouseenter", function () {
                    $(this).addClass("tr-hover");
                }).delegate("tr", "mouseleave", function () {
                    $(this).removeClass("tr-hover");
                })
            }
            //tr     
            if (selected) {
                tbody.delegate("tr","click",function(e){
                    if(e.target.tagName.toLowerCase()=="td"){
                        tbody.children(".tr-selected").removeClass('tr-selected');
                        $(this).addClass('tr-selected');
                    }
                })
            }
            //        
            if(rowMove){
                var targetEl,mouseDown=false;
                tbody.delegate("tr", "mousedown", function(e){
                    //  td    
                    if(e.target.tagName.toLowerCase() === "td"){
                        //        
                        targetEl = this,mouseDown = true; console.log(this);
                        $(this).css("cursor","move");
                        return false;
                    }
                }).delegate("tr", "mousemove", function(e){
                      //    
                      if (mouseDown) {
                          //          
                          if (targetEl != this) {
                              if ($(this).index()>$(targetEl).index()){
                                  $($(this)).after(targetEl);
                              } else {
                                  $($(this)).before(targetEl);
                              }
                          }
                      }
                      return false;
                }).delegate("tr", "mouseup", function (e) {
                    $(tr).css("cursor","default");
                    targetEl = null;
                })
                //       ,      
                it.delegate("tbody", "mouseleave", function (e) {
                    $(tr).css("cursor","default");
                    targetEl = null;
                    mouseDown = false;
                })
            }
        }
    })
})(jQuery)

$("#table").initTable({
    hover:true,
    selected:true,
    rowMove:true
})
</script>
</body>
</html>

좋은 웹페이지 즐겨찾기