Lookup module loading all records

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Lookup module loading all records

Postby alasdairs » Thu Mar 21, 2024 10:24 am

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:
Code: Select all
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:
Code: Select all
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);
      }
   }
}


Screenshot 2024-03-21 at 08.34.23.png
Screenshot 2024-03-21 at 08.34.23.png (23.84 KiB) Viewed 641 times

Screenshot 2024-03-21 at 08.34.31.png
Screenshot 2024-03-21 at 08.34.31.png (25.16 KiB) Viewed 641 times


Thanks!
Alasdair
Software Engineer
alasdairs
 
Posts: 45
Joined: Wed Dec 27, 2023 10:06 pm
Location: The Internet

Re: Lookup module loading all records

Postby alasdairs » Thu Mar 21, 2024 1:13 pm

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:
Code: Select all
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());
}
Software Engineer
alasdairs
 
Posts: 45
Joined: Wed Dec 27, 2023 10:06 pm
Location: The Internet

Re: Lookup module loading all records

Postby harbourj » Thu Mar 21, 2024 1:36 pm

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
harbourj
 
Posts: 6
Joined: Thu Nov 30, 2023 10:56 am

Re: Lookup module loading all records

Postby paronne » Thu Mar 21, 2024 1:41 pm

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.

Code: Select all
   // 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
paronne
 
Posts: 203
Joined: Fri Nov 02, 2012 3:21 pm

Re: Lookup module loading all records

Postby alasdairs » Fri Mar 22, 2024 12:02 pm

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
Code: Select all
filteredFS.removeFoundSerFilterPrarm('unitprice')
after creating a filter? I tried without and it doesn't seem to mess with the foundsets but heard that its good practice to do so.



Hi John,

Thank you, I guess we're on the journey together.
Software Engineer
alasdairs
 
Posts: 45
Joined: Wed Dec 27, 2023 10:06 pm
Location: The Internet

Re: Lookup module loading all records

Postby paronne » Fri Mar 22, 2024 4:34 pm

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 )
paronne
 
Posts: 203
Joined: Fri Nov 02, 2012 3:21 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 22 guests

cron