NEW Add a new widget $form->selectArrayAjax() to use combo list with

content coming from an Ajax URL.
This commit is contained in:
Laurent Destailleur 2015-10-26 11:23:31 +01:00
parent 132bc610b6
commit 71da059ef0
3 changed files with 33 additions and 22 deletions

View File

@ -44,14 +44,14 @@ if ((( ! empty($conf->societe->enabled) && (empty($conf->global->SOCIETE_DISABLE
{
$langs->load("companies");
//$searchform.=printSearchForm(DOL_URL_ROOT.'/societe/list.php', DOL_URL_ROOT.'/societe/list.php', $langs->trans("ThirdParties"), 'soc', 'sall', 'T', 'searchleftt', img_object('','company'));
$arrayresult['searchintothirdparty']=img_picto('','object_company').' '.$langs->trans("SearchIntoThirdparties", $search_boxvalue);
$arrayresult['searchintothirdparty']=array('text'=>img_picto('','object_company').' '.$langs->trans("SearchIntoThirdparties", $search_boxvalue), 'url'=>DOL_URL_ROOT.'/societe/list.php?sall='.urlencode($search_boxvalue));
}
if (! empty($conf->societe->enabled) && ! empty($conf->global->MAIN_SEARCHFORM_CONTACT) && $user->rights->societe->lire)
{
$langs->load("companies");
//$searchform.=printSearchForm(DOL_URL_ROOT.'/contact/list.php', DOL_URL_ROOT.'/contact/list.php', $langs->trans("Contacts"), 'contact', 'sall', 'A', 'searchleftc', img_object('','contact'));
$arrayresult['searchintocontact']=img_picto('','object_contact').' '.$langs->trans("SearchIntoContacts", $search_boxvalue);
$arrayresult['searchintocontact']=array('text'=>img_picto('','object_contact').' '.$langs->trans("SearchIntoContacts", $search_boxvalue), 'url'=>DOL_URL_ROOT.'/contact/list.php?sall='.urlencode($search_boxvalue));
}
if (((! empty($conf->product->enabled) && $user->rights->produit->lire) || (! empty($conf->service->enabled) && $user->rights->service->lire))
@ -59,7 +59,7 @@ if (((! empty($conf->product->enabled) && $user->rights->produit->lire) || (! em
{
$langs->load("products");
//$searchform.=printSearchForm(DOL_URL_ROOT.'/product/list.php', DOL_URL_ROOT.'/product/list.php', $langs->trans("Products")."/".$langs->trans("Services"), 'products', 'sall', 'P', 'searchleftp', img_object('','product'));
$arrayresult['searchintoproduct']=img_picto('','object_product').' '.$langs->trans("SearchIntoProductsOrServices", $search_boxvalue);
$arrayresult['searchintoproduct']=array('text'=>img_picto('','object_product').' '.$langs->trans("SearchIntoProductsOrServices", $search_boxvalue), 'url'=>DOL_URL_ROOT.'/product/list.php?sall='.urlencode($search_boxvalue));
}
/*if (((! empty($conf->product->enabled) && $user->rights->produit->lire) || (! empty($conf->service->enabled) && $user->rights->service->lire)) && ! empty($conf->fournisseur->enabled)
@ -73,14 +73,14 @@ if (! empty($conf->adherent->enabled) && ! empty($conf->global->MAIN_SEARCHFORM_
{
$langs->load("members");
//$searchform.=printSearchForm(DOL_URL_ROOT.'/adherents/list.php', DOL_URL_ROOT.'/adherents/list.php', $langs->trans("Members"), 'member', 'sall', 'M', 'searchleftm', img_object('','user'));
$arrayresult['searchintomember']=img_picto('','object_user').' '.$langs->trans("SearchIntoMembers", $search_boxvalue);
$arrayresult['searchintomember']=array('text'=>img_picto('','object_user').' '.$langs->trans("SearchIntoMembers", $search_boxvalue), 'url'=>DOL_URL_ROOT.'/adherents/list.php?sall='.urlencode($search_boxvalue));
}
if (! empty($conf->projet->enabled) && ! empty($conf->global->MAIN_SEARCHFORM_PROJECT) && $user->rights->projet->lire)
{
$langs->load("projects");
//$searchform.=printSearchForm(DOL_URL_ROOT.'/projet/list.php', DOL_URL_ROOT.'/projet/list.php', $langs->trans("Projects"), 'project', 'search_all', 'Q', 'searchleftproj', img_object('','projectpub'));
$arrayresult['searchintoprojects']=img_picto('','object_projectpub').' '.$langs->trans("SearchIntoProjects", $search_boxvalue);
$arrayresult['searchintoprojects']=array('text'=>img_picto('','object_projectpub').' '.$langs->trans("SearchIntoProjects", $search_boxvalue), 'url'=>DOL_URL_ROOT.'/projet/list.php?search_all='.urlencode($search_boxvalue));
}
// Execute hook printSearchForm

View File

@ -4504,16 +4504,17 @@ class Form
* Note: Do not apply langs->trans function on returned content of Ajax service, content may be entity encoded twice.
*
* @param string $htmlname Name of html select area
* @param string $url Url
* @param string $url Url. Must return a json_encode of array(key=>array('text'=>'A text', 'url'=>'An url'), ...)
* @param string $id Preselected key
* @param string $moreparam Add more parameters onto the select tag
* @param string $moreparamtourl Add more parameters onto the Ajax called URL
* @param int $disabled Html select box is disabled
* @param int $minimumInputLength Minimum Input Length
* @param string $morecss Add more class to css styles
* @param int $callurlonselect If set to 1, some code is added so an url return by the ajax is called when value is selected.
* @return string HTML select string.
*/
static function selectArrayAjax($htmlname, $url, $id='', $moreparam='', $moreparamtourl='', $disabled=0, $minimumInputLength=1, $morecss='')
static function selectArrayAjax($htmlname, $url, $id='', $moreparam='', $moreparamtourl='', $disabled=0, $minimumInputLength=1, $morecss='', $callurlonselect=0)
{
$out = '';
@ -4521,7 +4522,10 @@ class Form
$out.='<!-- JS CODE TO ENABLE '.$tmpplugin.' for id '.$htmlname.' -->
<script type="text/javascript">
$(document).ready(function () {
$(".'.$htmlname.'").select2({
'.($callurlonselect ? 'var saveRemoteData = [];':'').'
$(".'.$htmlname.'").select2({
ajax: {
dir: "ltr",
url: "'.$url.'",
@ -4534,12 +4538,13 @@ class Form
};
},
results: function (remoteData, pageNumber, query) {
//console.log(remoteData);
console.log(remoteData);
saveRemoteData = remoteData;
/* format json result for select2 */
result = []
$.each( remoteData, function( key, value ) {
result.push({id: key, text: value});
result.push({id: key, text: value.text});
});
//console.log(result);
//return {results:[{id:\'none\', text:\'aa\'}, {id:\'rrr\', text:\'Red\'},{id:\'bbb\', text:\'Search a into projects\'}], more:false}
return {results: result, more:false}
},
@ -4556,17 +4561,23 @@ class Form
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: '.$minimumInputLength.',
//templateResult: formatRepo, // omitted for brevity, see the source of this page
//templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
formatResult: function(result, container, query, escapeMarkup) {
return escapeMarkup(result.text);
}
});
$(".'.$htmlname.'").change(function() {
alert(\'eee\');
/* $(".'.$htmlname.'").select2("search",""); */
$(".'.$htmlname.'").select2("val",""); /* reset combo box */
} );
'.($callurlonselect ? '
$(".'.$htmlname.'").change(function() {
var selected = $(".'.$htmlname.'").select2("val");
$(".'.$htmlname.'").select2("val",""); /* reset visible combo value */
$.each( saveRemoteData, function( key, value ) {
if (key == selected)
{
console.log("Do a redirect into selectArrayAjax to "+value.url)
location.assign(value.url);
}
});
});' : '' ) . '
});
</script>';

View File

@ -64,11 +64,11 @@ print $form->selectarray('selectarray',$array);
print '<br><br>'."\n";
print "Test 4d: a select with ajax refresh<br>\n";
print "Test 4d: a select with ajax refresh and with onchange call of url<br>\n";
//$array=array(0=>'',1=>'Search into xxx',2=>'Search into yyy',3=>'Search into zzz');
$array=array();
$selected=-1;
print $form->selectArrayAjax('testselectc', DOL_URL_ROOT.'/core/ajax/selectsearchbox.php', $selected, 'style="min-width: 250px;"', '', 0, 1, '');
print $form->selectArrayAjax('testselectc', DOL_URL_ROOT.'/core/ajax/selectsearchbox.php', $selected, 'style="min-width: 250px;"', '', 0, 1, '', 1);
print '<br><br>'."\n";