2016-04-11 11 views
0

私はjqueryで新しく、どこかで動かないです。グリッドビューがあり、その中にイメージ列があります。これらの画像のサイズを変更しようとしていますが、実際にはわかりません。このコードを試して、画像がグリッドビューにないときはうまく動作しますが、グリッドビュー内で作業する必要があると言いました。ここでjqueryでグリッドビューの画像サイズを変更する

は私のグリッドです:

<asp:GridView ID="GridViewFuel" AutoGenerateColumns="false" CssClass="mGrid" runat="server" DataSourceID="EntityDataSourceFuels"> 
    <Columns> 
     <asp:TemplateField HeaderText="Sürücü"> 
      <ItemTemplate> 
       <%#GetDriverNameAndSurname(Convert.ToInt16(Eval("DriverId"))) %> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Araç"> 
      <ItemTemplate> 
       <%#GetCarPlate(Convert.ToInt16(Eval("CarId"))) %> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Fiş"> 
      <ItemTemplate> 
       <asp:Image ID="Image1" ImageUrl='<%#Eval("FuelPrice") %>' runat="server" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

そして、ここでは私のjQueryのです:

$(document).ready(function() { 
     $('.mGrid img').each(function() { 
      var maxWidth = 151; // Max width for the image 
      var maxHeight = 151; // Max height for the image 
      var ratio = 0; // Used for aspect ratio 
      var width = $(this).width(); // Current image width 
      var height = $(this).height(); // Current image height 

      // Check if the current width is larger than the max 
      if (width > maxWidth) { 
       ratio = maxWidth/width; // get ratio for scaling image 
       $(this).css("width", maxWidth); // Set new width 
       $(this).css("height", height * ratio); // Scale height based on ratio 
       height = height * ratio; // Reset height to match scaled image 
       width = width * ratio; // Reset width to match scaled image 
      } 

      // Check if current height is larger than max 
      if (height > maxHeight) { 
       ratio = maxHeight/height; // get ratio for scaling image 
       $(this).css("height", maxHeight); // Set new height 
       $(this).css("width", width * ratio); // Scale width based on ratio 
       width = width * ratio; // Reset width to match scaled image 
      } 
     }); 
    }); 

は、どのように私はそれが私のGridViewで作業することができますか?

答えて

0

私は各セルをループしてみます。イメージが存在する場合は、あなたの比率を行います。

$( 'MGRID TR')。各(関数(){

$('td', this).each(function() { 

    //check if image exists 

}); 

は})

+0

私は、あなたがすべき方法を示すことができれば、それは素晴らしいだろうので、私はjqueryの中で新しいです言ったように私はイメージかどうかを確認しますか? –

+0

各セルをループした後、イメージが存在するかどうかを確認する必要があります。私は自分でテストしなかったが、それは次のようなものでなければならない:var img = $(this).find( 'img'); if(img.length>){// imgとの比率を計算} – plottydotty

関連する問題