Lookup module loading all records

Hi all,

I recently put a svy lookupObj onto one of my forms, but I need the foundset to be confined to records where ‘foundset.new_stock’ = 0. On the button press I’m running in find mode (foundset.find()) and it works on initialization, but when I search in the search box it seems like foundset.loadAllRecords() is run, without the find. Does anyone know where I can edit this?

Here is my code:

function onActionAddStock(event) {
	var stockFoundset = databaseManager.getFoundSet(datasources.db.cpro.stock.getDataSource())
	stockFoundset.find()
	stockFoundset.new_stock = 0;
	var searchResult = stockFoundset.search();
	if (searchResult > 0) {
		var lookupObj = scopes.svyLookup.createLookup(stockFoundset);
		lookupObj.setMultiSelect(true);
		
		// add fields
		lookupObj.addField('make').setTitleText('Make');
		lookupObj.addField('model').setTitleText('Model');
		lookupObj.addField('colour').setTitleText('Colour');
		lookupObj.addField('serial_number').setTitleText('Serial Number');
	    
				
		// show pop-up
		lookupObj.showPopUp(onSelect, elements.btnNewProduct, controller.getFormWidth(), 412);
	}
}

I tried changing the search function in the abstract lookup form but it didn’t seem to work:

function search(txt) {
	if (foundset !== datasources.db.cpro.stock.getFoundSet()) {
		//if we are using a global method valuelist:
		//we need to refresh the foundset using global method
		updateDataSource(txt);
	
		// fix search disappear while typing
		// searchText = txt;
	
		// load all records if no input
		if (!txt) {
			foundset.loadAllRecords();
		}
	
		// create search object
		var simpleSearch = scopes.svySearch.createSimpleSearch(foundset);
		simpleSearch.setSearchText(txt);
	
		// Add search providers
		for (var i = 0; i < lookup.getFieldCount(); i++) {
			var field = lookup.getField(i);
			// TODO check if dataprovider actually exists
			if (field.isSearchable() && field.getDataProvider()) {
				simpleSearch.addSearchProvider(field.getDataProvider()).setAlias(field.getTitleText());
			}
		}
	
		// apply search
		simpleSearch.loadRecords(foundset);
	} else {
		var stockFoundset = datasources.db.cpro.stock.getFoundSet()
		stockFoundset.find()
		stockFoundset.new_stock = 0;
		var searchResult = stockFoundset.search();
		if (searchResult > 0) {
			updateDataSource(txt);
		
			if (!txt) {
				stockFoundset.loadAllRecords();
			}
		
			simpleSearch = scopes.svySearch.createSimpleSearch(stockFoundset);
			simpleSearch.setSearchText(txt);
		
			for (i = 0; i < lookup.getFieldCount(); i++) {
				field = lookup.getField(i);
				// TODO check if dataprovider actually exists
				if (field.isSearchable() && field.getDataProvider()) {
					simpleSearch.addSearchProvider(field.getDataProvider()).setAlias(field.getTitleText());
				}
			}
			simpleSearch.loadRecords(stockFoundset);
		}
	}
}

[attachment=1]Screenshot 2024-03-21 at 08.34.23.png[/attachment]
[attachment=0]Screenshot 2024-03-21 at 08.34.31.png[/attachment]

Thanks!
Alasdair

Figured it out.

If you go to the search(txt) function in the AbstractLookup form, and replace ‘foundset’ with lookup.getFoundSet() it seems to work with edited foundsets. So far haven’t had any issues with any other searches elsewhere.
Heres the code I have now:

function search(txt) {
	//if we are using a global method valuelist:
	//we need to refresh the foundset using global method
	updateDataSource(txt);

	// fix search disappear while typing
	// searchText = txt;

	// load all records if no input
	if (!txt) {
		lookup.getFoundSet().loadAllRecords();
	}

	// create search object
	var simpleSearch = scopes.svySearch.createSimpleSearch(lookup.getFoundSet());
	simpleSearch.setSearchText(txt);

	// Add search providers
	for (var i = 0; i < lookup.getFieldCount(); i++) {
		var field = lookup.getField(i);
		// TODO check if dataprovider actually exists
		if (field.isSearchable() && field.getDataProvider()) {
			simpleSearch.addSearchProvider(field.getDataProvider()).setAlias(field.getTitleText());
		}
	}

	// apply search
	simpleSearch.loadRecords(lookup.getFoundSet());
}

Well done. And thanks for posting this. I am a fair way behind you on the Servoy journey, and these sort of tidbits are really useful.

John
Dorset, UK

Hi,

if you want to filter a foundset used in lookup, the best way is to add foundset’s filter and create the lookup using that foundset.
Using foundset’s filter, any subsequent search of the user will hold the filters containing the user’s search in lookup.

	// filter product foundset with unitprice > 30
	var filteredFS = datasources.db.example_data.products.getFoundSet();
	filteredFS.addFoundSetFilterParam("unitprice",">","30","unitprice");
	filteredFS.sort("unitprice desc");
	filteredFS.loadAllRecords();

	// create lookup object with filtered foundset
	var lookupObj = scopes.svyLookup.createLookup(filteredFS);

See wiki: https://docs.servoy.com/guides/develop/ … ookup-list

Regards,
Paolo

Hi Paolo,

Thanks that makes a lot of sense. I’m kind of wary of messing with the modules because they’re currently too complicated for me so this helps. Do we still need to do ```
filteredFS.removeFoundSerFilterPrarm(‘unitprice’)


Hi John,

Thank you, I guess we're on the journey together.

Hi,

it won’t be necessary to remove the foundset filter in this scenario, because it will be a separate foundset; therefore does not affect any other foundset.
Removing filters is something you normally do when working with shared foundsets ( form’s default )