We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Set grid cell text in batch edit mode dynamically.

Thread ID:

Created:

Updated:

Platform:

Replies:

128219 Jan 9,2017 12:25 PM UTC Jan 11,2017 11:53 AM UTC ASP.NET MVC 3
loading
Tags: Grid
sachin
Asked On January 9, 2017 12:25 PM UTC

Hello,


I am using the grid with batch edit as suggested by your support team in order to save bulk grid data to database but i am stuck with other problem where i need to set grid cell value. When user clicks on cell to edit it , i am showing a popup model containing another syncfusion grid to let the user select values from the grid. In inline edit mode this functionality was working fine, i was able to get the selected value from the popup grid and assign that value to the selected grid cell by using the id of the cell (e.g. -  $("#" + this._id +"departmentCode").val("value") ) this method is not working with batch edit mode. Please help me how to get the selected value from a popup grid  and assign it to the main grid cell. this is my main grid 

   @(Html.EJ().Grid<EducationDepartmentModel>("FlatGrid")
            .Datasource(ds => ds.URL(Url.Action("EduDeptDataSource", "CRUD", null, Request.Url.Scheme)).BatchURL(Url.Action("EduDeptBatchUpdate", "CRUD", null, Request.Url.Scheme)).Adaptor(AdaptorType.UrlAdaptor))
            .EnableAltRow()
            .AllowFiltering()
            .SelectedRowIndex(0)
            .IsResponsive()
            .AllowSorting()
            .GridLines(GridLines.Horizontal)
            .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.Batch); })
            .PageSettings(page =>
            {
                page.PageSize(5);
            })
            .AllowTextWrap()
            .AllowResizeToFit()
            .EnableHeaderHover()
            .FilterSettings(filter => { filter.FilterType(FilterType.Excel); })
            .AllowPaging()
            .ClientSideEvents(eve =>
            {
                eve.ActionComplete("complete");
                eve.ActionFailure("failure");

                eve.RecordClick("recordClick");
                eve.RecordDoubleClick("recordDoubleClick");
                eve.ContextOpen("contextopen");
                eve.ContextClick("contextclick");
                eve.ActionBegin("beginAction");          
                eve.BeforeBatchAdd("beforeBatchAdd");
                eve.CellEdit("cellEdit");
              

                

            })
            .Columns(col =>
            {
                col.Field(p => p.departmentId).HeaderText("Department ID").IsPrimaryKey(true).Visible(false).TextAlign(TextAlign.Left).Width(70).Add();
                col.Field(p => p.educationId).HeaderText("Department ID").Visible(false).TextAlign(TextAlign.Left).Width(70).Add();

                col.Field(p => p.departmentCode).HeaderText("Department Code").TextAlign(TextAlign.Left).ValidationRules(v => v.AddRule("required", true).AddRule("number", true)).Width(70).Add();
                col.Field(p => p.description).HeaderText("Description").Width(90).ValidationRules(v => v.AddRule("required", true).AddRule("maxlength", 40)).Add();
                col.Field(p => p.budgetAmount).HeaderText("Budget Amount").EditType(EditingType.Numeric).Format("{0:C}").NumericEditOptions(new EditorProperties() { DecimalPlaces = 2 }).ValidationRules(v => v.AddRule("range", "[0,1000]")).Add();

                col.Field(p => p.costIncurred).HeaderText("Cost Incurred").EditType(EditingType.Numeric).Format("{0:C}").NumericEditOptions(new EditorProperties() { DecimalPlaces = 2 }).ValidationRules(v => v.AddRule("range", "[0,1000]")).Add();



            })
                )


To open the popup dialog box , i am using this code -
 
function cellEdit(args){                  

            
            if(args.columnName == "description"){
                args.cancel = true;           


            }

            if(args.columnName == "departmentCode"){
                $("#helpDialog").ejDialog("open");

            }

        }

and this is my popup grid model -
 

@{Html.EJ().Dialog("helpDialog").Title("Cost Center Code").IsResponsive(true).ShowOnInit(false).ContentTemplate(@<div>
        <form id="form1">
            @(Html.EJ().Grid<DepartmentModel>("FlatGrid2").Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dataSource2))
                    .AllowSelection()
                    .AllowTextWrap()
                    .EnableHeaderHover()
                    .EnableAltRow()

                    .AllowPaging()
                    .ToolbarSettings(toolbar =>
                    {
                        toolbar.ShowToolbar().ToolbarItems(items =>
                        {
                            items.AddTool(ToolBarItems.Search);
                        });
                    })
                    .PageSettings(page =>
                    {
                        page.PageSize(10);
                    })
                    .ClientSideEvents(eve =>
                    {

                        eve.RecordDoubleClick("select");


                    })
                    .Columns(col =>
                    {
                        col.Field(p => p.departmentCode).HeaderText("Department Code").IsPrimaryKey(true).Width(50).TextAlign(TextAlign.Left).Add();

                        col.Field(p => p.description).HeaderText("Department Description").TextAlign(TextAlign.Left).Width(90).Add();

                    })
            )


        </form>
    </div>).EnableModal(true).EnableResize(false).ClientSideEvents(evt => evt.Close("onDialogClose")).IsResponsive(true).Render();}

as you can see , i have added recordDoubleClick event to get the selected row value-

function select(args) {
            var result = this.getSelectedRecords();
            var costCode = result[0].departmentCode;
            var costDesc = result[0].description;

            $("#helpDialog").ejDialog("close");

            alert(args.value);
            var gridInstance = $("#FlatGrid").ejGrid("instance");

            var index = gridInstance.selectedRowsIndexes;        



            gridInstance.setCellText(index, "departmentCode", costCode);
            gridInstance.setCellText(index, "description", costDesc);


        }

when i use this code ,instead of setting the selected value to the cell , it creates a new textbox and set the selected value to it . Please guide me how to achieve this.

Regards, 
Sachin 

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On January 10, 2017 06:34 AM UTC

Hi Sachin, 
 
We are able to reproduce the problem at our end. The setCellText will not work with the Batch Edit Mode. So we suggest to use the setCellValue method of the Grid which has been recommended only for the Batch Edit mode of the Grid. Refer to the following code example and Help Document. 
 
    function select(args) { 
        var result = this.getSelectedRecords(); 
        var CustomerID = result[0].CustomerID; 
        var gridInstance = $("#FlatGrid").ejGrid("instance"); 
        var index = gridInstance.selectedRowsIndexes; 
        gridInstance.setCellValue(index, "CustomerID", CustomerID); 
        $("#helpDialog").ejDialog("close"); 
    } 
 
 
We have also prepared a sample with your requirement that can be downloaded from the following location. 
 
 
Regards, 
Seeni Sakthi Kumar S. 


sachin
Replied On January 10, 2017 07:54 AM UTC

Hello Seeni,


Thanks for the quick response. i have tried the setCellValue() method but it was creating a new text box below grid. after looking at your sample code i found that if we initialize the gird with Model and  set column fields using model and put validation for the column then this method wont work but we initialize the grid with object class and don't put validation then it will work.
For example if we use the this code to initialize the grid and bind the columns like this then setCellValue() method will create new textBox instead of setting the specific cell value-


    @(Html.EJ().Grid<EducationModel>("FlatGrid")
            .Datasource(ds => ds.URL(Url.Action("EduDeptDataSource", "CRUD", null, Request.Url.Scheme)).BatchURL(Url.Action("EduDeptBatchUpdate", "CRUD", null, Request.Url.Scheme)).Adaptor(AdaptorType.UrlAdaptor))
            .EnableAltRow()
            .AllowFiltering()
            .IsResponsive()
            .AllowSorting()
            .GridLines(GridLines.Horizontal)
            .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.Batch); })

            .AllowPaging()
            .ClientSideEvents(eve =>
            {
            
                eve.CellEdit("onCellEdit");
                eve.CellSelected("cellSelected");



            })
            .Columns(col =>
            {
                col.Field(e => e.departmentId).HeaderText("Department ID").IsPrimaryKey(true).Visible(false).TextAlign(TextAlign.Left).Width(70).Add();
                col.Field(e => e.educationId).HeaderText("Department ID").Visible(false).TextAlign(TextAlign.Left).Width(70).Add();

                col.Field(e => e.departmentCode).HeaderText("Department Code").ValidationRules(v => v.AddRule("required", true).AddRule("maxlength", 40)).TextAlign(TextAlign.Left).Width(70).Add();
                col.Field(e => e.description).HeaderText("Description").Width(90).Add();             



            })
                )


and if we use this code like you have used in the sample then setCellValue() method will work fine-

            @(Html.EJ().Grid<object>("FlatGrid")
            .Datasource(ds => ds.URL(Url.Action("EduDeptDataSource", "CRUD", null, Request.Url.Scheme)).BatchURL(Url.Action("EduDeptBatchUpdate", "CRUD", null, Request.Url.Scheme)).Adaptor(AdaptorType.UrlAdaptor))
            .EnableAltRow()
            .AllowFiltering()
            .IsResponsive()
            .AllowSorting()
            .GridLines(GridLines.Horizontal)
            .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.Batch); })

           
            .AllowPaging()
            .ClientSideEvents(eve =>
            {
              
                eve.CellEdit("onCellEdit");
                eve.CellSelected("cellSelected");



            })
            .Columns(col =>
            {
                col.Field("departmentId").HeaderText("Department ID").IsPrimaryKey(true).Visible(false).TextAlign(TextAlign.Left).Width(70).Add();
                col.Field("educationId").HeaderText("Department ID").Visible(false).TextAlign(TextAlign.Left).Width(70).Add();

                col.Field("departmentCode").HeaderText("Department Code").TextAlign(TextAlign.Left).Width(70).Add();
                col.Field("description").HeaderText("Description").Width(90).Add();



            })
                )

So i have changed according to the sample you provided me and its working fine. but now i need your help with the following queries-

1. is there any way we can put validation on cell in batch mode with the setCellValue() method? because like i mentioned it will create new textbox instead of setting the value to specific cell.
2. how to check for duplicates when a user tries to add a new row with the batchEdit mode and URL adapter and show a popup before saving it to the database.


Regards,

Sachin 


Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On January 11, 2017 11:53 AM UTC

Hi Sachin, 
 
Query #1: Is there any way we can put validation on cell in batch mode with the setCellValue() method? because like i mentioned it will create new textbox instead of setting the value to specific cell. 
 
We are able to reproduce the problem only if the validation has been set for the respective column. A textbox or editform will be shown along with the validation message when the value doesn’t pass the given validation rule which is the default behavior. The same scenario has been occurred in your case while setting the value through setCellValue method. If you would like to avoid this, you must remove the validation rule from the columns.  
 
If you are reporting a different scenario, please get back to us exact scenario or whether you do not want to check for validation rule while using setCellValue method. 
 
 
  
Query #2: how to check for duplicates when a user tries to add a new row with the batchEdit mode and URL adapter and show a popup before saving it to the database. 
 
You can check the duplicate values in the server-end and return an exception message to client-end via ActionFailure event of the Grid. Refer to the following code example and API Reference. 
 
@(Html.EJ().Grid<object>("FlatGrid") 
                   ..  .  
                   . . . .  
                   .ClientSideEvents(events => events.ActionFailure("actionFailure")) 
) 

<script type="text/javascript"> 
    function actionFailure(args) { 
        var str = ""; 
        str = ($($(args.error.responseText).find('b')[0]).text() + ":" + $(args.error.responseText).find('i').text()); 
        window.alert(str)//alert message for the exception 
    } 
 
</script> 
 
 
We have modified the sample that can be downloaded from the following location. 
 
 
Regards, 
Seeni Sakthi Kumar S. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;