Help - how to hand over value from on widget to an other? SCOPED ...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2024 07:05 AM
I need a widget letting me select a user ... this selection is to be passed to an other widget.
Somehow changes made to the user selection will not be handed over to the 2nd widget ...
I have no idea about html or angular .. and strugle here.
This the simple setup ...
Widget 1 HTML
<div class="request-for-container">
<label for="userSelect" class="request-for-label">Request for:</label>
<input type="text" ng-model="selectedUser.name" readonly ng-if="!data.canRequestOnBehalf" class="readonly-input" placeholder="Request for: {{selectedUser.name}}">
<select ng-if="data.canRequestOnBehalf"
ng-model="selectedUserId"
ng-options="user.sys_id as user.name for user in data.activeUsers"
ng-change="updateUserSelection()"
class="modern-select">
<option value="">Select a user...</option>
</select>
</div>
Widget 1 Client script
function($scope, $rootScope) {
// Initialize selectedUser with the current user's details
$scope.selectedUser = {
sys_id: $scope.data.userSysId,
name: $scope.data.userDisplayName
};
// Initialize selectedUserId
$scope.selectedUserId = $scope.selectedUser.sys_id;
// Function to handle user selection
$scope.updateUserSelection = function() {
var selectedUser = $scope.data.activeUsers.find(function(user) {
return user.sys_id === $scope.selectedUserId;
});
if (selectedUser) {
$scope.selectedUser = selectedUser;
console.log("Emitting selected user:", angular.copy($scope.selectedUser));
$rootScope.$emit('userSelected', angular.copy($scope.selectedUser)); // Use $rootScope to broadcast
}
};
// Initial user selection emission
$scope.updateUserSelection();
// Watch for changes to the selected userId
$scope.$watch('selectedUserId', function(newValue, oldValue) {
if (newValue && (newValue !== oldValue)) {
console.log("User ID Changed to:", newValue);
$scope.updateUserSelection();
}
});
}
Widget 1 Server Script
(function() {
data.userSysId = gs.getUserID();
data.userDisplayName = gs.getUserDisplayName();
data.activeUsers = [];
data.canRequestOnBehalf = false;
// Retrieve the comma-separated list of roles from the property
var allowedRoles = 'itil'; // or more
// Check if the user has any of the allowed roles
for (var i = 0; i < allowedRoles.length; i++) {
var role = allowedRoles[i].trim();
if (gs.hasRole(role)) {
data.canRequestOnBehalf = true;
gs.info("User has the role: " + role); // Log role confirmation
break;
}
}
// Log if no roles were found
if (!data.canRequestOnBehalf) {
gs.info("User does not have any of the allowed roles.");
}
// Populate active users if the user has permission
if (data.canRequestOnBehalf) {
var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.query();
while (gr.next()) {
data.activeUsers.push({
sys_id: gr.getValue('sys_id'),
name: gr.getDisplayValue()
});
}
}
})();
Widget 2 HTML:
<div class="user-listener-container">
<h3>Selected User Overview</h3>
<div>
<p>Currently Selected User: {{currentUser ? currentUser.name : 'None'}}</p>
</div>
</div>
Widget 2 Client Script:
function($scope, $rootScope) {
// Initialize currentUser
$scope.currentUser = null;
// Listen for the 'userSelected' event
$rootScope.$on('userSelected', function(event, user) {
if (user) {
$scope.currentUser = user; // Update the displayed user
alert("New User Selected: " + $scope.currentUser.name); // Alert for confirmation
console.log("User Selected:", $scope.currentUser); // Log the selected user
}
});
}
.. no matter hat user i pick in widget 1, alway my user is mentioned in the alert.
If someone could help my who to change this to alert the newly selected user, I could proceed from there ...
Thank you!! ChatCPT does not admit that it has no idea and tell me a lot of things but none worked ... I hope you know better ... 😉
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2024 11:28 AM - edited 10-30-2024 11:34 AM
Hi @Zod ,
You can understand this with some basic example shown and explained in the below link,
Communication between two widgets
You can just go through this community thread as well,
Pass the value from one widget to another
You can have a look at this nice YT video as well,
https://www.youtube.com/watch?v=e_mafzZbUAs
If this address your question, please mark this response correct by clicking on Accept as Solution and/or Kudos.
You may mark this helpful as well if it helps you.
Thanks,
Animesh Das
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 10:42 PM