Options array not loading properly

Options array not loading properly

rbicardrbicard Posts: 7Questions: 2Answers: 0
edited September 2017 in Editor

I have tried everything I can think of but I cannot get the options array to load for me. I have the left join working and pulling the description from the child class properly. But my selection option is showing with an empty dropdown due to the options class not returning anything.

The problem is this: the datatable loads just fine and pulls the defect description from the linked table (inspectionDefectCode table). But when I do the editor, the select field is empty. So, looking at the api call, I'm getting the filled out data records from the InspectionDefect and InspectionDefectCode linked table records. But in the options array at the bottom of the call, it is empty.

Can someone please tell me what I'm doing wrong? I've now spent two entire days trying every option I can imagine and searching through endless posts about this to no avail.

I'm using .NET and here is my model structure for the 2 tables referenced:

    public class DefectClassModel
    {
        public class InspectionDefect
        {
            public int inspectionDefectID { get; set; }

            public int projectID { get; set; }

            public int parentDefectCode { get; set; }

            public double? defectQuantity { get; set; }

            public string defectComment { get; set; }

            public bool? repairRequired { get; set; }

            public int? buildingID { get; set; }

            public bool? ratingItem { get; set; }

            public string defectUnit { get; set; }

            public bool? repairCompleted { get; set; }

            public int? unitID { get; set; }

            public double? unitConversionFactor { get; set; }

        }

        public class InspectionDefectCode
        {
            public int defectCode { get; set; }

            public string defectLabel { get; set; }

            public string defectDescription { get; set; }

            public int? defectCategoryID { get; set; }
        }
    }

Here's my controller code:

  public class InspectionDefectController : ApiController
  {
        DataContext dc = new DataContext();
        [Route("api/inspectiondefect/{id}")]
        [HttpGet]
        [HttpPost]
        public IHttpActionResult defectGetPost(int id)
        {
            var request = HttpContext.Current.Request;
            string dbConnectionString = ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString;
            string dbType = "sqlserver";

            using (var db = new Database(dbType, dbConnectionString))
            {
                if(request.Form["action"] == "create")
                {
                    var response = new Editor(db, "InspectionDefect", "inspectionDefectID")
                        .Model<InspectionDefectClass>()
                                                //.Field(new Field("InspectionDefect.defectCode")
                                                //          .Options(new Options()
                                                //          .Table("InspectionDefectCode")
                                                //          .Value("inspectionDefectID")
                                                //          .Label("defectDescription"))
                                                //          .Options("InspectionDefectCode", "inspectionDefectID", "defectDescription")
                                                //          )
                                                //.LeftJoin("InspectionDefectCode", "InspectionDefectCode.defectCode", "=", "InspectionDefect.defectCode")
                                                .Where("projectID", id, "=")
                                        .Field(new Field("InspectionDefectCode.defectDescription")
                                                .Options(new Options()
                                                        .Table("InspectionDefectCode")
                                                        .Value("InspectionDefectCode.defectCode")
                                                        .Label("InspectionDefectCode.defectDescription")
                                                )
                                                .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                                        )
                        .LeftJoin("InspectionDefectCode", "InspectionDefectCode.defectCode", "=", "InspectionDefect.parentDefectCode")

                        .Process(request)

                        .Process(request)
                        .Data();
                    return Json(response);
                }
                else
                {
                    var response = new Editor(db, "InspectionDefect", "inspectionDefectID")
                        .Model<DefectClassModel>()
                        .Field(new Field("InspectionDefect.parentDefectCode")
                                .Options(new Options()
                                        .Table("InspectionDefectCode")
                                        .Value("defectCode")
                                        .Label("defectDescription")
                                )
                                .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                                .Options("InspectionDefectCode", "defectCode", "defectDescription")
                        )
                        .LeftJoin("InspectionDefectCode", "InspectionDefectCode.defectCode", "=", "InspectionDefect.parentDefectCode")
                        .Where("projectID", id, "=")
                        .Process(request)
                        .Data();
                    return Json(response);
                }
            }

        }
    }

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Due to the if statement, it will be the code on lines 46-61 that is executed when the page is loaded. InspectionDefect.parentDefectCode is the field which has an Options method defined (note that this is different from what is in the other half of the if statement). The Options method is currently being called twice - only the second will be executed (although they currently look the same - just different forms). I'd suggest removing the second one just to tidy the code up a little.

    Can you show me the JSON that is being returned from the server-side please? Can you also show me your client-side (i.e. Javascript) initialisation for Editor?

    Thanks,
    Allan

  • rbicardrbicard Posts: 7Questions: 2Answers: 0

    Thanks for the quick response.

    Yes, I'm aware of the if statement being different but I was just trying to get it to work in the edit mode before I made the corresponding changes to fix the create code.

    As far as the Options being loaded twice, I didn't think we necessarily needed that 2nd one but I pulled that from one of your .NET samples (Editor 1.6.2 .NET Examples) where you did the same with the options for loading the sites with a value of id and label of name. You did it twice in there so I thought maybe I needed to in this case. (FYI - it's in the JoinLinkTableController.cs file).

    Here is the json returned from the server-side:

    {"draw":null,"data":[{"DT_RowId":"row_10377","InspectionDefect":{"parentDefectCode":8,"inspectionDefectID":10377,"projectID":4827,"defectQuantity":7.0,"defectComment":null,"repairRequired":true,"buildingID":3050,"ratingItem":true,"defectUnit":null,"repairCompleted":false,"unitID":1,"unitConversionFactor":1.0},"InspectionDefectCode":{"defectCode":8,"defectLabel":"FB","defectDescription":"Foam Blisters ","defectCategoryID":2}},{"DT_RowId":"row_10378","InspectionDefect":{"parentDefectCode":6,"inspectionDefectID":10378,"projectID":4827,"defectQuantity":300.0,"defectComment":null,"repairRequired":true,"buildingID":3050,"ratingItem":true,"defectUnit":null,"repairCompleted":false,"unitID":2,"unitConversionFactor":0.10000000149011612},"InspectionDefectCode":{"defectCode":6,"defectLabel":"EF","defectDescription":"Exposed Foam ","defectCategoryID":2}},{"DT_RowId":"row_10379","InspectionDefect":{"parentDefectCode":10,"inspectionDefectID":10379,"projectID":4827,"defectQuantity":1.0,"defectComment":null,"repairRequired":true,"buildingID":3050,"ratingItem":false,"defectUnit":null,"repairCompleted":false,"unitID":1,"unitConversionFactor":1.0},"InspectionDefectCode":{"defectCode":10,"defectLabel":"MD","defectDescription":"Mechanical Damage ","defectCategoryID":2}}],"recordsTotal":null,"recordsFiltered":null,"error":null,"fieldErrors":[],"id":null,"meta":{},"options":{},"files":{},"upload":{"id":null},"debugSql":null,"cancelled":[]}

    And here is the Javascript file:
    (function ($) {
    function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[[]]/g, "\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/+/g, " "));
    }

    var appID;
    appID = getParameterByName('id', window.location.href);
    $(document).ready(function () {
        console.log("In controller");
        var valueID = appID;
        var pth = "api/inspectionDefect/" + valueID;
    
        var editor = new $.fn.dataTable.Editor({
            ajax: pth,
            table: '#defectTable',
            fields: [
                {
                    label: "Defect Code:",
                    name: "InspectionDefectCode.defectDescription",
                    type: "select",
                    options: [
                        {
                            label: "InspectionDefectCode.defectDescription"
                            , value: "InspectionDefectCode.defectCode"
                        }
                    ]
                },
                {
                    label: "Qty:",
                    name: "defectQuantity"
                },
                {
                    label: "Units:",
                    name: "unitID"
                },
                {
                    label: "Rating Item:",
                    name: "ratingItem",
                    type: "checkbox",
                    separator: "|",
                    options: [
                        { label: '', value: 1 }
                    ]
                },
                {
                    label: "Repair Required:",
                    name: "repairRequired",
                    type: "checkbox",
                    separator: "|",
                    options: [
                        { label: '', value: 1 }
                    ]
                },
                {
                    label: "Repair Completed:",
                    name: "repairCompleted",
                    type: "checkbox",
                    separator: "|",
                    options: [
                        { label: '', value: 1 }
                    ]
                },
                {
                    label: "Comments:",
                    name: "defectComment"
                } 
            ]
        });
    
        var table = $('#defectTable').DataTable({
            dom: 'Bfrtip',
            ajax: pth,
            columns: [
                {
                    data: "InspectionDefectCode.parentDefectCode"
                },
                {
                    data: "defectQuantity"
                },
                {
                    data: "unitID"
                },
                {
                    data: "ratingItem"
                },
                {
                    data: "repairRequired"
                },
                {
                    data: "repairCompleted"
                },
                {
                    data: "defectComment"
                }
            ],
            columnDefs: [
                {
                    render: function (data, type, row) {
                        if (data == true) {
                            return '<input type="checkbox" checked disabled></input>';
                        }
                        else {
                            return '<input type="checkbox" disabled></input>';
                        }
                    },
                    targets: [3, 4, 5]
                }],
            pageLength: 50,
            sort: false,
            searching: false,
            select: true,
            buttons: [
                { extend: 'create', editor: editor },
                { extend: 'edit', editor: editor },
                { extend: 'remove', editor: editor }
            ]
        });
    
        editor.on('preSubmit', function (e, data, action) {
            if (action === 'create') {
                for (var key in data.data) {
                    data.data[key].projectID = appID;
                }
            }
        });
    
    });
    

    }(jQuery));

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You did it twice in there so I thought maybe I needed to in this case. (FYI - it's in the JoinLinkTableController.cs file).

    Oops - thank you! I'll get that fixed.

    What is weird is that the options object is completely empty. That sort of suggests that it isn't being executed at all.

    You mentioned Editor 1.6.2, I've got a feeling that this is a bug in that version. Could you update to 1.6.5 which is the current release.

    Thanks,
    Allan

  • rbicardrbicard Posts: 7Questions: 2Answers: 0

    That did indeed fix the issue, Allan. Thank you very much for your prompt responses!
    By the way, love your product!

This discussion has been closed.