Grid ComboBox Restrict Entries to provided list.

I have a grid combo box in my grid control that has an inventory list. I want the user to be able to search by typing text into the input box but I do not want them to be able to enter and save items that are not in the list. How would I do this?

I also need to be able to search on the value or the text. So in the screenshot below the user should be able to search on the number or the description in the string.

For example, right now I can type in 10154 and the item will be found but if I type in Armor nothing happens.


CODE:

@Html.EJS().Grid("ContainerGrid").DataSource(DataManager => { DataManager.Json(@Model.ContainerTransactions.ToArray()).InsertUrl("/Container/AddContainerTransaction").UpdateUrl("/Container/UpdateContainerTransaction").RemoveUrl("/Container/DeleteContainer").Adaptor("RemoteSaveAdaptor"); }).Width("auto").Columns(col =>

{

col.Field("TransactionNo").Visible(false).Add();

col.Field("InventoryID").HeaderText("Item").IsPrimaryKey(true).Width("40").Visible(false).EditType("dropdownedit").Edit(new { create = "create", read = "read", destroy = "destroy", write = "write" }).ValidationRules(new { required = true }).Add();

...

}).ActionBegin("actionBegin").ActionComplete("actionComplete")


<script>

 var elem;

    var inventoryObj;

    var inventoryData = @Html.Raw(Json.Encode(@Model.Inventory.ToArray()));


    function create(args) {


        elem = document.createElement('input');

        return elem;

    }


    function destroy() {

        inventoryObj.destroy();

    }


    function read(args) {

        return inventoryObj.value;

    }


    function write(args) {

        inventoryObj = new ej.dropdowns.ComboBox({

            dataSource: inventoryData,

            placeholder: args.column.headerText,

            value: args.rowData[args.column.field],

            floatLabelType: "Always",

            fields: { text: "CatalogDescription", value: "InventoryID" },

            change: function() {

                description = inventoryObj.text;

            }

        });


        inventoryObj.appendTo(args.element);

    }

</script>


3 Replies

RS Rajapandiyan Settu Syncfusion Team August 17, 2021 10:00 AM UTC

Hi Danyelle, 
 
Thanks for contacting Syncfusion support. 

Query #1: I want the user to be able to search by typing text into the input box but I do not want them to be able to enter and save items that are not in the list. How would I do this? 

Based on your query, you want to search a value and save it only if the value available in the List. You can achieve your requirement by using EJ2 Dropdown Component. In which, you can select a value only from the List. Please refer to the below documentation for more information. 


By setting allowFiltering property as true, you can enable the filtering feature in the Dropdown. By using this, you can search the values in the List. 



@Html.EJS().Grid("Grid").DataSource(dataManager => 
{ 
  dataManager.Json(ViewBag.DataSource1.ToArray()).InsertUrl("/Home/Insert").RemoveUrl("/Home/Remove").UpdateUrl("/Home/Update").Adaptor("RemoteSaveAdaptor"); 
}).Columns(col => 
{ 
    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right). Add(); 
    col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add(); 
    col.Field("ShipCountry").HeaderText("Ship Country").ValidationRules(new { required = "true" }).Edit(new { create = "create", read = "read", destroy = "destroy", write = "write" }).Width("150").Add(); 
 
}).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string> 
    () { "Add", "Edit", "Delete", "Update", "Cancel" }).Render() 
 
 
<script> 
    var elem; 
    var inventoryObj; 
    var inventoryData = @Html.Raw(Json.Encode(ViewBag.DataSource1)); 
 
    function create(args) { 
        elem = document.createElement('input'); 
        return elem; 
    } 
 
    function destroy() {                       
        // destroy the component 
        inventoryObj.destroy(); 
    } 
 
    function read(args) { 
       // return the dropdown value to the Grid 
        return inventoryObj.value; 
    } 
 
    function write(args) { 
        // create the Dropdown component to edit a column 
        inventoryObj = new ej.dropdowns.DropDownList({ 
            dataSource: inventoryData, 
            placeholder: args.column.headerText, 
            value: args.rowData[args.column.field], 
            floatLabelType: "Always", 
            fields: { text: "ShipCountry", value: "ShipCountry" }, 
             // enable the filtering feature 
            allowFiltering: true, 
        }); 
 
        inventoryObj.appendTo(args.element); 
    } 
</script> 



Query #2: I also need to be able to search on the value or the text. So in the screenshot below the user should be able to search on the number or the description in the string. 

You can achieve your requirement by using filtering event of Dropdown. When you start searching a value in the dropdown, the filtering event will be triggered. In that event, you can generate the predicates to perform filtering on multiple columns. Find the below code example for more information. 



    function write(args) { 
        inventoryObj = new ej.dropdowns.DropDownList({ 
            ----- 
            // shown multiple items in the dropdown popup 
            itemTemplate: "<span><span class='OrderID'>${OrderID}</span><span class ='ShipCountry'> - ${ShipCountry}</span></span>", 
            //bind the filtering event handler 
            filtering: (e) => { 
            // perform Searching on ShipCountry and OrderID column                  
                if (e.text == '') { 
                    e.updateData(inventoryData); // load overall data when search key empty. 
                } 
                else { 
                    var dropdown_query = new ej.data.Query(); 
                    // frame the filter query for multiple columns. 
                    dropdown_query.where(new ej.data.Predicate('ShipCountry', 'contains', e.text, true).or('OrderID', 'contains', e.text, true)); 
                    e.updateData(inventoryData, dropdown_query); // execute the customized query 
                } 
            } 
        }); 
        -----         
    } 


Find the below sample for your reference. 

  
Please get back to us if you need further assistance with this. 
 
Regards, 
Rajapandiyan S 



DA Danyelle August 18, 2021 01:17 PM UTC

worked perfectly. Thanks



RS Rajapandiyan Settu Syncfusion Team August 19, 2021 03:57 AM UTC

Hi Danyelle, 
 
We are glad that you have achieved your requirement with the provided solution. 
 
Please get back to us if you need further assistance. 
 
Regards, 
Rajapandiyan S 


Loader.
Up arrow icon