0

In my MVC 3 Razor view i have a table with check-boxes as follows

 <table border="1" id="permiison">
 <thead>
    <tr><th style="width:0px"></th><th>Menu Name</th><th>Add</th> <th>Edit</th><th>Authorize</th><th>View</th></tr>
</thead>
<tbody>
    <tr id="per_tr">
    <td style="width:0px">10</td><td></td><td><input type="checkbox" checked="True" style="width:80px" id="chkAdd"></td><td><input type="checkbox"   checked="True" style="width:80px"></td><td><input type="checkbox" checked="True" style="width:80px"></td> <td><input type="checkbox" style="width:80px" checked="True"></td>
    </tr>

    <tr id="per_tr"><td style="width:0px">11</td><td><span style="width:150px">Group</span></td><td><input type="checkbox" checked="True" style="width:80px" id="chkAdd"></td><td><input type="checkbox" checked="True" style="width:80px"></td><td><input type="checkbox" checked="True" style="width:80px"></td> <td><input type="checkbox" style="width:80px" checked="True"></td>

    </tr>
</tbody>
</table>

I want to convert the table into json with checked values of check boxes. If any body knows please share the methode.

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
Nithesh Narayanan
  • 10,227
  • 32
  • 91
  • 134

2 Answers2

3

You can do something like:

(function($){
    var getJsonFromTable = function()
        {
            var rows = [];
            $('#permiison tbody tr').each(function(i, n){
                var $row = $(n);
                rows.push({
                    id: $row.find('td:eq(0)').text(),
                    name: $row.find('td:eq(1)').text(),
                    add: $row.find('td:eq(2) input[type=checkbox]').prop('checked'), 
                    edit: $row.find('td:eq(3) input[type=checkbox]').prop('checked'), 
                    authorize: $row.find('td:eq(4) input[type=checkbox]').prop('checked'), 
                    view: $row.find('td:eq(5) input[type=checkbox]').prop('checked'),                                              
                });                
            });
            return JSON.stringify(rows);
        };
    $(function(){        
    console.log(getJsonFromTable ());
    });
})(jQuery);

http://jsfiddle.net/PKFaE/

Wouter J
  • 39,482
  • 15
  • 97
  • 108
gislikonrad
  • 3,121
  • 2
  • 19
  • 22
0

Or look into here and do the same for your own situation (its successfully answered question):

convert a HTML table data into a JSON object in jQuery

Good luck and hope this is of help to you.

Community
  • 1
  • 1
Display Name
  • 4,676
  • 1
  • 29
  • 43