表示テンプレートがコンテンツをレンダリングした後に他のJavaScriptを実行します。

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/49349

  •  09-12-2019
  •  | 
  •  

質問

これは問題です:

  1. カスタム表示で指定されたContentBySearchWebPartを作成します。 テンプレート '
  2. ディスプレイの後、他のJavaScriptコードを実行したい テンプレートはコンテンツをレンダリングしました
  3. その後、私のカスタムJavaScriptファイルを表示テンプレートに登録します。

            $includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Slider.js")'
    
    .

    問題は、my slider.jsファイルコード を実行することです。 テンプレートの「JavaScriptコードは。

    誰かが私にslider.js code を実行する方法を私に提供することができます 表示テンプレートは、の内容をレンダリングしました ContentBySearchWebPart(CSWP)?

役に立ちましたか?

解決

:) my issue is solved.

I did following:

  1. Registered the custom JS file in Control_YourTemplateName.html file(like Control_Banner).

    $includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/HWScripts/Banner.js");

  2. Added these three lines under the first div after body tag, in YourTemplateName.html (like Banner.html) file.

     <!--#_  
     ctx.OnPostRender = [];
     ctx.OnPostRender.push(function(){ 
        CustomMethodWhichIsIncludedInTheCustomJSFile();
     });
     _#-->  
    

But a better solution is provided below:

You can skip defining OnPostRender yourself. CBS Display Templates have this function baked in:

<!--#_
   AddPostRenderCallback(ctx, function(){
       alert(ctx.Title + "finished rendering!");
   });
_#-->

There is also AddPreRenderCallback(ctx, function(){});

他のヒント

スクリプトを含めてコード行を追加できます。

ExecuteOrDelayUntilScriptLoaded( function() { <your custom function name>; }, 'name of   display template ');
.

例えば:

ExecuteOrDelayUntilScriptLoaded( function() { console.dir( SP.Res ); }, 'sp.js');
.

ライセンス: CC-BY-SA帰属
所属していません sharepoint.stackexchange
scroll top