0

I have an array which is in following format

{
    "items": {
        "0": 96,
        "1": 121566,
        "3": "2014-10-30"
    },
    "name": [
        "asdfghj"
    ],
    "misc": [
        {
            "VehicleBrandName": "FORD",
            "VehicleTypeName": "111111"
        },
        {
            "VehicleBrandName": "BMW",
            "VehicleTypeName": "75676576"
        },
        {
            "VehicleBrandName": "FORD",
            "VehicleTypeName": "2222"
        }
    ]
}

here items,name,misc are values from different table. Items passed as an object and name and misc are arrays.

In jquery function

var misc = data.misc;
var items = data.items;
var name = data.customer;
$.each(items, function (key, value) {// working function
    part = $('.check').find('*[data-field="' + key + '"]');
    part.val(value);
});
$(misc).each(function (key, value) {//not working
   for (name in value) {
      if (value.hasOwnProperty(name)) {
         part = $('.check').find('*[table-field="' + name + '"]');
         part.text(value[name]);
       }
   }
   $('tr.active').clone().appendTo('tbody');
});

And HTML code is

<table class="table table-condensed table-striped check" id="TableAppend">
   <tbody class="items">
      <tr class="active">
         <td style="font-size: 12px;" width="70"><p table-field="VehicleBrandName"></p></td>
         <td style="font-size: 12px;" width="70"><p table-field="VehicleTypeName"></p></td>
      </tr>
    </tbody>
 </table>

items are working fine. But misc is getting only single value.Other two values are not getting. I expecting the following output.

FORD       BMW        FORD 
111111     75676576   2222

Please help me......

12
  • can you share the html sample Commented Nov 18, 2014 at 8:59
  • I don't think "$(misc).each(function (key, value)" is where the problem is, you need to use .find('*[table-field="' + key + '"]' or something. Commented Nov 18, 2014 at 9:05
  • 1
    What do you mean by 'is not getting any value'? Your loop works correctly based on the code provided - I'm able to console.log the variables inside the $(misc).each loop... Commented Nov 18, 2014 at 9:05
  • what is not working?I tried to loop over data your code is working Commented Nov 18, 2014 at 9:08
  • What exactly do you want to get? In your code you are just overwrite the html, and get "FORD 2222" as a result. jsfiddle.net/4u1wtgm0 Commented Nov 18, 2014 at 9:17

2 Answers 2

2

instead of

 part = $('.check').find('*[table-field="' + name + '"]');

try

part = $('.active').find('*[table-field="' + name + '"]');

because you have class=active for table-field.

you can try something like this:

  <script>

    $(document).ready(function(){ 

   var json=JSON.parse('{"items":{"0":96,"1":121566,"3":"2014-10-30"},"name":["asdfghj"],"misc":[{"VehicleBrandName":"FORD","VehicleTypeName":"111111"},{"VehicleBrandName":"BMW","VehicleTypeName":"75676576"},{"VehicleBrandName":"FORD","VehicleTypeName":"2222"}]}');
var misc=json.misc;
console.log(json);
var string1=""; 
var string2="";
           $(misc).each(function (key, value) {//not working

               for (name in value) { 
                   if (value.hasOwnProperty(name)) {  
                     part = $('.active').find('*[table-field="' + name + '"]');
                    // 
                    if(parseInt(value[name])%1== 0){
                    string1=string1+"  "+value[name];   
                    part.text(string1);   
                    $('.active').append(part);  
                    }else{
                        string2=string2+"  "+value[name];
                        part.text(string2);
                         $('.active').append(part);
                    }    

                   }
               }

            });

    });

   </script>
Sign up to request clarification or add additional context in comments.

Comments

0

Do it in this way:

var $tr = $('tr.active');
$(misc).each(function (key, value) {
   var $newTr = $tr.clone();
   $('[table-field="VehicleBrandName"]', $newTr).text(value.VehicleBrandName);
    $('[table-field="VehicleTypeName"]', $newTr).text(value.VehicleTypeName);

    $newTr.appendTo('tbody')
});
$tr.remove()

http://jsfiddle.net/rz4uswen/1/

1 Comment

Thank you.It worked. I tried to generalize that function. If more no of table-field contain, the code will very length.If database field name is given to table-field, then is it possible to get field name in this function so that I can generalize??

Your Answer

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