Extending or modifying the SharePoint Datasheet view

我只是一个虾纸丫 提交于 2020-01-16 18:06:26

问题


Has anyone discovered a way to extend or modify the functionality of the SharePoint Datasheet view (the view used when you edit a list in Datasheet mode, the one that looks like a basic Excel worksheet)?

I need to do several things to it, if possible, but I have yet to find a decent non-hackish way to change any functionality in it.

EDIT: An example of what I wish to do is to enable cascading filtering on lookup fields - so a choice in one field limits the available choices in another. There is a method to do this in the standard view form, but the datasheet view is completely seperate.

Regards

Moo


回答1:


I don't think you can modify it in any non-hackish way, but you can create a new datasheet view from scratch. You do this by creating a new ActiveX control, and exposing it as a COM object, and modifying the web.config file to make reference to the new ActiveX control.

There's an example here: Creating a custom datasheet control.




回答2:


Actually, you can do this. Here is a code snippet I stripped out of someplace where I am doing just what you asked. I tried to remove specifics.

var gridFieldOverrideExample = (function (){
    function fieldView(ctx){
        var val=ctx.CurrentItem[curFieldName];
        var spanId=curFieldName+"span"+ctx.CurrentItem.ID;
        if (ctx.inGridMode){
            handleGridField(ctx, spanId);
        }       
        return "<span id='"+spanId+"'>"+val+"</span>";      
    } 

    function handleGridField(ctx, spanID){
        window.SP.SOD.executeOrDelayUntilScriptLoaded(function(){
            window.SP.GanttControl.WaitForGanttCreation(function (ganttChart){
                var gridColumn = null;
                var editID = "EDIT_"+curFieldName+"_GRID_FIELD";
                var columns = ganttChart.get_Columns();
                for(var i=0;i<columns.length;i++){
                    if(columns[i].columnKey == curFieldName){
                        gridColumn = columns[i];
                        break;
                    } 
                }
                if (gridColumn){
                    gridColumn.fnGetEditControlName = function(record, fieldKey){
                        return editID;
                    };
                    window.SP.JsGrid.PropertyType.Utils.RegisterEditControl(editID, function (ctx) {
                        editorInstance = new SP.JsGrid.EditControl.EditBoxEditControl(ctx, null);
                        editorInstance.NewValue = "";
                        editorInstance.SetValue = function (value) {
                            _cellContext = editorInstance.GetCellContext(); 
                            _cellContext.SetCurrentValue({ localized: value });
                        };
                        editorInstance.Unbind = function () {
                            //This happens when the grid cell loses focus - hide controls here, do cleanup, etc.
                        }
                        //Below I grabbed a reference to the original 'BindToCell' function so I can prepend to it by overwriting the event.
                        var origbtc = editorInstance.BindToCell;
                        editorInstance.BindToCell = function(cellContext){
                            if ((cellContext.record) && 
                                (cellContext.record.properties) && 
                                (cellContext.record.properties.ID) && 
                                (cellContext.record.properties.ID.dataValue)){
                                editorInstance.ItemID = cellContext.record.properties.ID.dataValue;
                            }
                            origbtc(cellContext);
                        };
                        //Below I grabbed a reference to the original 'OnBeginEdit' function so I can prepend to it by overwriting the event.                   
                        var origbte = editorInstance.OnBeginEdit;
                        editorInstance.TargetID;
                        editorInstance.OnBeginEdit = function (cellContext){
                            this.TargetID = cellContext.target.ID;
                            /*
                            . . .
                            Here is where you would include any custom rendering 
                            . . .
                            */
                            origbte(cellContext);
                        };
                        return editorInstance;
                    }, []);

                }
            });
        },"spgantt.js");
    }

    return{
        fieldView : fieldView
    }
})();

(function () {
    function OverrideFields(){
        var overrideContext = {}; 
        overrideContext.Templates = overrideContext.Templates || {};
        overrideContext.Templates.Fields = {
            'FieldToOverride' : {
                'View': gridFieldOverrideExample.fieldView
            }
        };   
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideContext);
    }   
    ExecuteOrDelayUntilScriptLoaded(OverrideFields, 'clienttemplates.js');
})();   

Also, there are a couple of other examples out there. Sorry, I don't have the links anymore:



来源:https://stackoverflow.com/questions/1451372/extending-or-modifying-the-sharepoint-datasheet-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!