CRUD child row where child row is a property ILIST of the parent row

CRUD child row where child row is a property ILIST of the parent row

ShirleyWShirleyW Posts: 16Questions: 4Answers: 2
edited May 2017 in Free community support

Hello,

I have the following 2 classes defined in the Web API (I simplified the class here, the image provides a visual)
(1)Booking, the parent record; and
(2)Allocation, the child record, displayed as collapsible child rows under the parent

How can I add/delete/modify only the ProjectAllocation property of the Booking class?
I understand the child row is "drawn" when user clicks the expand icon, do I need to update the JSON and then redraw the child table for this parent or redraw the whole JSON and reload the whole datatable again? Any examples or where to research/read?
I am new to DataTables.net, am trying to wrap my head around the plugin....to use it the correct way.

Thanks again in advance,
Shirley

using System.Collections.Generic;

namespace BookingsAllocation.Models
{
    public class Booking
    {
        public string ProjectCode { get; set; }
        public string ProjectName { get; set; }             
        public IList<Allocation> ProjectAllocation { get; set; }
    }

 public class Allocation
    {
        public string ProjectCode { get; set; }
        public string EmpCode { get; set; }
        public string EmpName { get; set; }       
        public decimal AllocatedPercent { get; set; }     
    }
}
 function format(d) {
                var subTableHeader =
                    '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;"><thead><tr>' +
                    '<th></th><th></th>' +
                    '<th class="AlignContentLeft">Employee</th>' +
                    '<th class="AlignContentRight">Allocated %</th>' +
                    '<th class="AlignContentRight">Equivalent $</th>' +
                    '<th class="AlignContentRight" colspan=3>Comments</th><th></th><th></th></tr></thead>';
                var subTableBody = '';

                var totalChildRows = d.ProjectAllocation.length + 1;
                for (i = 0; i < d.ProjectAllocation.length; i++) {
                    subTableBody +=
                        '<tr><td><img src="Images/BAT-Delete.png" title="Delete" onclick="ConfirmDelete(' + "'" + d.ProjectAllocation[i].EmpName + "'" + ');"/></td>' +                      
                        '<td><img src="Images/BAT-Edit.png" title="Edit"/></td>' +
                        '<td class="AlignContentLeft">' + d.ProjectAllocation[i].EmpName + '</td>' +
                        '<td class="AlignContentRight">' + Math.round(d.ProjectAllocation[i].AllocatedPercent * 100) + '%' + '</td>' +
                        '<td class="AlignContentRight">' + addCommas(d.ProjectAllocation[i].AllocatedAmount) + '</td>'

                    if (i == 0) {
                        subTableBody += '<td rowspan= ' + totalChildRows + ' colspan=3 class="childRowComments"> ' + d.Comments + '</td></tr>'
                    }
                    else
                        subTableBody += '</tr>';
                };
                //Insert row to ADD new allocation
                //var insertionRow = '<tr><td></td><td><img src="Images/BAT-Edit.png" title="Insert"/></td><td>Name</td><td>Percent</td><td>$$$ Amount</td><td colspan=3>Comments</td></tr>';
                return '<div class="slider">' + subTableHeader + '<tbody>' + subTableBody + '</tbody></table></div>';
            }

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Hi Shirley,

    Can you show me your C# controller code as well please? Is ProjectAllocation being used as an MJoin for example?

    Unfortunately you can't you IList parameters in the models. The models must be directly convertible to JSON, which basically means that you are limited to numbers, booleans and strings. If you could show me your controller I'll see if I can understand what you goal is.

    It looks like you are trying to include the data for the editable child table in with the master table - is that correct? If so, I would abandon that approach. The provided .NET libraries weren't designed with that in mind. Instead I would suggest having the child table as its own table that is dynamically created. There is a blog post which describes how you can Ajax load content for the child row.

    Allan

This discussion has been closed.