Bulk Delete for Webgrid using Checkboxhttp://forums.asp.net/t/1771075.aspx/1?Bulk+Delete+for+Webgrid+using+CheckboxSun, 14 Apr 2013 13:47:54 -040017710754839004http://forums.asp.net/p/1771075/4839004.aspx/1?Bulk+Delete+for+Webgrid+using+CheckboxBulk Delete for Webgrid using Checkbox <p>How can I perform bulk delete operation on a webgrid? I am missing the part where all the checked row will be requested and dynamically add the id to the sql query. Here is the screenshot:</p> <p><img src="http://www4.picturepush.com/photo/a/7602247/640/7602247.jpg"></p> <p></p> <p>The code I have now:</p> <pre class="prettyprint">var db = Database.Open(&quot;MySQLDB&quot;); var query = &quot;SELECT * FROM sample&quot;; var data = db.Query(query); var grid = new WebGrid(source:data, defaultSort:&quot;date_sent&quot;, rowsPerPage:10, ajaxUpdateContainerId:&quot;grid&quot;); if(IsPost){ @*get the selected message ids to be deleted here..*@ var delFromSample = &quot;DELETE FROM sample WHERE IN (the selected message ids here..)&quot;; db.Execute(delFromSample, Request[&quot;messageId&quot;]); } } &lt;div&gt; &lt;form method=&quot;post&quot; action=&quot;&quot;&gt; &lt;input type=&quot;submit&quot; value=&quot;Delete&quot;/&gt; &lt;/form&gt; &lt;/div&gt; &lt;div&gt; @grid.GetHtml( tableStyle:&quot;grid&quot;, headerStyle:&quot;head&quot;, alternatingRowStyle:&quot;alt&quot;, columns: grid.Columns( grid.Column(&quot;id&quot;, &quot;Id&quot;, style:&quot;tdId&quot;), grid.Column(&quot;subject&quot;, &quot;Subject&quot;, style: &quot;tdStyle&quot;), grid.Column(&quot;sender&quot;, &quot;From&quot;, style: &quot;tdStyle&quot;), grid.Column(&quot;date_sent&quot;, &quot;Date&quot;, style:&quot;tdStyle&quot;), grid.Column(header:&quot;&quot;, format:@&lt;input type=&quot;checkbox&quot; name=&quot;messageId&quot; value=&quot;@item.id&quot;/&gt;) ) ) &lt;/div&gt;</pre> 2012-02-18T18:38:25-05:004839012http://forums.asp.net/p/1771075/4839012.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>First, you need to make sure that the WebGrid is inside the form tag so that the selected checkbox values are submitted. When they are, they will be submitted as one comma-separated string in Request[&quot;messageId&quot;]. Then, you need a helper to manage the IN clause: <a href="http://www.mikesdotnetting.com/Article/156/WebMatrix-Database-Helpers-for-IN-Clauses"> http://www.mikesdotnetting.com/Article/156/WebMatrix-Database-Helpers-for-IN-Clauses</a></p> <p></p> 2012-02-18T18:47:49-05:004839335http://forums.asp.net/p/1771075/4839335.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>Thank you for that Mike. It works fine. I just have another question to ask. I can't correctly place a checkbox as a header to the webgrid. The header checkbox will be the checkall thing. I already have js that handles the checking all checkboxes. I just can't place the checkbox as the header for that column. Thanks!</p> 2012-02-19T04:17:37-05:004839446http://forums.asp.net/p/1771075/4839446.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>There's no way to do that using Razor/C#. You have to use JavaScript/jQuery to add one. Since you are already using jQuery in your page here's how to do it:</p> <pre class="prettyprint">&lt;script&gt; $(function(){ var $chk = $('&lt;input/&gt;').attr({ type: 'checkbox', name:'chkAll'}); $('th:last').html('Select All &lt;br /&gt;').append($chk); }); &lt;/script&gt;</pre> 2012-02-19T07:18:00-05:004839518http://forums.asp.net/p/1771075/4839518.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>I added your script in my page, but the check all jquery plugin doesn't work anymore. Here is the code before adding your script:</p> <pre class="prettyprint">&lt;script type=&quot;text/javascript&quot;&gt; $(function() { var propFn = typeof $.fn.prop === 'function' ? 'prop' : 'attr'; $('#checkall').click(function() { $(this).parents('form:eq(0)').find(':checkbox')[propFn]('checked', this.checked); }); $(&quot;input[type=checkbox]:not(#checkall)&quot;).click(function() { if (!this.checked) { $(&quot;#checkall&quot;)[propFn]('checked', this.checked); } else { $(&quot;#checkall&quot;)[propFn]('checked', !$(&quot;input[type=checkbox]:not(#checkall)&quot;).filter(':not(:checked)').length); } }); }); &lt;/script&gt; &lt;div&gt; &lt;form method=&quot;post&quot; action=&quot;&quot; id=&quot;grid&quot;&gt; &lt;input type=&quot;checkbox&quot; id=&quot;checkall&quot;/&gt; &lt;div&gt; @grid.GetHtml( tableStyle:&quot;grid&quot;, headerStyle:&quot;head&quot;, alternatingRowStyle:&quot;alt&quot;, columns: grid.Columns( grid.Column(&quot;id&quot;, &quot;Id&quot;, style:&quot;tdId&quot;), grid.Column(&quot;subject&quot;, &quot;Subject&quot;, style: &quot;tdStyle&quot;), grid.Column(&quot;sender&quot;, &quot;From&quot;, style: &quot;tdStyle&quot;), grid.Column(&quot;date_sent&quot;, &quot;Date&quot;, style:&quot;tdStyle&quot;), grid.Column(header:&quot;&quot;, format:@&lt;input type=&quot;checkbox&quot; name=&quot;messageId&quot; value=&quot;@item.id&quot;/&gt;) ) ) &lt;/div&gt; &lt;input type=&quot;submit&quot; name=&quot;bulkDelete&quot; value=&quot;Delete&quot;/&gt; &lt;/form&gt; &lt;/div&gt;</pre> <p>After adding your script:</p> <pre class="prettyprint">&lt;script type="text/javascript"&gt; &#36;(function() { var propFn = typeof &#36;.fn.prop === 'function' ? 'prop' : 'attr'; &#36;('#checkall').click(function() { &#36;(this).parents('form:eq(0)').find(':checkbox')[propFn]('checked', this.checked); }); &#36;("input[type=checkbox]:not(#checkall)").click(function() { if (!this.checked) { &#36;("#checkall")[propFn]('checked', this.checked); } else { &#36;("#checkall")[propFn]('checked', !&#36;("input[type=checkbox]:not(#checkall)").filter(':not(:checked)').length); } }); }); &lt;/script&gt; &lt;div&gt; &lt;form method="post" action="" id="grid"&gt; &lt;script type="text/javascript"&gt; &#36;(function(){ var &#36;chk = &#36;('&lt;input/&gt;').attr({ type: 'checkbox', id:'checkall'}); &#36;('th:last').html('&lt;br /&gt;').append(&#36;chk); }); &lt;/script&gt; &lt;div&gt; @grid.GetHtml( tableStyle:"grid", headerStyle:"head", alternatingRowStyle:"alt", columns: grid.Columns( grid.Column("id", "Id", style:"tdId"), grid.Column("subject", "Subject", style: "tdStyle"), grid.Column("sender", "From", style: "tdStyle"), grid.Column("date_sent", "Date", style:"tdStyle"), grid.Column(header:"", format:@&lt;input type="checkbox" name="messageId" value="@item.id"/&gt;) ) ) &lt;/div&gt; &lt;input type="submit" name="bulkDelete" value="Delete"/&gt; &lt;/form&gt; &lt;/div&gt;</pre> <p>Thanks!</p> 2012-02-19T08:38:45-05:004839573http://forums.asp.net/p/1771075/4839573.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>I don't think you need the plugin. It seems straightforward enough to me without that.:</p> <pre class="prettyprint">&lt;form method=&quot;post&quot; action=&quot;&quot;&gt; &lt;div id=&quot;grid&quot;&gt; @grid.GetHtml( tableStyle:&quot;grid&quot;, headerStyle:&quot;head&quot;, alternatingRowStyle:&quot;alt&quot;, columns: grid.Columns( grid.Column(&quot;id&quot;, &quot;Id&quot;, style:&quot;tdId&quot;), grid.Column(&quot;subject&quot;, &quot;Subject&quot;, style: &quot;tdStyle&quot;), grid.Column(&quot;sender&quot;, &quot;From&quot;, style: &quot;tdStyle&quot;), grid.Column(&quot;date_sent&quot;, &quot;Date&quot;, style:&quot;tdStyle&quot;), grid.Column(header:&quot;&quot;, format:@&lt;input type=&quot;checkbox&quot; name=&quot;messageId&quot; value=&quot;@item.id&quot;/&gt;) ) ) &lt;/div&gt; &lt;input type=&quot;submit&quot; name=&quot;bulkDelete&quot; value=&quot;Delete&quot;/&gt; &lt;/form&gt; &lt;script&gt; $(function(){ addCheck(); }); function addCheck(){ var $chk = $('&lt;input/&gt;').attr({ type: 'checkbox', name:'chkAll', id: 'chkAll', title: &quot;Select All&quot;}); $('th:last').append($chk); $('#chkAll').click(function(){ $(':checkbox').attr('checked', $(this).is(':checked') ? 'checked' : '' ); }); } &lt;/script&gt;</pre> <p>Make sure you move the id="grid" from the form to the div, and also add an ajaxUpdateCallback to your new WebGrid() declaration to point ot the addCheck function (like this):</p> <pre class="prettyprint">var grid = new WebGrid(data, ajaxUpdateContainerId: "grid", ajaxUpdateCallback: "addCheck");</pre> <p></p> <p></p> 2012-02-19T10:20:43-05:004839602http://forums.asp.net/p/1771075/4839602.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>Thanks Mike! Your script made my code shorter. I just can't uncheck all the boxes once the checkall was checked.</p> <p></p> 2012-02-19T11:11:05-05:004839606http://forums.asp.net/p/1771075/4839606.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>It works fine in all browsers on my machine. Double check your code for typos and if you're still stcuk, post it here</p> 2012-02-19T11:18:49-05:004839791http://forums.asp.net/p/1771075/4839791.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>I don't know JQuery very well, but I think it's better to&nbsp;replace the line</p> <pre class="prettyprint">$(':checkbox').attr('checked', $(this).is(':checked') ? 'checked' : '' );</pre> <p>with</p> <pre class="prettyprint">&#36;(':checkbox').prop('checked', &#36;(this).is(':checked') ? true : false );</pre> <p>(using jQuery 1.6+).</p> <p>I have tried to implement a function that deselects chkAll if you uncheck another checkbox. The complete script follows:</p> <pre class="prettyprint">&lt;script&gt; &#36;(function(){ addCheck(); }); function addCheck() { var &#36;chk = &#36;('&lt;input/&gt;').attr({ type:'checkbox', name:'chkAll', id:'chkAll', title:"Select All"}); &#36;('th:last').append(&#36;chk); &#36;('#chkAll').click(function(){ &#36;(':checkbox').prop('checked', &#36;(this).is(':checked') ? true : false ); }); &#36;(':checkbox').not('#chkAll').click(function(){ testCheck(); }); } function testCheck() { if(&#36;(':checkbox').not('#chkAll').filter(':not(:checked)').length === 0){ &#36;('#chkAll').prop('checked', true); } else { &#36;('#chkAll').prop('checked', false); } } &lt;/script&gt;</pre> <p>Give it a try.</p> 2012-02-19T15:47:48-05:004839815http://forums.asp.net/p/1771075/4839815.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p></p> <blockquote><span class="icon-blockquote"></span> <h4>GmGregori</h4> <p></p> <pre class="prettyprint">$(':checkbox').prop('checked', $(this).is(':checked') ? true : false );</pre> <p>(using jQuery 1.6&#43;).</p> <p></p> </blockquote> <p></p> <p>I was using 1.4.2. That worked. it didn't with 1.7.1 until I changed to prop.</p> 2012-02-19T16:24:05-05:004840000http://forums.asp.net/p/1771075/4840000.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>This is awesome. Thanks!!</p> 2012-02-19T23:46:25-05:004840895http://forums.asp.net/p/1771075/4840895.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>Mike, I got another problem with the Database Helper. I can not use foreach correctly. It gives me this error:</p> <p></p> <pre class="prettyprint">'object' does not contain a definition for 'subject' and no extension method 'subject' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)</pre> <pre class="prettyprint">@foreach(var row in db.QueryIn(getFromSample, messageId)){ @row.subject&lt;br/&gt; @row.sender&lt;br/&gt; }</pre> 2012-02-20T10:12:19-05:004842369http://forums.asp.net/p/1771075/4842369.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>Change the extension method from my site so that it returns an IEnumerable&lt;dynamic&gt; instead of IEnumerable&lt;object&gt;.</p> 2012-02-21T07:01:01-05:005363420http://forums.asp.net/p/1771075/5363420.aspx/1?Re+Bulk+Delete+for+Webgrid+using+CheckboxRe: Bulk Delete for Webgrid using Checkbox <p>Another way to do that without any javascript;</p> <p></p> <pre class="prettyprint">@{ var db = Database.Open(&quot;MyGrid&quot;); if(IsPost){ var id = Request[&quot;RowID&quot;]; var arr = id.Split(','); var sql = &quot;delete from Clientes where ID=@0&quot;; for(int i = 0; i &lt; arr.Length; i&#43;&#43;) { db.Execute(sql,arr[i]); } } var data = db.Query(&quot;Select * from Clientes&quot;); var grid = new WebGrid(source:data, defaultSort:&quot;Nome&quot;, rowsPerPage:10, ajaxUpdateContainerId:&quot;grid&quot;); } &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;meta charset=&quot;utf-8&quot; /&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form method=&quot;post&quot; action=&quot;&quot;&gt; &lt;a href=&quot;Edit.cshtml&quot;&gt;Criar Novo&lt;/a&gt; &amp;nbsp | &amp;nbsp &lt;input type=&quot;submit&quot; value=&quot;Deletar&quot;/&gt; &lt;br&gt; @grid.GetHtml( tableStyle:&quot;grid&quot;, headerStyle:&quot;head&quot;, alternatingRowStyle:&quot;alt&quot;, columns: grid.Columns( grid.Column(&quot;id&quot;, &quot;Id&quot;, style:&quot;tdId&quot;), grid.Column(&quot;Nome&quot;, &quot;Nome&quot;, style: &quot;tdStyle&quot;), grid.Column(&quot;CNPJ&quot;, &quot;CNPJ&quot;, style: &quot;tdStyle&quot;), grid.Column(header:&quot;&quot;, format:@&lt;input type=&quot;checkbox&quot; name=&quot;RowId&quot; value=&quot;@item.id&quot;/&gt;))) &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><br> <br> </p> 2013-04-14T13:47:54-04:00