2016-06-21 84 views
-1

我正在學習如何使用webgrid和jquery構建一個父子關係。 這裏是的WebGrid獲取未被捕獲的TypeError與較新版本的jquery

  <div id="main" style="padding:25px; background-color:white;"> 
      @grid.GetHtml(
      htmlAttributes: new {id="gridT", width="700px" }, 
      columns:grid.Columns(
        grid.Column("order.OrderID","Order ID"), 
        grid.Column(header:"Order Date",format:(item)=> string.Format("{0:dd-MM-yyyy}",item.order.OrderDate)), 
        grid.Column("order.CustomerName","Customer Name"), 
        grid.Column("order.CustomerAddress","Address"), 

        grid.Column(format:(item)=>{ 
         WebGrid subGrid = new WebGrid(source: item.orderDetails); 
         return subGrid.GetHtml(
          htmlAttributes: new { id="subT" }, 
          columns:subGrid.Columns(
            subGrid.Column("Product","Product"), 
            subGrid.Column("Quantity", "Quantity"), 
            subGrid.Column("Rate", "Rate"), 
            subGrid.Column("Amount", "Amount") 
           )      
          ); 
        }) 
       ) 
      ) 
     </div> 

這裏是jQuery的

 @section Scripts{ 
      <script> 
       $(document).ready(function() { 
        var size = $("#main #gridT > thead > tr >th").size(); // get total column 
        $("#main #gridT > thead > tr >th").last().remove(); // remove last column 
        $("#main #gridT > thead > tr").prepend("<th></th>"); // add one column at first for collapsible column 
        $("#main #gridT > tbody > tr").each(function (i, el) { 
         $(this).prepend(
           $("<td></td>") 
           .addClass("expand") 
           .addClass("hoverEff") 
           .attr('title',"click for show/hide") 
          ); 

         //Now get sub table from last column and add this to the next new added row 
         var table = $("table", this).parent().html(); 
         //add new row with this subtable 
         $(this).after("<tr><td></td><td style='padding:5px; margin:0px;' colspan='" + (size - 1) + "'>" + table + "</td></tr>"); 
         $("table", this).parent().remove(); 
         // ADD CLICK EVENT FOR MAKE COLLAPSIBLE 
         $(".hoverEff", this).live("click", function() { 
          $(this).parent().closest("tr").next().slideToggle(100); 
          $(this).toggleClass("expand collapse"); 
         }); 
        }); 

        //by default make all subgrid in collapse mode 
        $("#main #gridT > tbody > tr td.expand").each(function (i, el) { 
         $(this).toggleClass("expand collapse"); 
         $(this).parent().closest("tr").next().slideToggle(100); 
        }); 

       }); 
      </script> 
     } 

這部分不能正常工作和運行與 「.live」(遺漏的類型錯誤$(...)住的錯誤是不是一個功能)。我在網上讀到,這已從jquery的新版本刪除 。

 $(".hoverEff", this).live("click", function() { 
          $(this).parent().closest("tr").next().slideToggle(100); 
          $(this).toggleClass("expand collapse"); 
         }); 

我試着改變它爲「.on」,但仍然沒有運氣。我正在使用jQuery-1.10.2。我該如何做這項工作?

回答

0

livedelegate被棄用前,隨後取出。新方法是使用on

+0

如何在此代碼中應用on?對我不起作用。 $(「。hoverEff」,this).on(「click」,function(){我剛學習這個。 – user2320476

+0

@ user2320476:'on'(和'live'和'delegate')的文檔顯示清晰的例子如何使用'on'和'委託'如果你正在學習,首先要學習的是*文檔是你的朋友*。:-)'$(document).on (「click」,「.hoverEff」,function(){/*...*/});' –

+0

我試過這個,不起作用。 $(document).on(「click」,「.hoverEff」,function(){ $(this).parent()。closest(「tr」)。next()。slideToggle(100); $(this ).toggleClass(「expand collapse」); }); – user2320476

相關問題