[Vtigercrm-developers] Multiple contacts connected to a single field
Sukhdev Mohan
s.mohan at myti.it
Mon Oct 5 21:07:21 GMT 2020
Hi Uma,
So I’ve managed to select, save, edit and view a single field. I’m now facing problem to add this functionality to other 4 fields. I’m sharing the Edit.js
Since I wanted to avoid duplicating registerRelatedContactSpecificEvents I’ve tried to modularise by defining an array of elements and cycle on it (not the most elegant solution I recon) But as you can guess this doesn’t work: when the pop up opens it’s fine, when I select the the contacts it give me error "Uncaught TypeError: element.data(...) is undefined: Im guessing the loop isn’t working as I thought. Can you (*or anybody else) help me out editing this code to work for my use case?
/*+***********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
*************************************************************************************/
Vtiger_Edit_Js("Processi_Edit_Js",{
},{
relatedContactElement : false,
elementwithContacts: [
'mbs_contact',
'bsm_contact',
'bi_contact',
'altri_contact',
'comune_contact'
],
getRelatedContactElement : function(form, element) {
if(typeof form == "undefined") {
form = this.getForm();
}
this.relatedContactElement = jQuery(`#${element}_display`, form);
return this.relatedContactElement;
},
openPopUp : function(e){
var thisInstance = this;
var parentElem = thisInstance.getParentElement(jQuery(e.target));
var params = this.getPopUpParams(parentElem);
params.view = 'Popup';
var isMultiple = false;
if(params.multi_select) {
isMultiple = true;
}
var sourceFieldElement = jQuery('input[class="sourceField"]',parentElem);
var prePopupOpenEvent = jQuery.Event(Vtiger_Edit_Js.preReferencePopUpOpenEvent);
sourceFieldElement.trigger(prePopupOpenEvent);
if(prePopupOpenEvent.isDefaultPrevented()) {
return ;
}
var popupInstance = Vtiger_Popup_Js.getInstance();
popupInstance.showPopup(params,function(data){
var responseData = JSON.parse(data);
var dataList = new Array();
for(var id in responseData){
var data = {
'name' : responseData[id].name,
'id' : id
}
dataList.push(data);
if(!isMultiple) {
thisInstance.setReferenceFieldValue(parentElem, data);
}
}
if(isMultiple) {
sourceFieldElement.trigger(Vtiger_Edit_Js.refrenceMultiSelectionEvent,{'data':dataList});
}
sourceFieldElement.trigger(Vtiger_Edit_Js.postReferenceSelectionEvent,{'data':responseData});
});
},
registerRelatedContactSpecificEvents : function(form) {
console.log(form);
this.elementwithContacts.forEach(element => {
console.log(element);
var thisInstance = this;
if(typeof form == "undefined") {
form = this.getForm();
}
form.find(`[name="${element}"]`).on(Vtiger_Edit_Js.preReferencePopUpOpenEvent,function(e){
var parentIdElement = form.find(`[name="${element}"]`);
var container = parentIdElement.closest('td');
var popupReferenceModule = jQuery('input[name="popupReferenceModule"]',container).val();
})
this.getRelatedContactElement(form).select2({
minimumInputLength: 3,
ajax : {
'url' : 'index.php?module=Contacts&action=BasicAjax&search_module=Contacts',
'dataType' : 'json',
'data' : function(term,page){
var data = {};
data['search_value'] = term;
return data;
},
'results' : function(data){
data.results = data.result;
for(var index in data.results ) {
var resultData = data.result[index];
resultData.text = resultData.label;
}
return data
},
transport : function(params){
return jQuery.ajax(params);
}
},
multiple : true,
//To Make the menu come up in the case of quick create
dropdownCss : {'z-index' : '10001'}
});
//To add multiple selected contact from popup
form.find(`[name="${element}"]`).on(Vtiger_Edit_Js.refrenceMultiSelectionEvent,function(e,result){
thisInstance.addNewContactToRelatedList(result,form, element);
});
this.fillRelatedContacts(form, element);
});
},
/**
* Function to get reference search params
*/
getReferenceSearchParams : function(element){
var tdElement = jQuery(element).closest('td');
var params = {};
var previousTd = tdElement.prev();
var multiModuleElement = jQuery('select.referenceModulesList', previousTd);
var referenceModuleElement;
if(multiModuleElement.length) {
referenceModuleElement = multiModuleElement;
} else {
referenceModuleElement = jQuery('input[name="popupReferenceModule"]',tdElement).length ?
jQuery('input[name="popupReferenceModule"]',tdElement) : jQuery('input.popupReferenceModule',tdElement);
}
var searchModule = referenceModuleElement.val();
params.search_module = searchModule;
return params;
},
initializeContactIdList : function(form, element) {
var relatedContactElement = this.getRelatedContactElement(form, element);
if(this.isEvents(form) && relatedContactElement.length) {
jQuery(`<input type="hidden" name=${element}_contactidlist />`).appendTo(form).val(relatedContactElement.val().split(',').join(';'));
form.find(`[name="${element}"]`).attr('name','');
}
},
registerRecordPreSaveEvent : function(form) {
var thisInstance = this;
if(typeof form === "undefined") {
form = this.getForm();
}
var InitialFormData = form.serialize();
app.event.one(Vtiger_Edit_Js.recordPresaveEvent,function(e) {
thisInstance.registerRecurringEditOptions(e,form,InitialFormData);
thisInstance.addInviteesIds(form);
thisInstance.resetRecurringDetailsIfDisabled(form);
thisInstance.initializeContactIdList(form);
});
},
/**
* Function which will fill the already saved contacts on load
*/
fillRelatedContacts : function(form, element) {
if(typeof form == "undefined") {
form = this.getForm();
}
var relatedContactValue = form.find(`[name="related_${element}_info"]`).data('value');
for(var contactId in relatedContactValue) {
var info = relatedContactValue[contactId];
info.text = info.name;
relatedContactValue[contactId] = info;
}
this.getRelatedContactElement(form, element).select2('data',relatedContactValue);
},
addNewContactToRelatedList : function(newContactInfo, form, element){
if(form.length <= 0) {
form = this.getForm();
}
var resultentData = new Array();
var element = jQuery(`#${element}_display`, form);
var selectContainer = jQuery(element.data('select2').container, form);
var choices = selectContainer.find('.select2-search-choice');
choices.each(function(index,element){
resultentData.push(jQuery(element).data('select2-data'));
});
var select2FormatedResult = newContactInfo.data;
for(var i=0 ; i < select2FormatedResult.length; i++) {
var recordResult = select2FormatedResult[i];
recordResult.text = recordResult.name;
resultentData.push( recordResult );
}
element.select2('data',resultentData);
if(form.find('.quickCreateContent').length > 0) {
form.find(`[name="related_${element}_info"]`).data('value', resultentData);
var relatedContactElement = this.getRelatedContactElement(form);
if(relatedContactElement.length > 0) {
jQuery(`<input type="hidden" name=${element}_contactidlist />`).appendTo(form).val(relatedContactElement.val().split(',').join(';'));
form.find(`[name="${element}"]`).attr('name','');
}
}
},
registerBasicEvents : function(container) {
this._super(container);
this.registerRecordPreSaveEvent(container);
this.registerRelatedContactSpecificEvents(container);
}
});
Best Regards,
Sukhdev Mohan | Software Developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20201005/632821aa/attachment-0001.html>
More information about the vtigercrm-developers
mailing list