2

I'm unable to do multiple deletion when I try something on my own and single delete is working but multiple delete is not working need help!

    // Flexigrid 
    if (com=='Delete')
            {

               if($('.trSelected').length>0){ 
                            if(confirm('Delete ' + $('.trSelected').length + ' rows?')){ 
                                var items = parseInt($('.trSelected').text(),10); 
                                    var itemlist =items; 
                                    for(i=0;i<items.length;i++){ 
                                          itemlist+= items[i].id.substr(3)+" "; //its contains no value for multiple delete
                                    }       
                                    $.ajax({
                   type: "POST",
                   dataType: "json",
                   url: "delete.php",
                   data: "items="+itemlist,
                   success: function(data){
                 alert("Query: "+data.query+" - Total affected rows: "+data.total);
                   $("#flex1").flexReload();
                   }
                 });
                }
                } else {
                    return false;
                } 
            }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stan
  • 21
  • 2

1 Answers1

0

This is the problem:

var items = parseInt($('.trSelected').text(),10); 
var itemlist =items; 
for(i=0;i<items.length;i++){ 
    itemlist+= items[i].id.substr(3)+" "; //its contains no value for multiple delete
} 

Whenever there are multiple items, you cannot call parseInt() on the jQuery collection, as it doesn't know what to do with it. Instead, you can map the selection into a list of parsed .text() values, e.g.

$('.trSelected').map(function(index, item) {
    return parseInt($(item).text(), 10);
}

This will turn a group of elements into an array of numbers. But then you cannot use it in the item list in the next for loop. So instead you need to simplify the logic, instead of returning the parsed int list, just return the id as you require it processed. e.g.

$('.trSelected').map(function(index, item) {
    return item.id.substr(3);
}

Just be careful with the map, if you want to treat the item as a regular DOM element, use item, but if you need jQuery stuff like .css() you need to wrap item in $. i.e. $(item). Alternatively you can use $(this) to refer to the current item, which will likely be faster.

Good luck!

Rym
  • 650
  • 4
  • 16
  • Ty for the reply :) i'll chk it rite a way ty man :) but i did try with out parseint and it didnt work – Stan Oct 23 '12 at 02:09