Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Service Portal Widget pagination

debopriya2
Tera Contributor

Hi,

Can anyone suggest how to customize a widget to enable pagination? Please share code if possible.

Thanks in advance.

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Hi Debopriya,



You may find link Pagination in service portal   helpful.


debopriya2
Tera Contributor

Hi Jaspal thanks 4 the update... but as said the records are not getting fetched


debopriya2
Tera Contributor

Hi all,



Kindly suggest on where I am going wrong.



HTML Body Template


-------------------------------------------------------------------------------------------


<div>


  <div class="panel panel-default">




      <div ng-if="data.results.length==0">


          <div class="panel-heading"><h4 class="panel-title">${No results}</h4></div>


          <div class="panel-body wrapper break-word">


              <p>${Your search - <b>{{data.q}}</b> - did not match any documents}</p>


              <p>${Suggestions}:</p>


              <ul>


                  <li>${Make sure all words are spelled correctly}</li>


                  <li>${Try different, more general, or fewer keywords}</li>


              </ul>


          </div>


      </div>


     


      <div ng-if="data.results.length>0" class="panel-heading break-word"><h4 class="panel-title">${Search results for '{{data.q}}'}</h4></div>




      <div ng-repeat="item in data.results | orderBy:'score':true" class="panel-body b-b ">




          <div ng-if="item.type == 'kb'">


              <a href="?id=kb_article&amp;sys_id={{item.sys_id}}" class="h4 text-primary m-b-sm block">


                  <i class="fa fa-book m-r-sm"></i>


                  <span ng-bind-html="highlight(item.short_description, data.q)"></span></a>


              <p ng-bind-html="highlight(item.text, data.q)"></p>


              <span class="text-muted">${Article}: {{item.number}}</span>


              <span class="text-muted">


                  <span class="m-l-xs m-r-xs"> &middot; </span>


                  ${Published}: <sn-day-ago date="item.published"/>


              </span>


          </div>




          <div ng-if="item.type == 'sc'">


              <a href="?id={{item.page}}&amp;sys_id={{item.sys_id}}" class="h4 text-primary m-b-sm block">


                  <i class="fa fa-shopping-cart m-r-sm"></i>


                  <span ng-bind-html="highlight(item.name, data.q)"></span></a>


              <div ng-style="getBGImage(item)" ng-if="item.picture" class="img-responsive m-r item-image pull-left"></div>


              <p ng-bind-html="highlight(item.short_description, data.q)"></p>


              <span class="text-muted m-r-sm" ng-if="item.price != '$0.00'">{{item.price}}</span>


          </div>


         


          <div ng-if="item.type == 'qa'">


              <a href="?id=kb_social_qa_question&amp;question_id={{item.sys_id}}" class="h4 text-primary m-b-sm block">


                  <i class="fa fa-comments-o m-r-sm"></i>


                  <span ng-bind-html="highlight(item.label, data.q)"></span></a>


              <p ng-bind-html="highlight(item.text, data.q)"></p>


              <div>


                  <a ng-repeat="tag in item.tags" ng-href="?id=sqa_tagged_questions&sys_id={{tag.sys_id}}" class="question-tag">{{tag.name}}</a>


              </div>


              <span class="text-muted">${Asked} <sn-day-ago date="item.sys_created_on"/></span>


              <span class="text-muted">


                  <span class="m-l-xs m-r-xs" ng-if="!$first"> &middot; </span>


                  ${Score}: {{item.votes}}


              </span>


          </div>


         


          <div ng-if="item.type == 'rec'">


              <a href="?id={{item.page}}&amp;sys_id={{item.sys_id}}&amp;table={{item.table}}" class="h4 text-primary m-b-sm block">


                  <span ng-bind-html="highlight(item.label, data.q)"></span>


              </a>


              <span class="text-muted" ng-repeat="f in item.fields | limitTo: 4">


                  <span class="m-l-xs m-r-xs" ng-if="!$first"> &middot; </span>


                  {{f.label}}: <span ng-bind-html="highlight(f.display_value, data.q)"></span>


              </span>


          </div>


         


          <tr ng-repeat="item in c.data.results | startFrom: currentPage*pageSize | limitTo: pageSize">



  <button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">


              Previous


  </button>


      {{currentPage+1}}/{{numberOfPages()}}


  <button ng-disabled="currentPage >= c.data.results.length/pageSize - 1" ng-click="currentPage=currentPage+1">


              Next


  </button>


         




          <div class="clearfix"></div>




      </div>


  </div>


</div>


-----------------------------------------------------------------------------------------------------------------------------------------------------



Server Script


******************************************************************************************************************************************************


// populate the 'data' variable


data.q = $sp.getParameter('q');


data.t = $sp.getParameter('t');


data.sc_default = $sp.getValue('sc_catalog');


data.kb_default = $sp.getValue('kb_knowledge_base');


data.sc = $sp.getParameter('sc');


data.kbase = $sp.getParameter('kb');


data.title = $sp.getParameter('title');


data.results = [];




if (data.t)


  data.count = 15;


else


  data.count = 30;




// add in additional search tables from sp_search_groups


var portalGR = $sp.getPortalRecord();


var portalID = portalGR.getUniqueValue();


var sg = GlideRecord('sp_search_group');


sg.addQuery('sp_portal', portalID);


sg.addActiveQuery();


sg.orderBy('order');


sg.query();


while (sg.next())


  addSearchTable(sg);




function addSearchTable(sg) {


  var table = sg.getValue('name');


  var condition = sg.getValue('condition');


  var gr = GlideRecordSecure(table);


  if (condition)


  gr.addEncodedQuery(condition);



  gr.addQuery('123TEXTQUERY321', data.q);


  gr.query();


  while (gr.next()) {


  if (data.t != table && data.t)


  continue;



  var rec = {};


  rec.type = "rec";


  rec.table = table;


  rec.sys_id = gr.getUniqueValue();


  rec.page = sg.getDisplayValue('sp_page');


  if (!rec.page)


  rec.page = "form";


  rec.label = gr.getDisplayValue();


  rec.score = parseInt(gr.ir_query_score.getDisplayValue());


  rec.fields = [];



  var fields = $sp.getListColumns(table,'sp');


  fields = fields.split(',');


  for (var i in fields) {


  var field = fields[i];


  var obj = getField(gr, field);


  if (rec.label != obj.value)


  rec.fields.push(obj);


  }



  data.results.push(rec);


  }


}




if (data.t == 'kb' || !data.t) {


  //var kb = $sp.getKBRecord();


  var kb = new GlideRecord("kb_knowledge");


  if(data.kbase){


  kb.addQuery('kb_knowledge_base', data.kbase);


  }


  else {


  kb.addQuery('kb_knowledge_base', data.kb_default);


  }


  kb.addQuery('123TEXTQUERY321', data.q);


  kb.setLimit(data.count);


  kb.query();


  data.article_count = kb.getRowCount();


  while (kb.next()) {


  // Does user have permission to see this item?


  if (!$sp.canReadRecord("kb_knowledge", kb.getUniqueValue()))


  continue;



  var article = {};


  $sp.getRecordDisplayValues(article, kb, 'sys_id,number,short_description,published,text')


  article.type = "kb";


  if (!article.text)


  article.text = "";


  article.text = $sp.stripHTML(article.text) + "";


  article.text = article.text.substring(0, 200);


  article.score = parseInt(kb.ir_query_score.getDisplayValue());


  data.results.push(article);


  }


  $sp.logSearch('kb_knowledge', data.q, kb.getRowCount());


}




if (gs.isLoggedIn() && (data.t == 'sc' || !data.t)) {


  var sc = new GlideRecord("sc_cat_item");


  if(data.sc){


  sc.addQuery('sc_catalogs', data.sc);


  } else {


  sc.addQuery('sc_catalogs', data.sc_default);


  }


  sc.addQuery('123TEXTQUERY321', data.q);


  sc.setLimit(data.count);


  sc.query();


  while (sc.next()) {


  // Does user have permission to see this item?


  var catItem = GlideappCatalogItem.get(sc.sys_id.getDisplayValue());


  if (!catItem.canViewOnSearch())


  continue;



  var item = {};


  item.type = "sc";


  $sp.getRecordDisplayValues(item, sc, 'name,short_description,picture,price,sys_id');


  item.score = parseInt(sc.ir_query_score.getDisplayValue());


  item.page = sc.getRecordClassName() == 'sc_cat_item_guide' ? 'sc_cat_item_guide' : 'sc_cat_item';


  data.results.push(item);


  }


  $sp.logSearch('sc_cat_item', data.q, sc.getRowCount());


}




data.sqandaEnabled = gs.getProperty('glide.sp.socialqa.enabled', false) === 'true';




if (data.sqandaEnabled && (data.t == 'qa' || !data.t)) {


  var questionGR = new GlideRecord("kb_social_qa_question")


  questionGR.addQuery('123TEXTQUERY321',data.q);


  questionGR.setLimit(data.count);


  questionGR.query();


  while (questionGR.next()) {


  if (!$sp.canReadRecord(questionGR))


  continue;



  var question = {};


  question.type = "qa";


  $sp.getRecordDisplayValues(question, questionGR, 'question,votes,question_details,sys_created_on,sys_id');



  question.text = (question.question_details) ? $sp.stripHTML(question.question_details) : "";


  question.text = question.text.substring(0,200);


  question.label = question.question;



  question.votes = 0;


  var voteGR = new GlideRecord("kb_social_qa_vote");


  voteGR.addQuery("reference_id", questionGR.getUniqueValue());


  voteGR.query();


  while (voteGR.next())


  question.votes = (voteGR.getValue("up_vote") === "1") ? question.votes + 1 : question.votes - 1;



  question.votes = question.votes;



  question.tags = [];


  var labelEntryGR = new GlideRecord("sqanda_tag_entry");


  labelEntryGR.addQuery("reference_id", question.sys_id);


  labelEntryGR.query();


  while (labelEntryGR.next()) {


  var labelGR = labelEntryGR.getElement("tag").getRefRecord();


  question.tags.push({


  sys_id: labelGR.getUniqueValue(),


  name: labelGR.getValue("name")


  })


  }



  data.results.push(question);


  }


  $sp.logSearch('kb_social_qa_question', data.q, questionGR.getRowCount());


}




function getField(gr, name) {


  var f = {};


  f.display_value = gr.getDisplayValue(name);


  f.value = gr.getValue(name);


  var ge = gr.getElement(name);


  if (ge == null)


  return f;



  f.type = ge.getED().getInternalType()


  f.label = ge.getLabel();


  return f;


}


********************************************************************************************************************************************************************



Client Controller


=========================================================================================================


function ($scope, $sce, $location, spUtil, $rootScope) {


  spUtil.setSearchPage($scope.data.t);


  $scope.currentPage=0;


  $scope.pageSize=10;



  $scope.numberOfPages=function(){


              return Math.ceil($scope.c.data.results.length/$scope.pageSize);                            


      }



  $scope.filter('startFrom', function() {


      return function(input, start) {


              start = +start; //parse to int


              return input.slice(start);


  }


      });



  $scope.getBGImage = function(item) {


  return {"background-image": "url('" + item.picture + "')"};


  }




  $scope.highlight = function(haystack, needle) {


  if (!haystack)


  return "";




  if (!needle)


  return $sce.trustAsHtml(haystack);




  return $sce.trustAsHtml(haystack.replace(new RegExp(needle, "gi"), function(match) {


  return '<mark class="highlight">' + match + '</mark>';


  }));


  }


}


===================================================================================================================



Please help.


Nithya V Sivara
Tera Contributor

HI DeboPriya,

 

  Were you able to co pagination?I am trying to implement Pagination. Could you please help me on that.

 

thanks,

Nithya