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. Image for the cookie policy date
close icon

Show columns on edit row

Hello Syncfusion Team,
how can I show certain columns and hide others only when the row is in edit mode?

Now, I use this code to hide the columns:

col.Field(m => m.ColumnProperty1).HeaderText("Property1").Visible(false).Add();
col.Field(m => m.ColumnProperty2).HeaderText("Property2").Visible(false).Add();

I tried with:

function OnBeginEdit(args) {
       $('#myTable').data("ejGrid").showColumns(["Property1", "Property2"]);
}

inside the BeginEdit client event:

@(Html.EJ().Grid<MyClass>("myTable") .ClientSideEvents(events =>
            {
                events.BeginEdit("OnBeginEdit");
                 ..........

But I received this error  in the browser console:

Uncaught TypeError: Cannot read property 'form' of undefined
a.extend.rules @ jquery.validate.min.js:4
t.gridFeatures.edit.setValidationToField @ ej.web.all.min.js:10
t.gridFeatures.edit.setValidation @ ej.web.all.min.js:10
t.widget._completeAction @ ej.web.all.min.js:10
t.widget.sendDataRenderingRequest @ ej.web.all.min.js:10
t.gridFeatures.common._processBindings @ ej.web.all.min.js:10
t.gridFeatures.edit.startEdit @ ej.web.all.min.js:10
t.gridFeatures.edit._editdblClickHandler @ ej.web.all.min.js:10
n.event.dispatch @ jquery-2.1.4.min.js:3
r.handle @ jquery-2.1.4.min.js:3



3 Replies

PK Prasanna Kumar Viswanathan Syncfusion Team November 17, 2015 09:46 AM UTC

Hi Omar,

Thanks for contacting Syncfusion support.

We are able to reproduce the mentioned script error. While using showcolumns method, the Grid content will refresh and the edit form will destroy. So, the script error will throw.

To achieve your requirement, use the toolbarClick event of ejGrid. This event triggers when toolbar clicked in Grid and we can get itemName in the arguments. We check the condition with ItemName and show the columns using showColumns method.


Please find the code example and sample :

@(Html.EJ().Grid<object>("FlatGrid")

.Datasource((IEnumerable<object>)ViewBag.datasource)

.AllowSorting()

.AllowPaging()

       .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing();})

            .ToolbarSettings(toolbar =>

            {

                toolbar.ShowToolbar().ToolbarItems(items =>

                {

                    items.AddTool(ToolBarItems.Add);

                    items.AddTool(ToolBarItems.Edit);

                    items.AddTool(ToolBarItems.Delete);

                    items.AddTool(ToolBarItems.Update);

                    items.AddTool(ToolBarItems.Cancel);

                });

            })

.ClientSideEvents(eve => eve.ToolbarClick("complete"))

.Columns(col =>

{

    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Visible(false).TextAlign(TextAlign.Right).Width(75).Add();

    col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();

    col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Visible(false).Width(75).Add();

    col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Visible(false).Format("{0:C}").Add();

    col.Field("OrderDate").HeaderText("Order Date").TextAlign(TextAlign.Right).Width(80).Visible(false).Format("{0:MM/dd/yyyy}").Add();

    col.Field("ShipCity").HeaderText("Ship City").Width(110).Visible(false).Add();

})

)


<script>

   function complete(args) {

           if (args.itemName == "Edit") {

                var grid = $("#Grid").ejGrid("instance");

                var index = parseInt(grid.selectedRowsIndexes[0]);

                $('#Grid').data("ejGrid").showColumns(["ShipCity", "Freight"]);

                grid.selectRows(index);

            }

</script>


Sample: http://www.syncfusion.com/downloads/support/forum/121167/ze/Sample118577-316275400

Refer to the Help document for the toolbarClick event,

http://help.syncfusion.com/js/api/ejgrid#events:toolbarclick

Regards,
Prasanna Kumar N.S.V



OM Omar Muscatello November 17, 2015 10:23 AM UTC

Thank you Prasanna Kumar Viswanathan.
The problem is that I don't use the toolbar. Users double-click on the row to start editing.
Are there alternatives to toolbar? 


SR Sellappandi Ramu Syncfusion Team November 18, 2015 12:54 PM UTC

Hi Omar,

By default we have the recordDoubleClick event in grid and we can show the columns in recordDoubleClick event.

Please refer to the online help documentation for recordDoubleClick event,

Document: http://help.syncfusion.com/js/api/ejgrid#events:recorddoubleclick

Note: While using showcolumns() method, the Grid content will refresh and the edit form will destroy. So, we need to use startEdit() method in recordDoubleClick event to edit the selected record.

Please refer to the code example and sample,

@(Html.EJ().Grid<object>("FlatGrid")

.Datasource((IEnumerable<object>)ViewBag.datasource)

.AllowSorting()

.AllowPaging()

           .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })

            .ToolbarSettings(toolbar =>

            {

                toolbar.ShowToolbar().ToolbarItems(items =>

                {

                    items.AddTool(ToolBarItems.Add);

                    items.AddTool(ToolBarItems.Edit);

                    items.AddTool(ToolBarItems.Delete);

                    items.AddTool(ToolBarItems.Update);

                    items.AddTool(ToolBarItems.Cancel);

                });

            })

.ClientSideEvents(eve => eve.ToolbarClick("complete").RecordDoubleClick("edit"))

.Columns(col =>

{

    ….


})

)


<script>

    function complete(args) {

        if (args.itemName == "Edit") {

            var grid = $("#FlatGrid").ejGrid("instance");

            var index = parseInt(grid.selectedRowsIndexes[0]);

            $("#FlatGrid").data("ejGrid").showColumns(["ShipCity", "Freight"]);

            grid.selectRows(index);

        }

    }

    function edit(args) {

        if (!this.columnPushed) {

            $("#FlatGrid").data("ejGrid").showColumns(["ShipCity", "Freight"]);

            this.columnPushed = true;// checking if columns are already pushed

            this.startEdit($(this.getContentTable().find("tr")[args.rowIndex]));

        }

    }

</script>


Sample: http://www.syncfusion.com/downloads/support/forum/121167/ze/Sample_1211671543503411

Regards,
Sellappandi R

Loader.
Live Chat Icon For mobile
Up arrow icon