Automatically update the autocomplete column based on another autocomplete column edited value Razor pages

Hi,

I'm struggling quite hard to achieve what it says in tile but it seems I have the wrong approach.  What I was trying to do is to follow this example. The main problem is that it looks like that autocomplete does not load new values if I change a parameter in the second autocomplete. 

For example, I have Column Customers and Column Orders. One customer can have many orders but each order belongs to only one customer (foreign key).


<ejs-grid id="PaleteGrid" load="onLoad" showColumnChooser="true" allowPaging="true" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel", "ColumnChooser" })" allowFiltering="true" allowSorting="true" allowPaging="true"})">

    <e-grid-editsettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showDeleteConfirmDialog="true"></e-grid-editsettings>
    <e-data-manager url="Palete/Index?handler=DataSource" insertUrl="Palete/Index?handler=Insert" updateUrl="Palete/Index?handler=Update" removeUrl="Palete/Index?handler=Delete" adaptor="UrlAdaptor"></e-data-manager>
    <e-grid-filterSettings type="Excel"></e-grid-filterSettings>
    <e-grid-pageSettings pageCount="5" pageSize="10"></e-grid-pageSettings>
    <e-grid-columns>
        <e-grid-column field="Id" headerText="ID" visible="true" isidentity="true" isPrimaryKey="true" textAlign="Right" width="120"> </e-grid-column>
        <e-grid-column field="Orders.Customer.Name" headerText="Customer" validationRules="@(new { required=true})" width="140" edit="@(new {create="CustomerCreate", read="CustomerRead", destroy="CustomerDestroy", write="CustomerWrite"})"></e-grid-column>
        <e-grid-column field="Orders.Number" headerText="Orders" width="140" edit="@(new {create="OrdersStevilkaCreate", read="OrdersStevilkaRead", destroy="OrdersStevilkaDestroy", write="OrdersStevilkaWrite"})"></e-grid-column>
    </e-grid-columns>
</ejs-grid>


<script>
var ObjectOrders;
var ObjectCustomer;


var ElementOrders;
var ElementCustomer;


function OrdersStevilkaCreate(args) {
    // create a input element
    ElementOrders = document.createElement('input');
    return ElementOrders;
}


function OrdersStevilkaRead(args) {
    // return the value which will be saved in the grid
    return ObjectOrders.value;
}


function OrdersStevilkaDestroy() {
    // destroy the custom component.
    ObjectOrders.destroy();
}


function OrdersStevilkaWrite(args) {
    //console.log(args);
    //console.log(args.rowData);
    // add multiple parameters in the Query
    var query = new ej.data.Query();
    query.addParams('CustomerID', args.rowData.Orders.CustomerID);
    //query.addParams('OrderID', args.rowData.OrderID)
    // create a autoComplete control
    ObjectOrders = new ej.dropdowns.AutoComplete({
        //bind the data manager instance to dataSource property
        dataSource: new ej.data.DataManager({
            url: 'Palete/Index?handler=AutoCompleteOrders',
            adaptor: new ej.data.UrlAdaptor(),
            headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
            crossDomain: true
        }),
        //bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
        value: args.rowData["Orders"]["Stevilka"],
        //map the appropriate columns to fields property
        fields: { text: 'Orders.Stevilka', value: 'Orders.Stevilka' },
        //set the placeholder to DropDownList input
        placeholder: "Find order",
        //bind query property
        query: query
    });
    //render the component
    ObjectOrders.appendTo(ElementOrders);
}


function CustomerCreate(args) {
    // create a input element
    ElementCustomer = document.createElement('input');
    return ElementCustomer;
}


function CustomerRead(args) {
    // return the value which will be saved in the grid
    return ObjectCustomer.value;
}


function CustomerDestroy() {
    // destroy the custom component.
    ObjectCustomer.destroy();
}


function CustomerWrite(args) {
    // create a autoComplete control
    ObjectCustomer = new ej.dropdowns.AutoComplete({
        //bind the data manager instance to dataSource property
        dataSource: new ej.data.DataManager({
            url: 'Palete/Index?handler=AutoCompleteCustomer',
            adaptor: new ej.data.UrlAdaptor(),
            headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
            crossDomain: true
        }),
        //bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
        value: args.rowData["Orders"]["Customer"]["Naziv"],
        //map the appropriate columns to fields property
        fields: { text: 'Orders', value: 'Customer' },
        //set the placeholder to DropDownList input
        placeholder: "Find Customer",
        change: (e) => {
            console.log(e.value);
            var formEle = document.getElementById("PaleteGrid").querySelector("form").ej2_instances[0];
            var CustomerEle = formEle.getInputElement('Orders___Customer___Name');
            var OrdersEle = formEle.getInputElement('Orders___Number');
            OrdersEle.value = "";
            CustomerEle.value = e.value;
        }
    });
    //render the component
    ObjectCustomer.appendTo(ElementCustomer);


</script>



What am I doing wrong?


11 Replies 1 reply marked as answer

RS Rajapandiyan Settu Syncfusion Team January 19, 2022 08:48 AM UTC

Hi Miha, 
 
Thanks for contacting Syncfusion support. 

You can change the value of autoComplete, by directly using its instance variable inside the change event. 


function CustomerWrite(args) { 
    // create a autoComplete control 
    ObjectCustomer = new ej.dropdowns.AutoComplete({ 
        --- 
        change: (e) => { 
            console.log(e.value); 
            ObjectOrders.value = "dynamic text"; // change the value of AutoComplete component using its instance 
        } 
    }); 
    //render the component 
    ObjectCustomer.appendTo(ElementCustomer); 
} 


Or, you can change the value by getting the input element using its id. 


function CustomerWrite(args) { 
    // create a autoComplete control 
    ObjectCustomer = new ej.dropdowns.AutoComplete({ 
        ---- 
        change: (e) => {  
            var formEle = document.getElementById("PaleteGrid").querySelector("form").ej2_instances[0]; 
            // get the input element           
            var CustomerEle = formEle.element.querySelector('#' + grid.element.id + 'Orders___Customer___Name'); 
            CustomerEle.value = "" // change the input element value 
        } 
    }); 
    //render the component 
    ObjectCustomer.appendTo(ElementCustomer); 
} 


Please get back to us if you need further assistance with this. 

Regards, 
Rajapandiyan S 



MI Miha January 22, 2022 03:45 PM UTC

Hi Rajapandiyan,


thank you for your help, I didn't know it was so easy. The end solution is that I needed to change the value of the additional parameter that I send to the server-side.

So the easiest way is to do this:


change: (e) => {
            query.params[0].value = ElementDobavitelj.value;
        },


If anyone will be interested in the whole solution you can find it bellow:


var ObjectDobavnica;
var ObjectDobavitelj;


var ElementDobavnica;
var ElementDobavitelj;


function dobavnicaStevilkaCreate(args) {
    // create a input element
    ElementDobavnica = document.createElement('input');
    return ElementDobavnica;
}


function dobavnicaStevilkaRead(args) {
    // return the value which will be saved in the grid
    return ObjectDobavnica.value;
}


function dobavnicaStevilkaDestroy() {
    // destroy the custom component.
    ObjectDobavnica.destroy();
}


function dobavnicaStevilkaWrite(args) {
    //console.log(args);
    //console.log(args.rowData);
    // add multiple parameters in the Query
    var query = new ej.data.Query();
    query.addParams('DobaviteljNaziv', args.rowData.Dobavnica.Dobavitelj.Naziv);
    //query.addParams('OrderID', args.rowData.OrderID)
    // create a autoComplete control
    ObjectDobavnica = new ej.dropdowns.AutoComplete({
        //bind the data manager instance to dataSource property
        dataSource: new ej.data.DataManager({
            url: 'Palete/Index?handler=AutoCompleteDobavnica',
            adaptor: new ej.data.UrlAdaptor(),
            headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
            crossDomain: true
        }),
        //bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
        value: args.rowData["Dobavnica"]["Stevilka"],
        //map the appropriate columns to fields property
        fields: { text: 'Dobavnica.Stevilka', value: 'Dobavnica.Stevilka' },
        //set the placeholder to DropDownList input
        placeholder: "Najdi dobavnico",
        change: (e) => {
            query.params[0].value = ElementDobavitelj.value;
        },
        //bind query property
        query: query
    });
    //render the component
    ObjectDobavnica.appendTo(ElementDobavnica);
}


function dobaviteljCreate(args) {
    // create a input element
    ElementDobavitelj = document.createElement('input');
    return ElementDobavitelj;
}


function dobaviteljRead(args) {
    // return the value which will be saved in the grid
    return ObjectDobavitelj.value;
}


function dobaviteljDestroy() {
    // destroy the custom component.
    ObjectDobavitelj.destroy();
}


function dobaviteljWrite(args) {
    // create a autoComplete control
    ObjectDobavitelj = new ej.dropdowns.AutoComplete({
        //bind the data manager instance to dataSource property
        dataSource: new ej.data.DataManager({
            url: 'Palete/Index?handler=AutoCompleteDobavitelj',
            adaptor: new ej.data.UrlAdaptor(),
            headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
            crossDomain: true
        }),
        //bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
        value: args.rowData["Dobavnica"]["Dobavitelj"]["Naziv"],
        //map the appropriate columns to fields property
        fields: { text: 'Dobavnica', value: 'Dobavitelj' },
        //set the placeholder to DropDownList input
        placeholder: "Najdi dobavitelja",
        change: (e) => {
            ObjectDobavnica.value = "";
        }
    });
    //render the component
    ObjectDobavitelj.appendTo(ElementDobavitelj);
}

Basically what is done is that every time we change the value Dobavnica (delivery note), we send to the serverside current value of the field Dobavitelj (Supplier).


I was wondering if there is any documentation available regarding handling the frontside code?


Best Regards, Miha



RS Rajapandiyan Settu Syncfusion Team January 24, 2022 05:01 AM UTC

Hi Miha, 
 
We are glad that you have achieved your requirement from your end. 

By referring below documentation, you can change the dropdown value based on another dropdown action (cascading dropdown). 


Please get back to us if you need further assistance. 

Regards, 
Rajapandiyan S 



MI Miha January 24, 2022 11:02 PM UTC

Hi Rajapandiyan,


thank you very much for the link. This is very useful. 

What I have figured out is that my solution is working only partly. If I have the name of the supplier already input, then I can autocomplete the delivery notes. Now the question which pops up is what if I don't have the Supplier field already filled, can I just choose the supplier based on the delivery note I have selected? I think this is called autofill (where you fill other columns based on the value you have selected)

I think this could be solved in two ways, but just don't have a clue on how to implement them:

  1. I return from the server-side JSON, with data about the delivery note and its supplier. In this case, I have to bind the data to different fields in the data grid.
  2. When I select the delivery note I lunch a query that will search for the Supplier in the database.

I'm not sure if the above-mentioned is possible and how to implement it. Can you please tell me if I'm on right track and how should I implement it?

Best Regards


RS Rajapandiyan Settu Syncfusion Team January 25, 2022 04:28 PM UTC

Hi Miha, 
 
Thanks for your update. 

We are unclear about your requirement at our end. Is your requirement is to bind some default value on Supplier field when it is undefined? If so, you can bind default value in the write method of edit template. 


[index.cshtml] 

function OrdersStevilkaWrite(args) {
    ----
    // create a autoComplete control
    ObjectOrders = new ej.dropdowns.AutoComplete({
        ----
        value: args.rowData["Supplier "] == undefined ? “defaultText” : args.rowData["Supplier "],
        ----
    });
    //render the component
    ObjectOrders.appendTo(ElementOrders);
} 


If this not your requirement, Kindly share below details to validate it further. 

  1. Explain your requirement in video demo.
  2. Share the details of Supplier and DeliveryNote columns. Share complete code details.
  3. If possible share the simple issue reproducible sample.

Regards, 
Rajapandiyan S 



MI Miha January 26, 2022 05:51 PM UTC

Hello Rajapandiyan,


maybe I should ask in a different way.

Is it possible to lunch a server-side query, from autocomplete change event and then bind its data to field values of column grid?


The idea is as I mentioned if I change the value of field "Dobavitelj":



Field "Dobavnica" gets cleared (this is working)



Then I'm able to select only from Dobavnica (Delivery note), which belongs to selected Dobavitelj (Customer). This is also working:



Now the question is: If I clear the field "Dobavtelj". Then I am able to choose from all delivery notes (this is working), but then when I select the "Dobavnica" (Delivery note). I want to update Dobavitelj (Customer) field with the Customer name to which the delivery note belongs.




Like I said: One Customer can have multiple delivery notes, but the delivery note can have only one customer.

So how to solve this?


Currently, I have this code on the front side:





var ObjectDobavnica;
var ObjectDobavitelj;
var ObjectMaterial;


var ElementDobavnica;
var ElementDobavitelj;
var ElementMaterial;


//create query
var queryDobavnica;
var queryDobavitelj;


function dobavnicaStevilkaCreate(args) {
// create a input element
ElementDobavnica = document.createElement('input');
return ElementDobavnica;
}


function dobavnicaStevilkaRead(args) {
// return the value which will be saved in the grid
return ObjectDobavnica.value;
}


function dobavnicaStevilkaDestroy() {
// destroy the custom component.
ObjectDobavnica.destroy();
}


function dobavnicaStevilkaWrite(args) {
// add multiple parameters in the Query
var dobaviteljNaziv;
if (args.rowData.Dobavnica.Dobavitelj.Naziv !== undefined)
dobaviteljNaziv = args.rowData.Dobavnica.Dobavitelj.Naziv;
queryDobavnica = new ej.data.Query();
queryDobavnica.addParams('DobaviteljNaziv', dobaviteljNaziv);
// create a autoComplete control
ObjectDobavnica = new ej.dropdowns.AutoComplete({
//bind the data manager instance to dataSource property
dataSource: new ej.data.DataManager({
url: 'Palete/Index?handler=AutoCompleteDobavnica',
adaptor: new ej.data.UrlAdaptor(),
headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
crossDomain: true
}),
//bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
value: args.rowData["Dobavnica"]["Stevilka"],
//map the appropriate columns to fields property
fields: { text: 'Dobavnica.Stevilka', value: 'Dobavnica.Stevilka' },
//set the placeholder to DropDownList input
placeholder: "Najdi dobavnico",
change: (e) => {
if (ObjectDobavitelj.value === null && ObjectDobavnica.value != null) {
queryDobavitelj.params[0].value = ObjectDobavnica.value;
}
},
//bind query property
query: queryDobavnica
});
//render the component
ObjectDobavnica.appendTo(ElementDobavnica);
}


function dobaviteljCreate(args) {
// create a input element
ElementDobavitelj = document.createElement('input');
return ElementDobavitelj;
}


function dobaviteljRead(args) {
// return the value which will be saved in the grid
return ObjectDobavitelj.value;
}


function dobaviteljDestroy() {
// destroy the custom component.
ObjectDobavitelj.destroy();
}


function dobaviteljWrite(args) {
var dobavnicaStevilka;
if (args.rowData.Dobavnica.Stevilka !== undefined)
dobavnicaStevilka = args.rowData.dobavnicaStevilka;
queryDobavitelj = new ej.data.Query();
queryDobavitelj.addParams('DobavnicaStevilka', dobavnicaStevilka);
// create a autoComplete control
ObjectDobavitelj = new ej.dropdowns.AutoComplete({
//bind the data manager instance to dataSource property
dataSource: new ej.data.DataManager({
url: 'Palete/Index?handler=AutoCompleteDobavitelj',
adaptor: new ej.data.UrlAdaptor(),
headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
crossDomain: true
}),
//bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
value: args.rowData["Dobavnica"]["Dobavitelj"]["Naziv"],
//map the appropriate columns to fields property
fields: { text: 'Dobavnica', value: 'Dobavitelj' },
//set the placeholder to DropDownList input
placeholder: "Najdi dobavitelja",
change: (e) => {
if (ObjectDobavitelj.value !== null) {
if (queryDobavnica.params[0].value !== ObjectDobavnica.value) {
ObjectDobavnica.value = null;
}
}
queryDobavnica.params[0].value = ElementDobavitelj.value;
},
//bind query property
query: queryDobavitelj
});
//render the component
ObjectDobavitelj.appendTo(ElementDobavitelj);
}


function materialCreate(args) {
// create a input element
ElementMaterial = document.createElement('input');
return ElementMaterial;
}


function materialRead(args) {
// return the value which will be saved in the grid
return ObjectMaterial.value;
}


function materialDestroy() {
// destroy the custom component.
ObjectMaterial.destroy();
}


function materialWrite(args) {
// create a autoComplete control
ObjectMaterial = new ej.dropdowns.AutoComplete({
//bind the data manager instance to dataSource property
dataSource: new ej.data.DataManager({
url: 'Palete/Index?handler=AutoCompleteMaterial',
adaptor: new ej.data.UrlAdaptor(),
headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
crossDomain: true
}),
//bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
value: args.rowData["Tip"],
//map the appropriate columns to fields property
fields: { text: 'Tip', value: 'Tip' },
//set the placeholder to DropDownList input
placeholder: "Najdi material",
});
//render the component
ObjectMaterial.appendTo(ElementMaterial);
}


RS Rajapandiyan Settu Syncfusion Team January 27, 2022 01:38 PM UTC

Hi Miha, 

Thanks for your update. 

Based on your requirement, you want to select the value of Dobavncia column based on the Dobavitelj column’s selected value which described below,

1. First, we need to select a value in Dobavitelj field. 
2. Then Dobavncia field’s dataSource is filtered based on the selected value of Dobavitelj field. 
3. Now we can select any one of the filtered value. 

This is the behavior of cascading autoComplete component. 

But with this requirement, you want to do reverse action like if we select the value in Dobavncia field, you want to update the Dobavitelj field when it is undefined. Since this is not the valid case. Because all the actions are performed based on one column. We are not supposed to do vice versa. 

For this case, we need to bind validationRules on Dobavitelj field and disable the Dobavncia field. Like in below screenshot. 

Screenshot: Disable the Dobavncia column if Dobavitelj has empty value.
 



    function countryWrite(args) { 
        countryObj = new ej.dropdowns.AutoComplete({ 
                        dataSource: data, 
                        fields: { value: 'ShipCountry', text: 'ShipCountry' }, 
                        change: function(){ 
                            if(countryObj.value == "" || countryObj.value == null){ 
                                stateObj.enabled = false; // disable the cascade field if have empty value 
                            } 
                            --- 
                        }, 
                        filtering: function(args){ 
                            if(args.text == ""){ 
                                stateObj.enabled = false; 
                            } 
                        }, 
                    }); 
                    countryObj.appendTo(args.element); 
    } 


Note: You can also achieve your requirement using Dropdown component with Filtering feature if you want to save the data only from its dataSource. 

Regards, 
Rajapandiyan S 



MI Miha January 27, 2022 05:57 PM UTC

Hi Rajapandiyan,


I can't completely agree, that selecting from the list of Dobavnica while Dobavitelj is undefined it's an invalid case. This should be a valid case because each Dobavnica belongs to exactly one Dobavitelj.


I had got this working using jQuery you can see the gif animation of the working project attached.


bellow is the jQuery code that does this:


$(document).ready(function () {
    $('#DobaviteljNaziv').autocomplete({
        autoFocus: true,
        source: function (request, response) {
            $.ajax({
                url: '/Palete/Create?handler=AutoCompleteDobavitelj',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("XSRF-TOKEN",
                        $('input:hidden[name="__RequestVerificationToken"]').val());
                },
                data: { "prefix": request.term },
                type: "POST",
                success: function (data) {
                    response($.map(data, function (item) {
                        return item;
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        select: function (event, ui) {
            (document.getElementById("DobavnicaStevilka").value = "");
        }
    });
    $('#DobavnicaStevilka').autocomplete({
        autoFocus: true,
        source: function (request, response) {
            $.ajax({
                url: '/Palete/Create?handler=AutoCompleteDobavnica',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("XSRF-TOKEN",
                        $('input:hidden[name="__RequestVerificationToken"]').val());
                },
                data: { "prefix": request.term, "DobaviteljNaziv": DobaviteljNaziv.value },
                type: "POST",
                success: function (data) {
                    response($.map(data, function (item) {
                        return item;
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        select: function (event, ui) {
            if (document.getElementById("DobaviteljNaziv").value == "") {
                var $form = $('form');
                var data = {
                    'dobavnicaSt': ''
                };
                data.dobavnicaSt = ui.item.label;
                data = $form.serialize() + '&' + $.param(data);
                $.post('/Palete/Create?handler=DobavnicaSelect', data, function (msg) {
                    document.getElementById("DobaviteljNaziv").value = msg;
                });
            }
        }
    });
});


The main part of the code which solve this case is here:


select: function (event, ui) {
            if (document.getElementById("DobaviteljNaziv").value == "") {
                var $form = $('form');
                var data = {
                    'dobavnicaSt': ''
                };
                data.dobavnicaSt = ui.item.label;
                data = $form.serialize() + '&' + $.param(data);
                $.post('/Palete/Create?handler=DobavnicaSelect', data, function (msg) {
                    document.getElementById("DobaviteljNaziv").value = msg;
                });
            }
        }


This means that when I select Dobavnica (in your case state), I fire a new request to the serverside, to search for Dobavitelj, to which Dobavnica belongs. 

I do not know if something like this is possible to do with your grid?



Attachment: Autofill_587d5925.zip


RS Rajapandiyan Settu Syncfusion Team January 28, 2022 10:50 AM UTC

Hi Miha, 

Thanks for your update. 

Based on your requirement, If we select a value in Dobavncia field, you want to update the Dobavitelj field when it is undefined. You can achieve it by using change event of Dobavncia field with flag variable. 


    function countryWrite(args) { 
        countryObj = new ej.dropdowns.AutoComplete({ 
                        ---- 
                        change: function(args){ 
                            // change the stateObject query based on flag variable 
                            if(countrychangedbystate){ 
                                countrychangedbystate = false; // make the flag variable as false 
                            } else { 
                               --- 
                                 // change the StateObj query 
                                var tempQuery = new ej.data.Query().where('ShipCountry', 'equal', countryObj.value); 
                                stateObj.query = tempQuery; 
                                stateObj.text = null; 
                                stateObj.dataBind(); 
                            } 
                        }, 
                        --- 
                    }); 
    } 

    function stateWrite(args) { 
        --- 
        stateObj = new ej.dropdowns.AutoComplete({ 
                    --- 
                    change: function (args){ 
                        if(countryObj.value == null){ 
                            countrychangedbystate = true; // make the flag variable true 
                            // change the countryObject value when it is null 
                            countryObj.element.value = args.itemData.ShipCountry;  // get the shipCountry value from autoComplete itemData 
                            countryObj.value = args.itemData.ShipCountry; 
                        } 
                    }, 
                    --- 
                    }); 
    } 



Please get back to us if you need further assistance with this. 

Regards, 
Rajapandiyan S 


Marked as answer

MI Miha January 31, 2022 03:32 PM UTC

Hi Rajapandiyan,


thank you the solution is working perfectly. If anyone needs the code there is a final code below:


front end:

var ObjectDobavnica;
var ObjectDobavitelj;
var ObjectMaterial;


var dobaviteljChangedByDobavnica = false;


function dobavnicaStevilkaCreate(args) {
    // create a input element
    var ElementDobavnica = document.createElement('input');
    return ElementDobavnica;
}


function dobavnicaStevilkaRead(args) {
    // return the value which will be saved in the grid
    return ObjectDobavnica.value;
}


function dobavnicaStevilkaDestroy() {
    // destroy the custom component.
    ObjectDobavnica.destroy();
}


function dobavnicaStevilkaWrite(args) {
    // add multiple parameters in the Query
    var query = new ej.data.Query();
    query.addParams('Dobavnica.Dobavitelj.Naziv', args.rowData.Dobavnica.Dobavitelj.Naziv);
    query.addParams('Dobavnica.Stevilka', args.rowData.Dobavnica.Stevilka);
    query.where('Dobavitelj.Naziv', 'equal', args.rowData["Dobavnica"]["Dobavitelj"]["Naziv"]);
    // create a autoComplete control
    ObjectDobavnica = new ej.dropdowns.AutoComplete({
        //bind the data manager instance to dataSource property
        dataSource: new ej.data.DataManager({
            url: 'Palete/Index?handler=AutoCompleteDobavnica',
            adaptor: new ej.data.UrlAdaptor(),
            headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
            crossDomain: true
        }),
        //bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
        value: args.rowData["Dobavnica"]["Stevilka"],
        //map the appropriate columns to fields property
        fields: { text: 'Stevilka', value: 'Stevilka' },
        //set the placeholder to DropDownList input
        placeholder: "Najdi dobavnico",
        floatLabelType: 'Never',
        enabled: args.rowData["Dobavnica"]["Stevilka"] != undefined,
        change: (e) => {
            if (ObjectDobavitelj.value == null) {
                dobaviteljChangedByDobavnica = true;
                ObjectDobavitelj.element.value = e.itemData.Dobavitelj.Naziv;
                ObjectDobavitelj.value = e.itemData.Dobavitelj.Naziv;
            }
        }
    });
    //render the component
    ObjectDobavnica.appendTo(args.element);
}


function dobaviteljCreate(args) {
    // create a input element
    var ElementDobavitelj = document.createElement('input');
    return ElementDobavitelj;
}


function dobaviteljRead(args) {
    // return the value which will be saved in the grid
    return ObjectDobavitelj.value;
}


function dobaviteljDestroy() {
    // destroy the custom component.
    ObjectDobavitelj.destroy();
}


function dobaviteljWrite(args) {
    // create a autoComplete control
    ObjectDobavitelj = new ej.dropdowns.AutoComplete({
        //bind the data manager instance to dataSource property
        dataSource: new ej.data.DataManager({
            url: 'Palete/Index?handler=AutoCompleteDobavitelj',
            adaptor: new ej.data.UrlAdaptor(),
            headers: [{ 'XSRF-TOKEN': $("input:hidden[name=__RequestVerificationToken]").val() }],
            crossDomain: true
        }),
        //bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value
        value: args.rowData["Dobavnica"]["Dobavitelj"]["Naziv"],
        //map the appropriate columns to fields property
        fields: { text: 'Dobavnica', value: 'Dobavitelj' },
        //set the placeholder to DropDownList input
        placeholder: "Najdi dobavitelja",
        change: (e) => {
            if (dobaviteljChangedByDobavnica) {
                dobaviteljChangedByDobavnica = false;
            }
            else {
                if (ObjectDobavitelj.value == "" || ObjectDobavitelj.value == null) {
                    var tempQuery = new ej.data.Query();
                    ObjectDobavnica.query = tempQuery;
                    ObjectDobavnica.dataBind();
                }
                else {
                    var tempQuery = new ej.data.Query().where('Dobavnica.Dobavitelj.Naziv', 'Equal', ObjectDobavitelj.value)
                    ObjectDobavnica.query = tempQuery;
                    ObjectDobavnica.text = null;
                    ObjectDobavnica.dataBind();
                }
            }
        }
    });
    //render the component
    ObjectDobavitelj.appendTo(args.element);
}


server-side:

        public async Task<JsonResult> OnPostAutoCompleteDobavnicaAsync([FromBody] Data dm)
        {
            var Data = await _context.Dobavnica
                            .Include(d => d.Dobavitelj)
                            .Take(dm.take)
                            .OrderByDescending(d => d.Datum)
                            .ToListAsync();


            if (dm.where != null)
            {
                if (dm.where.Count > 1 && dm.@where[1].value != null)
                {
                    if(dm.where[0].field == "Dobavnica.Dobavitelj.Naziv" && dm.where[1].field == "Stevilka")
                    {
                        Data = await _context.Dobavnica
                            .Include(d => d.Dobavitelj)
                            .Where(d => d.Dobavitelj.Naziv.Contains(dm.@where[0].value.ToString().ToLower()) && d.Stevilka.Contains(dm.@where[1].value.ToString().ToLower()))
                            .Take(dm.take)
                            .OrderByDescending(d => d.Datum)
                            .ToListAsync();
                    }
                }
                else if(dm.@where[0].value != null)
                {
                    if(dm.where[0].field == "Stevilka")
                    {
                        Data = await _context.Dobavnica
                            .Include(d => d.Dobavitelj)
                            .Where(d => d.Stevilka.Contains(dm.@where[0].value.ToString().ToLower()))
                            .Take(dm.take)
                            .OrderByDescending(d => d.Datum)
                            .ToListAsync();
                    }
                }
            }
            return new JsonResult(Data);
        }

I was also wondering if you could explain this code please:

    // add multiple parameters in the Query
    var query = new ej.data.Query();
    query.addParams('Dobavnica.Dobavitelj.Naziv', args.rowData.Dobavnica.Dobavitelj.Naziv);
    query.addParams('Dobavnica.Stevilka', args.rowData.Dobavnica.Stevilka);
    query.where('Dobavitelj.Naziv', 'equal', args.rowData["Dobavnica"]["Dobavitelj"]["Naziv"]);

Thank you again.



RS Rajapandiyan Settu Syncfusion Team February 1, 2022 10:13 AM UTC

Hi Miha, 
 
We are glad that you have achieved your requirement with the provided solution. 

If you want to send additional parameters to server side, you can achieve this by using query.addParams method. 


Please get back to us if you need further assistance. 

Regards, 
Rajapandiyan S 


Loader.
Up arrow icon