c# - Bootstrap Datatables JSON class parsing -


i have problems bootstrap datatable plugin class looks this:

model

public class class1 {   public string employeeid { get; set; }   public string firstname { get; set; }   public position position { get; set; }    public class1 getemployees()  {   return this;  } }  public class position {   public string positionid { get; set; }   public string positionname { get; set; } //other functions below } 

controller

public jsonresult getemployees() {  return json(new class1().getemployees(), jsonrequestbehavior.allowget); } 

html

   <table id="tblleavecredits"     data-url="/employees/getemployees"      data-toggle="table"      data-search="true"     data-click-to-select="true"     data-select-item-name="rdoselecteditem"     data-cache="false">      <thead>       <tr>        <th data-field="state" data-radio="true"></th>        <th data-field="firstname" data-sortable="true">description</th>        <th data-field="position.positionname" data-sortable="true">available</th>       </tr>      </thead>    </table> 

my question is, how can data of position class json return , display field in datatable?

as stated above seems datatable plugin has no option bind array of nested json-objects table. used client side approach flatten array of nested json-objects able bind data-table. see my demo bind nested object datatable:

$(function () {     // apply flattenjson every item in array , return new array     // using jquery.map     flatteneddata = jquery.map( data, function(d){ return flattenjson(d) });     // bind unnested array datatable     $('#table').bootstraptable({         data: flatteneddata     });     console.log(flatteneddata); }); 

given array of nested json objects:

var data = [     {         "employeeid": "123",         "firstname": "marc",         "position": {"positionid": 1, "positionname": "supermarket"}     },     {         "employeeid": "456",         "firstname": "scott",         "position": {"positionid": 2, "positionname": "googleplex"}     },     {         "employeeid": "789",         "firstname": "john",         "position": {"positionid": 3, "positionname": "sanfran"}     }     ]; 

to flatten nested json object took code answer:

function flattenjson(data) {     var result = {};     function recurse (cur, prop) {         if (object(cur) !== cur) {             result[prop] = cur;         } else if (array.isarray(cur)) {              for(var i=0, l=cur.length; i<l; i++)                  recurse(cur[i], prop + "[" + + "]");             if (l == 0)                 result[prop] = [];         } else {             var isempty = true;             (var p in cur) {                 isempty = false;                 recurse(cur[p], prop ? prop+"."+p : p);             }             if (isempty && prop)                 result[prop] = {};         }     }     recurse(data, "");     return result; } 

to bind unnested object can use html

<table id="table">     <thead>     <tr>         <th data-field="employeeid">id</th>         <th data-field="firstname">firstname</th>         <th data-field="position.positionid">positionid</th>                 <th data-field="position.positionname">positionname</th>             </tr>     </thead> </table> 

that see working demo bind nested object datatable


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -