11-17-2015, 04:09 PM
Now I've done that as well. The jQuery is now expanded. The autocomplete call for sales orders is now like:
And a few line changed in the JSON.php file:
Replaced the old query. Now, when you've selected an account, the sales order autocomplete will filter to show only sales orders from this account.
PHP Code:
jQuery( "#so_autocomplete" ).autocomplete({
// Beware: Autocomplete sets a search term as GET parameter to the URL
// The php file has to take this into account and use the search term
// in its mySQL query
source: "http://crmdevelop.cbx-nederland.nl/index.php?module=Calendar4You&action=Calendar4YouAjax&searchmodule=SalesOrders&file=JSON",
// On open we have to set a high z-index to the UL, else it will
// fall behind the 'addEvent' UI box
open: function(){
jQuery(this).autocomplete('widget').css('z-index', 999999);
return false;
},
// Here we add the result on selection to a hidden input field
// That the calendar module uses to connect a crmentity
select: function( event, ui) {
// Add crmentity id to hidden input
jQuery('input[name=so_rel]').attr('value',ui.item.value);
// Overwrite the input field with the salesorder name in stead of the crmentity
jQuery('#so_autocomplete').val(ui.item.label);
return false;
},
// Also update the input field with the label during focus
focus: function( event, ui ) {
// Overwrite the input field with the salesorder name in stead of the crmentity
jQuery('#so_autocomplete').val(ui.item.label);
return false;
},
minLength: 2
// Make sure browser 'autocomplete' is turned on for this field
}).attr("autocomplete","off")
// Add '_renderItem' to use HTML in the results, so we can use
// things like address fields and better readable markup
.autocomplete( "instance" )._renderItem = function( ul, item ) {
return jQuery( "<li>" )
.append( "<a>" + "<b>" + item.label + "</b>" + "<br>" + item.subject + "<br>" + item.accountname + "</a>" )
.appendTo( ul );
};
// During typing, dynamically change the autocomplete source to check for accountID
jQuery("#so_autocomplete").keyup(function(){
jQuery( "#so_autocomplete" ).autocomplete("option","source","http://crmdevelop.cbx-nederland.nl/index.php?module=Calendar4You&action=Calendar4YouAjax&searchmodule=SalesOrders&accountid="+jQuery('input[name=parent_id]').val()+"&file=JSON");
});
And a few line changed in the JSON.php file:
PHP Code:
// Get the salesorders that match part of the search term
// Check to see if there was an account ID passed in the URL
if (isset($_REQUEST['accountid']) && $_REQUEST['accountid'] != "") {
// If account ID was set, use it in the query
$soAccountID = $_REQUEST['accountid'];
$soresults = $adb->query("SELECT subject, salesorderid, salesorder_no, accountid FROM vtiger_salesorder WHERE salesorder_no LIKE '%$term%' AND accountid = $soAccountID");
} else {
// If no account was selected
$soresults = $adb->query("SELECT subject, salesorderid, salesorder_no, accountid FROM vtiger_salesorder WHERE salesorder_no LIKE '%$term%'");
}
Replaced the old query. Now, when you've selected an account, the sales order autocomplete will filter to show only sales orders from this account.