2010-09-29 52 views
1

在MVC中,我做了一个jQuery模态,询问我是否确定要删除它,然后用$.post进行回发我在所有'索引视图'中都使用它,它相当多。MVC Deletemodal使用jQuery的额外选项

对于大多数的意见,当我删除的东西,从表中的行还与hide()

现在,对于一些意见,事情并没有真正被删除删除,但他们得到“无效”。因此,在这种情况下,我不想从表中删除他们,我宁愿只是给他们的类“rowinactive”

所以这就是我的了:

的Index.aspx

<script type="text/javascript"> 
    $(document).ready(function() { 

     $(".delbtn").click(function() { 
      var msg = "deelnemer \"" + $(this).parent().parent().find(".dlnVoornaam").text() + " "+$(this).parent().parent().find(".dlnNaam").text()+"\" "; 
      deleteConfirmation(msg, this, true); 
      return false; 
     }); 
    }); 
</script> 

的HTML表

  <th> 
      </th> 
      <th> 
       Aanspreking 
      </th> 
      <th> 
       Naam 
      </th> 
      <th> 
       Voornaam 
      </th> 

      <th> 
       Functie 
      </th> 
      <th> 
       Creatiedatum 
      </th> 
      <th> 
       Annulatiedatum 
      </th> 
      <th> 

       Update 
      </th> 
      <th> 
       Email 
      </th> 
      <th> 
       Username 
      </th> 
     </tr> 

     <tr class="rowinactive"> 

      <td> 

      </td> 
      <td> 
       De heer 
      </td> 
      <td class="dlnNaam"> 
       Van der Straeten                                                            
      </td> 
      <td class="dlnVoornaam"> 
       Stefan                                                               
      </td> 

      <td> 
       Webdev                        
      </td> 
      <td> 
       29/09/2010 
      </td> 
      <td> 
       29/09/2010 
      </td> 
      <td> 


      </td> 
      <td> 
       xxx                                                          
      </td> 
      <td> 
       Stefan.Van der Stra 
      </td> 
     </tr> 

     <tr class=""> 
      <td> 


<a href='/2011-2012/Deelnemer/Edit/5?instID=3562'> 
    <img src="/img/pencil.png" alt="Edit" width="16" /></a> 

<a href='/2011-2012/Deelnemer/Details/5?instID=3562'> 
    <img src="/img/application_view_detail.png" alt="Details" 
     width="16" /></a> 

<a class="delbtn" href='/2011-2012/Deelnemer/Delete/5?instID=3562'> 
    <img src="/img/cancel.png" alt="Delete" width="16" /></a> 

      </td> 
      <td> 

       De heer 
      </td> 
      <td class="dlnNaam"> 
       Van der Straeten                                                            
      </td> 
      <td class="dlnVoornaam"> 
       Stefan                                                               
      </td> 
      <td> 
       Webdev                        
      </td> 

      <td> 
       29/09/2010 
      </td> 
      <td> 

      </td> 
      <td> 

      </td> 
      <td> 
       xxx                                                          
      </td> 

      <td> 
       Stefan.VanderStraet 
      </td> 
     </tr> 

     <tr class="rowinactive"> 
      <td> 

      </td> 
      <td> 
       De heer 
      </td> 

      <td class="dlnNaam"> 
       Van der Straeten                                                            
      </td> 
      <td class="dlnVoornaam"> 
       Stefan                                                               
      </td> 
      <td> 
       Webdev                        
      </td> 
      <td> 

       29/09/2010 
      </td> 
      <td> 
       29/09/2010 
      </td> 
      <td> 

      </td> 
      <td> 
       xxx                                                          
      </td> 

      <td> 
       Stefan.VanderStraet1 
      </td> 
     </tr> 

     <tr class=""> 
      <td> 

<a href='/2011-2012/Deelnemer/Edit/7?instID=3562'> 
    <img src="/img/pencil.png" alt="Edit" width="16" /></a> 

<a href='/2011-2012/Deelnemer/Details/7?instID=3562'> 

    <img src="/img/application_view_detail.png" alt="Details" 
     width="16" /></a> 

<a class="delbtn" href='/2011-2012/Deelnemer/Delete/7?instID=3562'> 
    <img src="/img/cancel.png" alt="Delete" width="16" /></a> 

      </td> 
      <td> 
       De heer 
      </td> 
      <td class="dlnNaam"> 
       Van der Straeten                                                            
      </td> 

      <td class="dlnVoornaam"> 
       Stefan                                                               
      </td> 
      <td> 
       Webdev                        
      </td> 
      <td> 
       29/09/2010 
      </td> 
      <td> 


      </td> 
      <td> 

      </td> 
      <td> 
       xxx                                                          
      </td> 
      <td> 
       Stefan.VanderStraet2 
      </td> 
     </tr> 


    </table> 

外部JS文件

function deleteConfirmation(msg, handler) { 
    deleteConfirmation(msg, handler, false); 
} 

function deleteConfirmation(msg, handler, inactive) { 
    var showIt = function (hash) { 
     hash.w.find("#modaltekst").text(msg); 
     $("#btnJa").data('handler', $(handler)); 
     $("#btnJa").data('inactive', inactive); 
     $("#delmodal").show(); 
     return false; 
    }; 

    $("#delmodal").jqm({ onShow: showIt }).jqmShow(); 

} 

$(document).ready(function() { 
    $("#btnJa").click(function() { 
     $("#delmodal").jqmHide(); 
     setHighlight("Verwijderen..."); 
     $.post($(this).data('handler').attr('href'), null, function (data) { 
      if (data.succes) { 
       setHighlightOK("Verwijderd"); 
       if ($("#btnJa").data('inactive')==false) { 
        $("#btnJa").data('handler').parent().parent().fadeOut("slow"); 
       } else { 
        $("#btnJa").data('handler').parent().parent().addClass("rowinactive"); 
       } 
      } else { 
       if (data.error != "") { 
        setError(data.error); 
       } else { 
        setError("Verwijderen mislukt."); 
       } 
      } 
     }, "json"); 
    }); 

    $("#btnNee").click(function() { 
     $("#delmodal").jqmHide(); 
    }); 
}); 

因此,为了使有可能使该行不活动的,而不是隐藏它,我加了一个booleanvar inactive

现在,我不想添加inactive var,我想在我的Index.aspx中调用deleteConfirmation发送function。该函数将包含需要在回发的成功执行的代码。我想要这个,因为我想隐藏不活动行的按钮,尽管我可能可以通过live()来完成,但这对我来说看起来并不干净。

附加问题是:您将如何优化此代码?从别人

回答

0

我从我们的代码假设,就是你想两件事情

1区分非活动行 2,禁用删除和编辑链接

U可以做到这一点通过编写一些CSS和jQuery。 ü可以写为不活动的行

.inactive 
{ 
    background-color:gray; 

} 

$("tr.inactive a").live("click",function(){ 
    return false; 
}); 

和当u使用Ajax调用灭活乌尔行U可以只添加类“不活跃”,以相应的TR和关闭相应的删除按钮,像

$("tr.inactive .delBtn").attr("disabled","disabled"); 
+0

我知道如何风格写CSS,我知道我可以用live函数来做,但是,这不是我想要的。我明确表示我不想使用现场。我想有可能在我的index.aspx中编写一个函数,当回传到来时执行它... – Stefanvds 2010-09-29 17:43:07