0

I have this list of items, and I have a button at the bottom for deleting some of them according to their checkboxes. I also got a response from a jQuery get() with the elements that represent the new list. So, the rest need to be removed. This is how my list is built :

<table style="float:left;width:100%">
<tbody>
    <?php foreach ($items as $item): ?>
    <tr>
        <div>
            <td ?>;
                <input type="checkbox" name="item_<?php echo $item['NAME'];?>" value="<?php echo $item['ID'];?>" />
            </td>
        </div>
    </tr>
    <?php endforeach; ?>
</tbody>

I use the get() function and get the following reply in json.

[{"ID":"2","EMAIL":"[email protected]","DATETIME":"2011-12-12 03:20:01"},{"ID":"4","EMAIL":"[email protected]","DATETIME":"2011-10-09 01:15:22"}"]

All I want to do now is simply eliminate the correspoding where the input value (which is the json's ID) is NOT in the json reply.

Any ideas how to do this with jQuery ? Thanks !

1 Answer 1

1

There are several different ways to do this. I kind of like going through your json response, flagging those that are confirmed, delete those unconfirmed, and remove the confirmed flag. e.g.

var json = [{'id':'2','email':... }];

// first, flag what we DO have
$.each(json,function(n,r){
  var $i = $(':input[value="' + r.ID + '"]');
  if ($i.length > 0){
    $i.addClass('ajax-confirmed');
  }
});
// delete what hasn't been flagged
$(':input:not(.ajax-confirmed)').each(function(m,r){
  var $row = $(r).closest('tr');
  $row.remove();
});
// then remove the flag (cleanup)
$(':input.ajax-confirmed').removeClass('ajax-confirmed');

Fiddle showing it in action: http://jsfiddle.net/v9paf/ (Or if you want to feel like you're deleting it... http://jsfiddle.net/v9paf/1/)

Sign up to request clarification or add additional context in comments.

7 Comments

PS, a <div> tag has no place being between a <tr> and a <td>--I think you need to reverse the <div>/<td> spots...
I have more elemnts inside that tr, (I simplified the table for readablility). I was trying to make these elements show one on top of the other, any suggestions ?
For some reason it is erasing all the rows.
@Ted: Do you have a webpage up that I can see it on? Or can you paste what you have (maybe create your own fiddle?)
jsfiddle.net/9vYDe/1 This is actual code from the site. and it doesn't work on jsFiddle either
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.