Here’s some code I’ve been working on for quite some time. It’s probably not something a ton of people need, but if you do it was hard to find.
Use case: You have a datasheet view in a classic web part page and only want certain fields to be editable.
Setup:
- Create a notepad file with the code below and save it something like “datasheetlock.js”
- Upload the file to your Site Assets library
- Go into page edit mode on your classic web part page and edit the datasheet web part
- Under Miscellaneous/JS Link add this link “~sitecollection/SiteAssets/datasheetlock.js”. Click OK to save.
Here’s the code for your datasheetlock.js file:
var fieldContext = {
Templates:{
Fields: {
FieldNameToMakeReadOnly: {
View: function(ctx, b)
{
b.AllowGridEditing = false;
return ctx.CurrentItem.FieldNameToMakeReadOnly;
}
}
}
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldContext);
If you have a people picker, you have to do it a little differently:
var fieldContext = {
Templates:{
Fields: {
PeoplePickerField: {
View: function(ctx, b)
{
b.AllowGridEditing = false;
return "<span>" + ctx.CurrentItem.PeoplePickerField[0].title + "</span>";
}
}
}
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldContext);