Using Multiselect Dropdown control in Grid and nested data

Good morning,

I have a grid with a remote datasource to data similar to this:


        {
            "id": 143,
            "code": 1,
            "name": "FILOSOFÍA",
            "education_corps": [
                {
                    "id": 1,
                    "code": 511,
                    "name": "CUERPO DE CATEDRÁTICOS DE ENSEÑANZA SECUNDARIA"
                },
                {
                    "id": 2,
                    "code": 590,
                    "name": "CUERPO DE PROFESORES DE ENSEÑANZA SECUNDARIA"
                }
            ]
        },
        {
            "id": 72,
            "code": 1,
            "name": "ALEMÁN",
            "education_corps": [
                {
                    "id": 4,
                    "code": 512,
                    "name": "CUERPO DE CATEDRÁTICOS DE ESCUELAS OFICIALES DE IDIOMAS"
                },
                {
                    "id": 5,
                    "code": 592,
                    "name": "CUERPO DE PROFESORES DE ESCUELAS OFICIALES DE IDIOMAS"
                }
            ]
        },


I have another datasource to feed the elements on multiselect dropdown with this look:


        {
            "id": 1,
            "code": 511,
            "name": "CUERPO DE CATEDRÁTICOS DE ENSEÑANZA SECUNDARIA"
        },
        {
            "id": 4,
            "code": 512,
            "name": "CUERPO DE CATEDRÁTICOS DE ESCUELAS OFICIALES DE IDIOMAS"
        },
        {
            "id": 8,
            "code": 513,
            "name": "CUERPO DE CATEDRÁTICOS DE ARTES PLÁSTICAS Y DISEÑO"
        },
        {
            "id": 2,
            "code": 590,
            "name": "CUERPO DE PROFESORES DE ENSEÑANZA SECUNDARIA"
        },
        {
            "id": 3,
            "code": 591,
            "name": "CUERPO DE PROFESORES TÉCNICOS DE FORMACIÓN PROFESIONAL"
        },


And a Grid:

var grid = new ej.grids.Grid({
      allowPaging: true,
      dataSource: dataEduSpec,
      allowFiltering: true,
      allowSorting: true,
      allowPdfExport: true,
      allowExcelExport: true,
      reorderColumns: ['name'],
      groupSettings: { allowReordering: true},
      editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },
      toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel','Search','PdfExport', 'ExcelExport'],
      rowSelected: rowSelected,
      columns:[
        { field: 'id', visible: false, isPrimaryKey: true },
        { field: 'code', headerText: 'Código', width: 20},
        { field: 'name', headerText: 'Especialidad Educativa', width: 60 },
        { field: 'education_corps', visible: false},
        { field: 'education_corps_name', headerText: 'Cuerpos Educativos', allowSorting: false, allowFiltering: false, width: 100, valueAccessor: valueAccess, edit: {
            create : function(){
              eduCorpElem = document.createElement('input');
              eduCorpElem.value = valueAccess;
              return eduCorpElem;
            },
            read: function(args){
              return eduCorpObj.value;
            },
            destroy: function(){
              eduCorpObj.destroy();
            },
            write: function(args){;
              elementsEduCorpSelected = elementsSelected(args);
              eduCorpObj = new ej.dropdowns.MultiSelect({
                dataSource: dataEduCorp,
                fields: { value: 'id', text: 'name' },
                placeholder: 'Seleccione un cuerpo educativo',
                mode: 'CheckBox',
                showSelectAll: true,
                popupHeight: '300px',
                popupWidth: '480px',
                width: '100%',
                allowFiltering: true,
                change: syncData(args),
                value: Array.isArray(elementsEduCorpSelected)? elementsEduCorpSelected : [elementsEduCorpSelected],
              });
              console.log(elementsEduCorpSelected);
              eduCorpObj.appendTo(eduCorpElem);
            }
          },
        },
      ],
    });
grid.appendTo('#Grid');



function valueAccess(field, data, column){
  //first put names selected on multiselect that are in record          
  let eduCorpNameArray = [];
  for (const [key,value] of Object.entries(data['education_corps'])) {
    eduCorpNameArray.push(value['name']);
  }
  //Put on record the items selected in multiselect
  let eduCorpObjArray = [];
  if((typeof eduCorpObj != 'undefined') && ('education_corps_name' in data)) {
    eduCorpObj.listData.forEach((element) => {
      if(data['education_corps_name'].includes(element['id'])){
        eduCorpObjArray.push(element);
      }
    });
    grid.setCellValue(data['id'],'education_corps',eduCorpObjArray);
  }
  return eduCorpNameArray;
}



I can sync initial display vale on grid, but i don't know how to update cell value when the edit finished.

Any clue?

Thank you



1 Reply

VS Vikram Sundararajan Syncfusion Team July 3, 2023 03:06 PM UTC

Hi Jose,


Greetings from Syncfusion support,


Based on your query In Multiselect dropdown, want to know how to update cell value when the edit finished. We have already discussed in this topic in our documentation. Please refer the below code snippet and sample for more information,


[index.js]

 

function createShipCityFn() {

  ddElem = document.createElement('input');

  return ddElem;

}

function readShipCityFn() {

  return multiSelectObj.value.join(',');

}

function destroyShipCityFn() {

  multiSelectObj.destroy();

}

function writeShipCityFn(args) {

  {

    let multiSelectVal = args.rowData[args.column.field]

      ? args.rowData[args.column.field].split(',')

      : [];

    multiSelectObj = new ej.dropdowns.MultiSelect({

      value: multiSelectVal,

      dataSource: multiselectDatasource,

      fields: { value: 'ShipCity'text: 'ShipCity' },

      floatLabelType: 'Never',

      mode: 'Box',

    });

    multiSelectObj.appendTo(ddElem);

  }

}


Multiselect-dropdown-

https://ej2.syncfusion.com/javascript/documentation/grid/editing/edit-types#render-multiselect-dropdown-component-while-editing


If you require further assistance, please do not hesitate to contact us. We are always here to help you.

Regards,

Vikram S


Loader.
Up arrow icon