How to make bind and edit a foreign keys in tree grid

Hi,
Q1- How to make bind and edit a foreign keys in self reference tree grid ?
Q2- How to make enable and disable specific field based on dropdown list value changed?
I tried this code  
Index.cshtml
@{
    var DDL_TicketDeduction = new Syncfusion.EJ2.DropDowns.DropDownList()
    {
        DataSource = ViewBag.DeductionTypes,
        Query = "new ej.data.Query()",
        Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings()
        {
            Value = "ID",
            Text = "DeductionName"
        }
    };
    var DDL_DurationTypes = new Syncfusion.EJ2.DropDowns.DropDownList()
    {
        DataSource = ViewBag.ExcuseDurationTypes,
        Query = "new ej.data.Query()",
        Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings()
        {
            Value = "ID",
            Text = "DurationName"
        }
    };
    var DDL_MinEmpDurationType = new Syncfusion.EJ2.DropDowns.DropDownList()
    {
        DataSource = ViewBag.DurationTypes,
        Query = "new ej.data.Query()",
        Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings()
        {
            Value = "Value",
            Text = "Text"
        }
    };
    var DDL_ResetIntervals = new Syncfusion.EJ2.DropDowns.DropDownList()
    {
        DataSource = ViewBag.ResetIntervals,
        Query = "new ej.data.Query()",
        Change = "onChange",
        Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings()
        {
            Value = "Value",
            Text = "Text"
        }
    };

}

<ejs-treegrid id="TreeGrid" allowTextWrap="true" height="400" idMapping="ID" actionFailure="failure" allowSelection="true"  actionBegin="onactionBegin" hasChildMapping="Isparent" parentIdMapping='ParentExcuseConfigurationID' treeColumnIndex="1" toolbar="@(new List<string>() { "Add","Edit", "Delete", "Update", "Cancel" })">
    <e-data-manager url="/ExcuseConfiguration/DataSource" insertUrl="/ExcuseConfiguration/Create" updateUrl="/ExcuseConfiguration/Edit" removeUrl="/ExcuseConfiguration/Delete" adaptor="UrlAdaptor"></e-data-manager>
    <e-treegrid-editsettings allowAdding="true"  allowEditing="true"  allowDeleting="true" mode="Dialog" ></e-treegrid-editsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="ID" headerText="ID" visible="false" isPrimaryKey="true" textAlign="Right" width="70"></e-treegrid-column>
        <e-treegrid-column field="ExcuseName" headerText="@ResourcesWeb.PayrollResource.ExcuseName" editType="stringedit" width="180"></e-treegrid-column>
        <e-treegrid-column field="MaxNumberAllowed" headerText="MaxNumberAllowed" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="TotalNumberAllowed" headerText="TotalNumberAllowed" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="TotalDurationAllowed" headerText="TotalDurationAllowed" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="MaxDurationAllowd" headerText="MaxDurationAllowd" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="MinDurationAllowd" headerText="MinDurationAllowd" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="IntervalTimeOfExcuse" headerText="IntervalTimeOfExcuse" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="ResetIntervalCreditDuration" headerText="ResetIntervalCreditDuration"  width="180" editType="dropdownedit" edit="new {@params = DDL_ResetIntervals }"></e-treegrid-column>
        <e-treegrid-column id="CustomYearsNumber" field="CustomYearsNumber" headerText="CustomYearsNumber" editType="numericedit" validationRules="@(new {number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="MinToleranceDaysExcuse" headerText="MinToleranceDaysExcuse" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="DurationType" headerText="DurationType" width="180" editType="dropdownedit" edit="new {@params = DDL_MinEmpDurationType }"></e-treegrid-column>
        <e-treegrid-column field="MinEmployeeDuration" headerText="MinEmployeeDuration" width="180"></e-treegrid-column>
        <e-treegrid-column field="TicketDeductionTypeID" headerText="TicketDeductionType" width="180" editType="dropdownedit" edit="new {@params = DDL_TicketDeduction }"></e-treegrid-column>
        <e-treegrid-column field="ExcuseDurationTypeID" headerText="ExcuseDurationType" width="180" editType="dropdownedit" edit="new {@params = DDL_DurationTypes }"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>


<script>
    function failure(args) {
        var errorMessage = args.error[0].error.responseText.split("Exception:")[1].split('<br>')[0];  //extract the message from args
        alert(errorMessage);
    }
    function onactionBegin(args) {
        if (args.requestType === 'beginEdit') {
            // while the double click the row or clicking the toolbar edit icon
            this.getColumnByIndex(1).allowEditing = args.rowData.ResetIntervalCreditDuration === 0 ? false : true;
        }
    }
    function onChange(args) {
        // disable or enable
        //    // while changing the dropdown
            var enable = args.itemData.ResetIntervalCreditDuration === 0 ? false : true;

            document.getElementById('CustomYearsNumber')[enable ? 'removeAttribute' : 'setAttribute']('disabled', 'disabled');
            ej.base[enable ? "removeClass" : "addClass"](document.querySelectorAll('#CustomYearsNumber'), 'e-disabled');
            ej.base[enable ? "removeClass" : "addClass"]([document.querySelector('#CustomYearsNumber').closest('span')], ['e-disabled'])
        //
    }
</script> 
But there is an error " Uncaught TypeError: Cannot read property 'removeAttribute' of null "

Q3- Why I Can not edit on the same code above ?
In Controller
        public async Task<ActionResult> Edit([FromBody] CRUDModel<ExcuseConfigurationViewModel> excuseConfigurationViewModel)
        {
               //Code
        }
The error is "Uncaught TypeError: this.getColumnByIndex is not a function"

Thanks very much.



7 Replies 1 reply marked as answer

AH Ahmed April 1, 2021 09:21 AM UTC

Continuous for Q1
In above Code I Can not View the Foreign Key text  , it views as an integer not a text .


MP Manivannan Padmanaban Syncfusion Team April 2, 2021 02:16 PM UTC

Hi Ahmed, 

Greetings from Syncfusion Support. 

Query 1: How to make bind and edit a foreign keys in self-reference tree grid ? && I Cannot View the Foreign Key text 
 
The self-referential data support has the foreign key relationship within them and also by using the EditType and Edit properties of TreeGrid Column, we can render Dropdownlist with external or foreign dataSource. 
Using Fields property of Edit Property object foreignkey fields should be mapped and also we can show the foreign key text on initial rendering using QueryCellInfo event of Tree Grid. Refer to the below code example, 

@{ 
    var DDL_TicketDeduction = new Syncfusion.EJ2.DropDowns.DropDownList() 
    { 
        DataSource = ViewBag.DeductionTypes, 
        Query = "new ej.data.Query()", 
        Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings() 
        { 
            Value = "TaskID", 
            Text = "DeductionName" 
        } 
    }; 
     
  
} 
  
<ejs-treegrid ID="TreeGrid" queryCellInfo="OnQueryCellInfo" ….> 
………………. 
    <e-treegrid-columns> 
        …………….. 
        <e-treegrid-column field="TaskID" headerText="TaskID" width="180"  isPrimaryKey="true" editType="dropdownedit"  
edit="new {@params = DDL_TicketDeduction }"></e-treegrid-column> 
…………………… 
    </e-treegrid-columns> 
</ejs-treegrid> 
  
  
<script> 
 …………………………… 
  
     var ForeignkeyData = @Html.Raw(Json.Serialize(ViewBag.DropData)) // get the ForeignkeyData here. 
  
    function OnQueryCellInfo(args) { 
        if (args.column.field == "TaskID") { 
            for (var i = 0; i < ForeignkeyData.length; i++) { 
                if (args.data.TaskID == ForeignkeyData[i].TaskID) { 
                    args.cell.innerText = ForeignkeyData[i].DeductionName// assign the foreignkey field value to the innertext 
                } 
            } 
  
        } 
    } 
</script>  


Query 2: How to make enable and disable specific field based on dropdown list value changed? 
 
We have achieved your requirement, please refer to the below code example. 


@{    var DDL_TicketDeduction = new Syncfusion.EJ2.DropDowns.DropDownList()    {        DataSource = ViewBag.DeductionTypes,        Query = "new ej.data.Query()",        Change = "onChange",        Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings()        {            Value = "TaskID",            Text = "DeductionName"        }    };………………….. } <ejs-treegrid ID="TreeGrid" ………….    <e-treegrid-columns>……………….        <e-treegrid-column field="TaskID" headerText="TaskID" width="180"  isPrimaryKey="true" editType="dropdownedit" edit="new {@params = DDL_TicketDeduction }"></e-treegrid-column>    </e-treegrid-columns></ejs-treegrid>

function onChange(args) {    // TreeGrid_gridcontrol followed by field name    document.getElementById("TreeGrid_gridcontrolTaskID").setAttribute("disabled"true); // disable the editing}
 
Also like same you can remove the disabled attribute to enable the field. 
 
Query 3: Why I Cannot edit on the same code above ? 
 
We are quite unclear about this query. So, please get back to us with the below details which will help us to resolve the reported issue as early as possible. 

  1. Share issue in detail.
  2. Share the complete stack trace of the error.
  3. Screenshot Or video demonstration  of the issue.

Regards, 
Manivannan Padmanaban 




AH Ahmed April 4, 2021 12:25 PM UTC

Thank you for your answers,
But 
1-  In the second question  "How to make enable and disable specific field based on dropdown list value changed?"
If the field I want to be disable is a numeric field, it still counted up and down although it is disabled. how I can fix this issue.

2-  In the third question " Why I Can not edit on the same code above ?
There is a JavaScript error When I want to edit row in my tree grid  
The error is "Uncaught TypeError: this.getColumnByIndex is not a function"

and My Code Is:

<ejs-treegrid id="TreeGrid"  queryCellInfo="OnQueryCellInfo" allowTextWrap="true" height="400" idMapping="ID" actionFailure="failure" allowSelection="true"  actionBegin="onactionBegin" hasChildMapping="Isparent" parentIdMapping='ParentExcuseConfigurationID' treeColumnIndex="1" toolbar="@(new List<string>() { "Add","Edit", "Delete", "Update", "Cancel" })">
    <e-data-manager url="/ExcuseConfiguration/DataSource" insertUrl="/ExcuseConfiguration/Create" updateUrl="/ExcuseConfiguration/Edit" removeUrl="/ExcuseConfiguration/Delete" adaptor="UrlAdaptor"></e-data-manager>
    <e-treegrid-editsettings allowAdding="true"  allowEditing="true"  allowDeleting="true" mode="Dialog" ></e-treegrid-editsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="ID" headerText="ID" visible="false" isPrimaryKey="true" textAlign="Right" width="70"></e-treegrid-column>
        <e-treegrid-column field="ExcuseName" headerText="@ResourcesWeb.PayrollResource.ExcuseName" editType="stringedit" width="180"></e-treegrid-column>
        <e-treegrid-column field="MaxNumberAllowed" headerText="MaxNumberAllowed" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="TotalNumberAllowed" headerText="TotalNumberAllowed" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="TotalDurationAllowed" headerText="TotalDurationAllowed" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="MaxDurationAllowd" headerText="MaxDurationAllowd" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="MinDurationAllowd" headerText="MinDurationAllowd" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="IntervalTimeOfExcuse" headerText="IntervalTimeOfExcuse" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="ResetIntervalCreditDuration" headerText="ResetIntervalCreditDuration" width="180" editType="dropdownedit" edit="new {@params = DDL_ResetIntervals }"></e-treegrid-column>
        <e-treegrid-column field="CustomYearsNumber" headerText="CustomYearsNumber" editType="numericedit" validationRules="@(new {number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="MinToleranceDaysExcuse" headerText="MinToleranceDaysExcuse" editType="numericedit" validationRules="@(new {required=true , number = true, min = 0 })" width="180"></e-treegrid-column>
        <e-treegrid-column field="DurationType" headerText="DurationType" width="180" editType="dropdownedit" edit="new {@params = DDL_MinEmpDurationType }"></e-treegrid-column>
        <e-treegrid-column field="MinEmployeeDuration" headerText="MinEmployeeDuration" width="180"></e-treegrid-column>
        <e-treegrid-column field="TicketDeductionTypeID" headerText="TicketDeductionType" width="180" editType="dropdownedit" edit="new {@params = DDL_TicketDeduction }"></e-treegrid-column>
        <e-treegrid-column field="ExcuseDurationTypeID" headerText="ExcuseDurationType" width="180" editType="dropdownedit" edit="new {@params = DDL_DurationTypes }"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
In Controller:
 public async Task<ActionResult> Edit([FromBody] CRUDModel<ExcuseConfigurationViewModel> excuseConfigurationViewModel)
        {
          //Code

        }




AH Ahmed April 4, 2021 12:58 PM UTC

And
How to get the value of dropdown list when edit or adding a row in  TreeGrid to check this value ?


PK Padmavathy Kamalanathan Syncfusion Team April 5, 2021 10:49 AM UTC

Hi Ahmed, 
 
Thanks for the update. 
 
Query 1: If the field I want to be disable is a numeric field, it still counted up and down although it is disabled. 
 
We are able to reproduce the reported issue at our end. In order to resolve this issue, we suggest you to follow the below steps, 
  1. In dropdown change event, take the instance of numeric text box
  2. Then set the “enabled” property of numeric text box to false to disable it based on the dropdown value
 
Please check the below code snippet,

 
 
<script> 
    function onChange(args) {  // dropdown change event 
        var changedValue = args.value; // args.value is the changed value of dropdown 
        console.log(changedValue); 
        var numericTextBoxInstance = document.getElementsByClassName("e-numerictextbox")[0].ej2_instances[0];   
         // numericTextBox Instance 
        if (changedValue > 5) { // comparing dropdown value 
            numericTextBoxInstance.enabled = false; // disabling numericTextBox 
        } else { 
            numericTextBoxInstance.enabled = true; // enabling numericTextBox 
        } 
    } 
</script> 
 
Query 2: There is a JavaScript error When I want to edit row in my tree grid. The error is "Uncaught TypeError: this.getColumnByIndex is not a function" 
 
In order to resolve this issue, we suggest you to use the getColumnByIndex method of Grid from Tree Grid. Please check the below code snippet, 
 
 
<ejs-treegrid id="TreeGrid" idMapping="TaskId" parentIdMapping="ParentId" 
              treeColumnIndex="2" actionBegin="begin" actionComplete="complete"> 
</ejs-treegrid> 
 
<script> 
    function begin(args) { 
        if (args.requestType === "beginEdit") { 
            this.grid.getColumnByIndex(2).allowEditing = false; 
            // setting allowEditing to false for particular column using method getColumnByIndex of Grid 
        } 
    } 
</script> 
 
 
Query 3: How to get the value of dropdown list when edit or adding a row in  TreeGrid to check this value ? 
 
On editing/adding, while changing dropdown value, dropdown’s “change” event will be triggered. In that change event, we can access the value of dropdown from “args.value” as shown in the below code, 
 
 
<script> 
    function onChange(args) { // dropdown change event 
        var changedValue = args.value; // args.value is the changed value of dropdown 
        console.log(changedValue); 
    } 
</script> 
 
 
(OR) 
 
You can also access the updated value of dropdown column in the “actionComplete” event with requestType “save” from “args.data.EmployeeID” (EmployeeID is the field name of dropdown column). Please check the below code, 
 
 
<ejs-treegrid id="TreeGrid" idMapping="TaskId" parentIdMapping="ParentId" 
              treeColumnIndex="2" actionBegin="begin" actionComplete="complete"> 
</ejs-treegrid> 
 
<script> 
 
    function complete(args) { 
        if (args.requestType === "save") { 
            var dropDownData = args.data.EmployeeID; 
            // we can access changed value of dropdown in actionComplete event also 
            console.log(dropDownData); 
        } 
    } 
</script> 
 
 
Please check the below API help documentations, 
 
Kindly get back to us for further assistance. 
 
Regards, 
Padmavathy Kamalanathan           


Marked as answer

AH Ahmed June 15, 2021 08:47 AM UTC

Perfect,
Thanks very much for your response



PK Padmavathy Kamalanathan Syncfusion Team June 16, 2021 05:54 AM UTC

Hi Ahmed, 
 
We are happy to hear that you have achieved your requirement. 
 
Please get back to us if you need further assistance. 
 
Regards, 
Padmavathy Kamalanathan 


Loader.
Up arrow icon