Skip to content

Instantly share code, notes, and snippets.

@BuZZ-dEE
Created June 13, 2014 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BuZZ-dEE/038db8e111824eb78efb to your computer and use it in GitHub Desktop.
Save BuZZ-dEE/038db8e111824eb78efb to your computer and use it in GitHub Desktop.
Typeahead for clients and customer during typing.
/**
* @type {JSDataSet}
*
* @private
*/
var clientCache = null;
/**
* @type {JSDataSet}
*
* @private
*/
var customerCache = null;
/**
* @type {JSFoundSet}
*
* @private
*/
var clientFs = null;
/**
* @type {JSFoundSet}
*
* @private
*/
var customerFs = null;
/**
* Called when the client valuelist needs data, it has 3 modes.
* real and display params both null: return the whole list
* only display is specified, called by a typeahead, return a filtered list
* only real value is specified, called when the list doesnt contain the real value for the give record value, this will insert this value into the existing list
*
* @param {String} displayValue The value of a lookupfield that a user types
* @param realValue The real value for a lookupfield where a display value should be get for
*
* @returns {JSDataSet} A dataset with 1 or 2 columns display[,real]
*
* @AllowToRunInFind
*/
function clientSearchSuggestions( displayValue, realValue ) {
if ( !clientFs ) {
/** @type JSFoundSet<db:/boss_sql/mandpara> */
var fs = databaseManager.getFoundSet( 'db:/boss_sql/mandpara' );
fs.addFoundSetFilterParam( 'mandnr', '<', 100000, 'Clients without foreign vessels and seamen' );
fs.addFoundSetFilterParam( 'schiff', '=', 1, 'Clients with vessels' );
fs.loadAllRecords( );
clientFs = fs;
} else {
fs = clientFs;
}
/** @type {JSDataSet} */
var result = null;
result = searchSuggestions( displayValue, realValue, fs, 'name', 'mandnr' );
return result;
}
/**
* Called when the customer valuelist needs data, it has 3 modes.
* real and display params both null: return the whole list
* only display is specified, called by a typeahead, return a filtered list
* only real value is specified, called when the list doesnt contain the real value for the give record value, this will insert this value into the existing list
*
* @param {String} displayValue The value of a lookupfield that a user types
* @param realValue The real value for a lookupfield where a display value should be get for
*
* @returns {JSDataSet} A dataset with 1 or 2 columns display[,real]
*
* @AllowToRunInFind
*/
function customerSearchSuggestions( displayValue, realValue ) {
if ( !customerFs ) {
/** @type JSFoundSet<db:/boss_sql/kunden> */
var fs = databaseManager.getFoundSet( 'db:/boss_sql/kunden' );
fs.addFoundSetFilterParam( 'firma', '!=', '', 'nur_firmen_anzeigen' );
fs.loadAllRecords( );
customerFs = fs;
} else {
fs = customerFs;
}
/** @type {JSDataSet} */
var result = null;
result = searchSuggestions( displayValue, realValue, fs, 'firma', 'kdnr' );
return result;
}
/**
* Fills the type ahead client and customer valuelist.
*
* @param {String} displayValue , the diplay value
* @param {String} realValue , the real value
* @param {JSFoundSet} foundSet , the foundset from which the type ahead values are get
* @param {String} displayColumn , the column which is used for showing
* @param {String} [realColumn] , the column which is used for further work
*
* @returns {JSDataSet} A dataset with 1 or 2 columns display[,real]
*
* @see #clientSearchSuggestions()
* @see #customerSearchSuggestions()
*
* @AllowToRunInFind
* @private
*/
function searchSuggestions( displayValue, realValue, foundSet, displayColumn, realColumn ) {
/** @type {JSDataSet} */
var result = null;
if ( !realColumn ) {
realColumn = displayColumn;
}
if ( displayValue == null ) { //field is empty
foundSet.sort( displayColumn + ' asc' );
if ( displayColumn === 'name' && clientCache ) {
return clientCache;
} else if ( displayColumn === 'firma' && customerCache ) {
return customerCache;
} else {
result = databaseManager.convertToDataSet( foundSet, [ displayColumn, realColumn ] ); //[display value, real value]
}
if ( displayColumn === 'name' ) {
clientCache = result;
} else if ( displayColumn === 'firma' ) {
customerCache = result;
}
} else if ( displayValue != null ) { //field has text
var searchText = utils.stringTrim( displayValue );
var searchArray = searchText.split( ' ' );
if ( foundSet.find( ) ) {
foundSet[ displayColumn ] = '#%' + searchArray[ 0 ] + '%';
foundSet.search( );
for ( var i = 1; i < searchArray.length; i++ ) {
if ( foundSet.find( ) ) {
foundSet[ displayColumn ] = '#%' + searchArray[ i ] + '%';
foundSet.search( false, true );
}
}
}
foundSet.sort( displayColumn + ' asc');
result = databaseManager.convertToDataSet(foundSet, [ displayColumn, realColumn ] );
}
result.removeColumn( 2 );
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment