1

I have a json data from the server side and I have iterated it into a table using jQuery. Now I want to delete some elements or ids from the original JSON result(Based on the checkbox selection of the table) and bind the new JSON data with the table.

My ajax function:

$.ajax({
        type: "POST",
        contentType: 'application/json;charset=utf-8',
        dataType:'json',
          url : methodURL,
          data : JSON.stringify(val),
          success : function(data) {
              //alert("success");


              if(data!=''){
                  $('#load_image').hide();
                  $("#selectitem").show();
                  $("#button_grp").show();


                  $.each(data, function(i, item) {

                       tr = $('<tr/>');
                       tr.append($("<td />").html('<input type="checkbox" name="selector[]" value='+item[1]+'>'));


                       tr.append("<td>" + item[1] + "</td>");
                       tr.append("<td>" + item[2] + "</td>");
                       tr.append("<td>" + item[3] + "</td>");
                       tr.append("<td>" + item[4] + "</td>");
                       tr.append("<td>" + item[5] + "</td>");


                       tr.append($("<td/>").html('<input type="textbox" name="quantity" id="quantity"/>'));

                       $('#ttp').append(tr);
                    });
       }
$('#result').text(data);
          }
      });


<table id="ttp"></table>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Don
  • 141
  • 10
  • 1
    Do you have any control over the data layer? You shouldn't be removing anything from the JSON result, you ideally want to just not return them at all - perhaps pass up some filter criteria through the `data: {}` property on your AJAX call? – Paul Aldred-Bann Jan 22 '15 at 10:13
  • At first i want to bind whole result to a table after i select some rows from the table with the check box which i have used in my code only need to bind the result which i have not selected. @PaulAldred-Bann – Don Jan 22 '15 at 10:24
  • Oh I see, so you're creating sub-sets of your main set. You still don't really want to be actually removing these elements in this way - perhaps introduce some kind of filtering to what DOM elements you're creating, I'd suggest looking at a data binding framework - something like http://knockoutjs.com or https://angularjs.org – Paul Aldred-Bann Jan 22 '15 at 10:36
  • 1
    Cant i use pure jquery for doing this filtering mechanism? Or is there any other way using jquery? @PaulAldred-Bann – Don Jan 22 '15 at 10:53
  • 1
    well you can... but your code runs the risk of becoming spaghetti-like as you'll have to do something like `if ... then create DOM element else move to next object end if` where a framework will handle all this boiler plate stuff for you - in addition to setting you up for things like this in the future. – Paul Aldred-Bann Jan 22 '15 at 11:36
  • once i have checked a check box how can i again calling the ajax again and apply if ... else between the already bind table. @PaulAldred-Bann – Don Jan 23 '15 at 06:03
  • that's kinda out of scope for the comments list, there will surely be a question like this already - basically you need to `.on("change")` your checkbox DOM element - look for javascript event listeners. – Paul Aldred-Bann Jan 23 '15 at 10:18

1 Answers1

0

Use one of these:

 delete data.property;
 delete myJSONObject['property'];

 var property = "property";
 delete data[property];

stackoverflow blog post about delete by @kangax

Community
  • 1
  • 1
Simon K.
  • 348
  • 1
  • 7
  • 24