In views i am setting service is inuse as default filter but that is not working in onLoad time

mani55
Tera Contributor

In views i am setting services in use as default filter but that is not working in onLoad time

 

we need to work inuse filter in onload time as well 


HTML:

  <div class="panels-container list-group">
  <div class="row" style="margin:35px 0 20px 0;"> 
  <div class="col-md-9 col-xs-7"></div>
  <div class="col-md-3 col-xs-5">
    <div class="form-inline" ng-if="data.dataTableWidget && !data.invalid_table && tab == 1" style="text-align:right;">
      <label class="control-label hidden-xs" for="view">${View}</label>
      <select id="view" ng-model="c.viewFilter" class="form-control sc-basic-select" ng-change="c.changeView()" style="width:80%">
        <option value="all">${All}</option>
        <option value="inUse">${Services in use}</option>
        <option value="onOrder">${Services currently on order}</option>
        <option value="retired">${Services no longer in use (retired)}</option>  
      </select>
    </div>
  </div> 
  </div> 
  </div>   

<form class="form-inline pull-right padd" ng-if="tab == 1">
  <div class="checkbox sn-tooltip-basic" title="Requestor - the person who made the service request. This person is normally the person using the service but can also request on behalf of others (see Requested For)
" data-toggle="tooltip" data-placement="auto">
    <label>
      <input type="checkbox" ng-model="c.requester" ng-change="c.changeFilter()" /> Requester
    </label>
  </div>
  <div class="checkbox sn-tooltip-basic" title="Requested For  - who the service is for, even if the request was made on your behalf"  data-toggle="tooltip" data-placement="auto">
    <label>
      <input type="checkbox" ng-model="c.requested_for" ng-change="c.changeFilter()" />  Requested for
    </label>
  </div>
  <div class="checkbox sn-tooltip-basic" title="Responsible - people who oversee the use of the service e.g. project manager (not budget holder), team leader etc.
"  data-toggle="tooltip" data-placement="auto">
    <label>
      <input type="checkbox" ng-model="c.responsible" ng-change="c.changeFilter()" /> Responsible
    </label>
  </div>
  <div class="checkbox sn-tooltip-basic" title="Project Owner – person who is responsible for the project code. This may also be your budget holder"  data-toggle="tooltip" data-placement="auto">
    <label>
      <input type="checkbox" ng-model="c.project_code_owner" ng-change="c.changeFilter()" /> Project Owner
    </label>
    </div>
   <div class="checkbox sn-tooltip-basic" title="I have Transversal Rights – for the Financial controller. This is not the project code owner.
"  data-toggle="tooltip" data-placement="auto">
    <label>
      <input type="checkbox" ng-model="c.transversal" ng-change="c.changeFilter()" /> I have Transversal Rights
    </label>
    </div>
</form>

<ul class="nav nav-tabs" id="tabs">
  <li role="presentation" ng-class="{active: tab == 1}"><a href="javascript&colon;void(0)" ng-click="c.changeFilter('1')">Premium Service Items</a></li>
  <li role="presentation" ng-class="{active: tab == 2}"><a href="javascript&colon;void(0)" ng-click="c.changeFilter('2')">Hardware</a></li>
  <!--<li role="presentation" ng-class="{active: tab == 3}"><a href="javascript&colon;void(0)" ng-click="tab='3'">Retired</a></li>-->
</ul>

<div ng-if="data.dataTableWidget && !data.invalid_table">
  <sp-widget widget="data.dataTableWidget"></sp-widget>
</div>


Sever script:

(function() {
    deleteOptions(['table', 'field_list', 'order_by', 'order_direction', 'order']);

    data.view = 'sp';
    data.userID = gs.getUserID();
    if (input) {
        data.table = input.table;
        data.view = input.view;
    } else {
        data.table = $sp.getParameter('table') || $sp.getParameter('t');
        // data.filter = $sp.getParameter('filter');
        // 	data.viewFilter = $sp.getParameter('viewFilter');
        data.viewFilter = $sp.getParameter('viewFilter') || 'inUse';

        if ($sp.getParameter('filter')) {
            data.filter = $sp.getParameter('filter');
        } else {
            data.filter = 'install_status=1^ORinstall_status=3';
        }
    }

    if (!data.table) {
        data.invalid_table = true;
        data.table_label = "";
        return;
    }

    var gr = new GlideRecordSecure(data.table);
    if (!gr.isValid()) {
        data.invalid_table = true;
        data.table_label = data.table;
        return;
    }

    // page is where the record URLs go
    var sp_page = $sp.getValue('sp_page');
    if (sp_page) {
        var pageGR = new GlideRecord('sp_page');
        pageGR.get(sp_page);
        data.page_id = pageGR.id.getDisplayValue();
    }

    // widget parameters
    data.table_label = gr.getLabel();
    data.fields = $sp.getListColumns(data.table, 'mobile');
    data.title = gr.getPlural();

    // copyParameters(data, ['p', 'o', 'd', 'filter']);

	copyParameters(data, ['p', 'o', 'd']);
	
    copyParameters(data, ['relationship_id', 'apply_to', 'apply_to_sys_id']);
    data.filterACLs = true;
    data.show_new = true;
    data.show_keywords = true;
    data.show_breadcrumbs = gs.getUser().hasRole('admin') ? true : false;
    data.o = "sys_created_on";
    data.d = "desc";
    data.dataTableWidget = $sp.getWidget('cg-tabs-services-tables', data);

    data.fromUrl = true;

    //data.roleAdmOrItil = (gs.getUser().hasRole('admin') || gs.getUser().hasRole('itil') ) ? true : false;

    function copyParameters(to, names) {
        names.forEach(function(name) {
            data[name] = $sp.getParameter(name);

        });

    }

    // in case this widget is tied to the wrong instance type 
    function deleteOptions(names) {
        names.forEach(function(name) {
            delete options[name];
        });
    }
})();

Client controller:

function($scope, $rootScope, $location, spUtil, $timeout) {
	if ($scope.data.dataTableWidget)
		angular.extend($scope.data.dataTableWidget.options, $scope.options);
	
	var c = this;
	// Force the default view filter to 'inUse'
	c.viewFilter = 'inUse';	

	/* start customzation for filter */
	$scope.tab = $location.search().tab;
	c.requester = true;
	c.requested_for = true;
	c.responsible = true;
	c.project_code_owner = true;
	c.transversal = true;
	//$scope.viewFilter = 'all';/
	$scope.options.filter = $scope.data.filter;
	
	// Define the basic queries for alm_hardware and u_alm_service
	var hardwareQuery = 'assigned_to='+$scope.data.userID;
	var serviceQuery = 'u_requester='+$scope.data.userID;
	serviceQuery += '^ORassigned_to='+$scope.data.userID;
	serviceQuery += '^ORu_responsible_1='+$scope.data.userID+'^ORu_responsible_2='+$scope.data.userID+'^ORu_responsible_3='+$scope.data.userID+'^ORu_responsible_4='+$scope.data.userID;
	//serviceQuery += '^ORowned_by='+$scope.data.userID;
	serviceQuery += '^ORu_project_code.manager='+$scope.data.userID;
	serviceQuery += '^ORu_project_code.u_delegate='+$scope.data.userID;
	serviceQuery += '^ORu_project_code.parent.u_usersLIKE'+$scope.data.userID;	

	var initializing = true;
	$timeout(function() { initializing = false; });
	
	c.changeView = function() {
		resetParams();

		// Get the checkboxes filters
		var tmpTab1Filter = [];		
		
		if(!c.requester && !c.requested_for && !c.responsible && !c.project_code_owner) 
			tmpTab1Filter.push('u_requesterDYNAMIC0');		
		if (c.requester)				
			tmpTab1Filter.push('u_requester='+$scope.data.userID);
		if (c.requested_for)
			tmpTab1Filter.push('assigned_to='+$scope.data.userID);
		
		if (c.responsible)		
			tmpTab1Filter.push('u_responsible_1='+$scope.data.userID+'^ORu_responsible_2='+$scope.data.userID+'^ORu_responsible_3='+$scope.data.userID+'^ORu_responsible_4='+$scope.data.userID);	
			
		if (c.project_code_owner)
			tmpTab1Filter.push('u_project_code.manager='+$scope.data.userID+'^ORu_project_code.u_delegate='+$scope.data.userID+'^ORu_project_code.parent.u_usersLIKE'+$scope.data.userID+'^ORu_project_code.parent.parent.u_usersLIKE'+$scope.data.userID+'^ORu_project_code.parent.parent.parent.u_usersLIKE'+$scope.data.userID+'^ORu_project_code.parent.parent.parent.parent.u_usersLIKE'+$scope.data.userID+'^ORu_project_code.parent.parent.parent.parent.parent.u_usersLIKE'+$scope.data.userID);
			//tmpTab1Filter.push('owned_by='+$scope.data.userID+'^ORu_project_code.manager='+$scope.data.userID+'^ORu_project_code.u_delegate='+$scope.data.userID+'^ORu_project_code.parent.u_usersLIKE'+$scope.data.userID+'^ORu_project_code.parent.parent.u_usersLIKE'+$scope.data.userID+'^ORu_project_code.parent.parent.parent.u_usersLIKE'+$scope.data.userID+'^ORu_project_code.parent.parent.parent.parent.u_usersLIKE'+$scope.data.userID+'^ORu_project_code.parent.parent.parent.parent.parent.u_usersLIKE'+$scope.data.userID);
		
		var filterQuery = '';
		if($scope.tab == '1'){
			$scope.data.table = 'u_alm_service';
			if(c.viewFilter=='all')
				filterQuery = 'install_statusIN2,1,3,7^' + tmpTab1Filter.join('^OR');
			else if (c.viewFilter == 'inUse')
				filterQuery = 'install_status=1^ORinstall_status=3^' + tmpTab1Filter.join('^OR');
			else if(c.viewFilter == 'onOrder' && tmpTab1Filter.length > 0)
				filterQuery = 'install_status=2^' +  tmpTab1Filter.join('^OR');
			else if(c.viewFilter == 'onOrder' && tmpTab1Filter.length <= 0)
				filterQuery = 'install_status=2';
			else if(c.viewFilter == 'retired')
				filterQuery = 'install_status=7^' + tmpTab1Filter.join('^OR');	
		}
		else if($scope.tab == '2'){
			$scope.data.table = 'alm_hardware';
			filterQuery = hardwareQuery+ '^install_status=1';	
		}		

		$scope.options.filter = filterQuery;
		$scope.data.filter = filterQuery;
		$scope.data.fields = "";  // reset
		$scope.data.invalid_table = false;
		getData(true,filterQuery);		
	};

	 c.changeFilter = function(tab) {
		
		 
		// Reset the filter and create it from the inputs
		var filterQuery = ''
		// Get the checkboxes filters
		var tmpTab1Filter = [];
		if(!c.requester && !c.requested_for && !c.responsible && !c.project_code_owner) 
			tmpTab1Filter.push('u_requesterDYNAMIC0');
		if (c.requester)				
			tmpTab1Filter.push('u_requester='+$scope.data.userID);	
		if (c.requested_for)
			tmpTab1Filter.push('assigned_to='+$scope.data.userID);
		if (c.responsible)		
			tmpTab1Filter.push('u_responsible_1='+$scope.data.userID+'^ORu_responsible_2='+$scope.data.userID+'^ORu_responsible_3='+$scope.data.userID+'^ORu_responsible_4='+$scope.data.userID);
		if (c.project_code_owner)
			tmpTab1Filter.push('u_project_code.manager='+$scope.data.userID+'^ORu_project_code.u_delegate='+$scope.data.userID);
			//tmpTab1Filter.push('owned_by='+$scope.data.userID+'^ORu_project_code.manager='+$scope.data.userID+'^ORu_project_code.u_delegate='+$scope.data.userID);
		if (c.transversal)
			tmpTab1Filter.push('u_project_code.parent.u_usersLIKE'+$scope.data.userID);
				
				
		if (initializing && $scope.data.filter)
			return;

		if(typeof tab !== 'undefined')
			$scope.tab = tab;
		 
		if($scope.tab == '1'){
			if(c.viewFilter=='all')
				filterQuery = 'install_statusIN2,1,3,7^' + tmpTab1Filter.join('^OR');
		else if(c.viewFilter == 'inUse')
				filterQuery = 'install_status=1^ORinstall_status=3^' + tmpTab1Filter.join('^OR');
			else if(c.viewFilter == 'onOrder' && tmpTab1Filter.length > 0)
				filterQuery = 'install_status=2^' +  tmpTab1Filter.join('^OR');
			else if(c.viewFilter == 'onOrder' && tmpTab1Filter.length <= 0)
				filterQuery = 'install_status=2';
			else if(c.viewFilter == 'retired')
				filterQuery = 'install_status=7^' + tmpTab1Filter.join('^OR');	
			$scope.data.table = 'u_alm_service';	
		}
		else if($scope.tab == '2'){
			filterQuery = hardwareQuery+ '^install_status=1';
			$scope.data.table = 'alm_hardware';		
		}		 
		$scope.options.filter = filterQuery;
		$scope.data.filter = filterQuery;

		
		//	$scope.options.filter += "^" + tmp.join('^OR');
		angular.extend($scope.data.dataTableWidget.options, $scope.options);
		 
		getData(true);
	};
	/* end customzation for filter */

	$scope.$on('data_table.click', function(e, parms) {
		var oid = $location.search().id;
		var p = $scope.data.page_id || 'form';
		var s = {id: p, table: parms.table, sys_id: parms.sys_id, view: $scope.data.view};
		if (oid == p) {
			s.spa = 1;
			var t = $location.search();
			s = angular.extend(t, s);
			$rootScope.$broadcast('$sp.list.click', s);
		}
		$location.search(s);
	});

	$scope.$on('select2.ready', function(e, $el){
		if ($scope.data.invalid_table){
			e.stopPropagation();
			$el.select2('open');
		}
	})

	$scope.selectedTable = {
		displayValue: $scope.data.table,
		value: $scope.data.table
	}

	function resetParams(){
		delete $scope.data.p;
		delete $scope.data.o;
		delete $scope.data.d;
		delete $scope.data.q;
		delete $scope.data.table;	
	}

	$scope.onChange = function() {
		resetParams();
		$scope.data.table = $scope.selectedTable.value;
		$scope.data.fields = "";  // reset
		$scope.data.invalid_table = false;
		getData(true);	
	}

	function getData(updateUrl) {
		var f = $scope.data;
		spUtil.update($scope).then(function(data) {
			$scope.data.dataTableWidget = null;
			$timeout(function(){
				$scope.data.dataTableWidget = data.dataTableWidget;
				angular.extend($scope.data.dataTableWidget.options, $scope.options);
				/*if (updateUrl)
					setPermalink(f.table);*/
			});
		});	
	}

	function setPermalink(table) {
		$scope.ignoreLocationChange = true;
		var searchParms = $location.search();
		var search = {
			spa: 1, 
			table: table,
			id: searchParms.id
		};
		$location.search(search);	
	}
}
2 REPLIES 2

Tanushree Maiti
Tera Patron

Hi @mani55 

 

If you are getting error share that.

Check browser console to check the script related error.

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

there is no error i am setting default view is below like this

c.viewFilter = 'inUse';	

 

View is came like that but based on that table data not coming