How to pass the reference value from the HTML to the client controller in the widget

Samiksha2
Mega Sage

Hi,

I want to pass the reference value from the HTML to the client controller in the widget.

Here is my code:

HTML:

<sn-record-picker field="name" table="'sys_user'" id="reference_field" display-field="'name'" display-fields="'name,user_name'" value-field="'sys_id'" search-fields="'name'" page-size="50" ng-change="c.referChange()" ></sn-record-picker>

 

Client controler:


api.controller=function($location,$scope) {
/* widget controller */
var c = this;

c.referChange=function(){

$location.url('sp?id=portal&table=u_user_table&filter=u_user_delegateLIKE'+(what will be here? how to add the value of reference));

}

};

 

Please help in this.

 

Thanks,

Samiksha

13 REPLIES 13

Hi @Syedmd08 ,

ng-change is not working now.

I try to change the reference field value but url is not changing.

If the ng-change directive is not triggering the referChange function, you can try using the ng-model directive to watch for changes to the selected record and trigger the function when a new record is selected.

Here's an updated HTML code that uses the ng-model directive to watch for changes to the selected record:

 

<sn-record-picker field="name" table="'sys_user'" id="reference_field" display-field="'name'" display-fields="'name,user_name'" value-field="'sys_id'" search-fields="'name'" page-size="50" ng-model="c.selectedRecord" ></sn-record-picker>

In the client controller, you can use the $scope.$watch function to monitor changes to the selectedRecord variable and trigger the referChange function when a new record is selected. Here's an updated api.controller function:

 

api.controller = function($location, $scope) {
var c = this;

$scope.$watch('c.selectedRecord', function(newValue, oldValue) {
if (newValue !== oldValue) {
var selectedSysId = c.selectedRecord.sys_id;
$location.url('sp?id=portal&table=u_user_table&filter=u_user_delegateLIKE' + selectedSysId);
}
});
};

 

 

With these changes, the referChange function should be triggered whenever a new record is selected, and the URL should be updated with the selected record's sys_id value.

 

Error is coming.

Samiksha2_0-1678476554384.png

 

one ) was missing. I added now error is not coming.
Again reference field is coming as text box.

It looks like the sn-record-picker widget is not designed to allow the reference value to be passed directly to the client controller.

To work around this limitation, you can use a hidden input field to store the selected reference value and then use the ng-model directive to bind the value to a variable in the scope of your controller.

Here's an updated HTML code that includes a hidden input field to store the selected reference value:

 

<input type="hidden" id="reference_field" ng-model="c.selectedReference">

<sn-record-picker field="name" table="'sys_user'" id="reference_field_picker" display-field="'name'" display-fields="'name,user_name'" value-field="'sys_id'" search-fields="'name'" page-size="50" ng-change="c.referChange()" ></sn-record-picker>

 

In the client controller, you can access the value of the reference field using the $scope object and the variable name that you bound the field to in the HTML (c.selectedReference in this case). Here's an updated referChange function that includes the reference value in the URL:

 

api.controller = function($location, $scope) {
var c = this;

c.referChange = function() {
var selectedSysId = $scope.c.selectedReference;
$location.url('sp?id=portal&table=u_user_table&filter=u_user_delegateLIKE' + selectedSysId);
};
};

 

With these changes, the referChange function should now receive the value of the reference field when it is changed by the user in the sn-record-picker.