Use child row to display all self referencing table rows

Use child row to display all self referencing table rows

fredrik.fredrik. Posts: 1Questions: 1Answers: 0
edited February 2017 in Free community support

Hello, I have a DB table structured the following way:

https://gyazo.com/c8d2c95703a12f788767b00ccb8a9524 (Child PK)

Basically, I want the child columns with parent null and their descriptions to be displayed first. Then I want the child columns that are referenced to their parent to be displayed under.

So if you use this picture as a reference it would look like: https://gyazo.com/102731409cd5847ec2ca9320fc0b6228

DimCustomer - Customer Dimension
Name - Customer's name
Country - Customer's country

Is it possible to somehow loop through this? Ideas, tips, example codes, everything will be appreciated. Thanks :)

<html>
        <div style="max-width:70%; margin:0 auto">
        <table id="myDatatable" class="display responsive" width="100%">
            <thead>
                <tr>
                    <th class="all">Table</th>
                    <th class="all">Description</th>
                    <th class="none"></th>
                </tr>
            </thead>
        </div>

</html>


<script>
            $(document).ready(function () {
                var oTable = $('#myDatatable').DataTable({

                    "ajax": {
                        "url": '/home/GetTable',
                        "type": "get",
                        "datatype": "json"
                    },

                    "columns": [
                         
                        {"data": "Child", "autoWidth": true},
                        { "data": "Description", "autoWidth": true },
                        { "data": "Parent", "autoWidth": true },



                    ],


                })


            })

        </script>


```
public ActionResult GetTable()
{
using (DBEntities dc = new DBEntities())
{
var table = dc.Tables.Where(a => a.Parent == null).ToList();
return Json(new { data = table }, JsonRequestBehavior.AllowGet);
}
}

```

This discussion has been closed.