Skip to content

Instantly share code, notes, and snippets.

@kcabading
Created February 21, 2017 08:43
Show Gist options
  • Save kcabading/3fe0d6026f8dda7210838fd07837b748 to your computer and use it in GitHub Desktop.
Save kcabading/3fe0d6026f8dda7210838fd07837b748 to your computer and use it in GitHub Desktop.
Initialising select2 in sugarcrm sidecar with options to load default list using an app_strings_list or from module's records.
({
_render: function() {
// load default options using app strings
this._setSelectField("element_id_here", "", app.lang.getAppListStrings("cstm_lead_source_list") , false, null, null, null);
// if you want to load default selected value
$('#element_id_here').select2('val','id_in_the_option_list');
// load options from records of a module
this._setSelectField("element_id_of_the_field", "", null, "Accounts", "name", this.getPersonRecordList);
},
getPersonRecordList: function(strName, strModule, fieldname, callback) {
// set local scope
var self = this;
// do we have name
if (strName) {
// setup id as default field to select,
var objFilter = {
"max_num": 10,
"fields": ["id"],
"order_by": "date_modified:DESC"
},
arFilter = [];
switch (fieldname) {
case 'name':
arFilter.push(
{
"name":{
"$contains": strName
}
}
);
// add the filter parameter
objFilter.filter = [
{
"$or": arFilter
}
];
// and add as addition fields
objFilter.fields.push(fieldname);
break;
case 'full_name':
arFilter.push(
{
"first_name":{
"$contains": strName
}
},
{
"last_name":{
"$contains": strName
}
}
);
// add the filter parameter
objFilter.filter = [
{
"$or": arFilter
}
];
// and add as addition fields
objFilter.fields.push(fieldname);
break;
case 'assigned_user_name':
arFilter.push({
"assigned_user_id" : strName
});
// add the filter parameter
objFilter.filter = arFilter;
// and add as addition fields
objFilter.fields.push('full_name');
objFilter.fields.push(fieldname);
default:
break;
}
// build url
var strUrl = app.api.buildURL(strModule, null, null, objFilter);
// call function
app.api.call('GET', strUrl, null, {
success: function (data) {
// initialise result
var objData = {
results: []
};
// Loop through each brand
$.each(data.records, function(strKey, objDetail) {
// Add the result
objData.results.push({
id: objDetail.id,
text: objDetail[fieldname]
});
});
// call select2 callback
callback(objData);
}
});
}
// return
return false;
},
_setSelectField: function(strFieldId, objSelected, arData, strModule, strFieldName, objFunction) {
// initiliase scope
var self = this;
// initialise select2
$("#" + strFieldId).select2({
allowClear: true,
width: '100%',
initSelection: function (element, callback) {
// do we have default value
if (objSelected) {
// initialise default list
callback({id: objSelected.id, text: objSelected.text });
}
},
query: function(query) {
// Initialise the results
var objData = {
results: []
};
// do we have a function call?
if (objFunction != null) {
// call function
objFunction(query.term, strModule, strFieldName, query.callback);
} else {
// Do we have results?
if (arData != undefined) {
// Loop through each brand
$.each(arData, function(strKey, objDetail) {
// Add the result
objData.results.push({
id: strKey,
text: objDetail
});
});
} else {
// Add warning message
objData.results.push({
id: "",
text: "No Data to Load"
});
}
// invoke callback
query.callback(objData);
}
}
});
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment