简体   繁体   中英

KendoUI Grid: How to programmatically make a specific column non editable

I want to make a KendoUI Grid column programmatically non editable.

The information which column should be non editable, I will get after the grid was created and filled with data in my application. Thats why I have to set it programmatically.

First I created the grid on this way:

var dataSource = new kendo.data.DataSource({
    schema: {
        model: {
            fields: {
                field1: {
                   editable: true,
                },
                field2: {
                   editable: true,
                }
            }
        }
    }
});

$(domNode).kendoGrid({
    editable: true,
    dataSource: dataSource,
    columns: [{
            field: 'field1',
            title: 'First column'
        },
        {
            field: 'field2',
            title: 'Second column'
        }
    ]
});

Then I add some data (I know its not realy necessary to show this here):

var grid = $(domNode).data('kendoGrid'); 

grid.dataSource.add({
    field1: 'Some value',
    field2: 'Some other value'
});

Later in my application, I will get the information which column should be non editable. Then I've tried the following:

grid.dataSource.options.schema.model.fields['field1'].editable = false;

grid.dataSource.read(); // No changes, cloumn is still editable
grid.refresh(); // No changes, cloumn is still editable
grid.setDataSouce(grid.dataSouce); // No changes, cloumn is still editable

I'm desperately. Whats the correct way to porogrammaticly make a column non editable?

I would suggest two approaches:

  1. Define an editor function for each field and when invoked decide on how to render it it if should be an input or a plain div / span . This might be a lot of work since you need to repeat this for each field that can be both editable and not editable depending on that information that you receive latter.
  2. Whenever you receive the information about which fields are editable, create a new grid that contains the actual information about which fields are editable and which ones are not. You might opt for destroying the previous grid definition or just hide it and make the new one visible in the same place.

Unless there are just a few fields which might be editable or not, I would go with the second approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM