Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
75
How to add custom editor provider in mvc for combo with multi checkbox
posted

Hi,

am using  ig 17.1 version.

I have to create comboeditor with multiselect option in mvc.For that i need to create a custom editorprovider.

How to create new custom editorprovider in mvc. Can you please provide some sample code?

Parents
  • 17590
    Offline posted

    Hello George,

    Setting custom editor provider in MVC scenario can be achieved as following:

    1) Create custom editor provider that extends the default EditorProviderCombo. Some further reference about working with combo editor provider is available here.
    2) Set the editorProvider option in the MVC Wrapper along with its editorOptions.

    For example:

    <body>
        @{
            @(Html.Infragistics().Grid(Model)
                .ID("grid")
                .Height("300px")
                .Width("100%")
                .PrimaryKey("Id")
                .AutoCommit(true)
                .Regional("bg")
                .Language("bg")
                .Columns(column =>
                {
                    column.For(x => x.Id).Width("60px");
                    column.For(x => x.StartTime).Width("100px");
                    column.For(x => x.Description);
                    column.For(x => x.Percentage);
                    column.For(x => x.Amount).DataType("number");
                })
                .Features(f =>
                {
                f.Updating()
                .EnableAddRow(false)
                .EditMode(GridEditMode.Row)
                .ColumnSettings(cs =>
                {
                            cs.ColumnSetting().ColumnKey("Id").ReadOnly(true);
                            cs.ColumnSetting().ColumnKey("StartTime").EditorType(ColumnEditorType.DatePicker);
    						cs.ColumnSetting().ColumnKey("Description").EditorProvider("new $.ig.ComboEditorProviderCustom()").EditorOptions("mode: 'dropdown', dataSource: northWindCategoriesJSON, textKey: 'Name', valueKey: 'Name',allowCustomValue: true,multiSelection: {enabled: true,showCheckboxes: true,itemSeparator: ', '}");
    			});
        .DataBind()
        .Render();
        }
        <script>
            $(function () {
                northWindCategoriesJSON = [
                    { "ID": 0, "Name": "Food" },
                    { "ID": 1, "Name": "Beverages" },
                    { "ID": 2, "Name": "Electronics" }
                ]; 
                $.ig.ComboEditorProviderCustom = $.ig.EditorProviderCombo.extend({
                    getValue: function () {
                        var val = this.editor.value();
                        var text = this.editor.text();
                        if ($.type(val) === "array" && val.length) {
                            //When the passed value is of complex type return the text instead.
                            //This will be the value saved in the grid data source after editing ends.
                            return text;
                        }
                        return val;
                    },
                    setValue: function (val, fire) {
                        var array = [];
                        this.editor.deselectAll();
                        if (this.options.multiSelection.enabled && val.contains(this.options.multiSelection.itemSeparator)) {
                            //if multiSelection is enabled and the value passed from the grid cell to the edito contains the specified itemSeparator
                            //Then split the value by the separator and set a complex value back to the editor so that the matching items will get selected.
                            array = val.split(this.options.multiSelection.itemSeparator);
                            return this.editor.value(array, null, fire);
                        }
                        this.editor.value(val, null, fire);
                    }
                });
            });
        </script>
    </body>
    </html>

    Please test it on your side and let me know if you have any additional questions regarding this matter

Reply
  • 17590
    Offline posted in reply to george

    Hello George,

    The reason why an exception is thrown is that the multiple selected values are combined to a string, where different values are separated with the character set to the itemSeparator option of the combo editor, which is ", " by default. This is required because by design igGrid does not support complex values in the underlying data source.  To support such scenario a custom implementation is needed for the getValue/setValue methods of the igCombo provider  to handle the complex data (in the form of array) passed from the combo multiple selection.

    In your scenario if you select PC1 and PC2 they will be represented in the grid as "PC1,PC2" and this is going to be the val that you will be evaluating in the formatter function. My suggestion for applying formatting  is separating values in an array, using split method and loop trough them. This will give you an array of substrings which you can itterate. 

     function formatProduct(val) {
     //when val is string, coming from a multiple selection, it will look like to following: "sel1,sel2,sel3"
     //If you want to check every single value you can create an array with substrings containing separate values
            var i, product,
                   valsArray = val.split(",");
         // apply your logic here and return the corresponding value
      
            return val;
        }

    Please keep in mind that formatter function is going to be applied initially when the grid is loaded and this scenario should also be handled. 

    Please let me know if you need any further assistance with this matter,

Children