JSLink对列执行函数

时间:2017-03-09 12:33:22

标签: javascript sharepoint

是否可以对file.Js中的列执行函数?

示例:Suppliers是我的列名。

我想执行一个函数

var lookupSample = lookupSample || {};

lookupSample.CustomizeFieldRendering = function() {
// Intialize the variables for overrides objects
var overrideCtx = {
    Templates: {
        Fields: {
            'Suppliers': {
                'NewForm': lookupSample.singleLookupValue
            },
        }
    }
};
overrideCtx.Templates.OnPostRender = PostRenderJs;


// Register the override of the field
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
}

lookupSample.singleLookupValue = function(ctx) {
var output = [];
var field = ctx.CurrentFieldSchema.Choices;
output.push('<div class="ui-widget"> <select id="combobox">');
// Check if field contains data
if (field.length > 0) {
    for (i = 0; i < field.length; i++) {
        output.push('<option id="');
        output.push(field[i].LookupId);
        output.push('">');
        output.push(field[i].LookupValue);
        output.push('</option>');
    }
    output.push('</select></div>');
}
// Push the value to the array
return output.join('');
}

function PostRenderJs (ctx){
    alert('Hello World');
}

lookupSample.CustomizeFieldRendering();

此代码执行我想要的操作,但它打开3(因为我的列表中有3列,如果我有4列,弹出4次,等等)警告同样的消息。

我想只播放一次此消息(基于我的专栏Suppliers)。

这样的东西
function PostRenderJs (ctx){
  if(ctx.columnname=="Suppliers"){
    alert('Hello World');
  }
};

所以它只会播放Hello World一次。

1 个答案:

答案 0 :(得分:0)

请参阅以下代码段:

如果您只想为特定字段执行某些代码,您可以直接为该特定字段编写代码。

var options = {
      Templates: {
        Fields: {
          'Field1_Internal_Name': {
              View: /* function or string */,
              EditForm: /* function or string */,
              DisplayForm: /* function or string */,
              NewForm: /* function or string */
          },
        }
      },
    };

参考上面的代码段,

  1. 将“ Field1_Internal_Name ”替换为字段的内部名称

  2. 指定您是否正在为View,EditForm,DisplayForm,NewForm

  3. 编写代码
  4. 编写代码。

  5. 参考:https://www.codeproject.com/Articles/620110/SharePoint-Client-Side-Rendering-List-Views