load model data on page load

  • 1
  • Question
  • Updated 3 years ago
  • Answered
We have "The load model data on page load" set to False on the model because we want the users to use the filters first before they see the data. The problem we are facing when the filters are persisted and users comes back again the data is not loaded even though all the filters are properly set. Is there any way we can check and see if the filters are set then load model data on page load is set to True? 
Photo of Vineet

Vineet

  • 492 Points 250 badge 2x thumb

Posted 3 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
You could use Inline JavaScript that's run on page load to check the values of the Filters' associated Model Conditions, and if the values are set, then query the Models, e.g. like this:

(function(skuid){   
    var $ = skuid.$;
    $(document.body).one('pageload',function(){
       
        //
        // NAMES OF MODEL CONDITIONS NEEDING VALUES, KEYED BY MODEL NAME
        // 
        var modelConditionsNeedingValues = {
            'Account': ['Type','__autofilter__OwnerId'],
            'Contacts': ['__autofilter__AccountId','HasRelatedTasks','SomeOtherConditionName']
        };
        
        var modelsToQuery = [];
        $.each(modelConditionsNeedingValues,function(modelName,conditionNames){
            var model = skuid.$M(modelName),
                queryModel;
            if (model) {
                queryModel = true;
                $.each(conditionNames,function(i,conditionName){
                    var condition = model.getConditionByName(conditionName,true),
                        haveValue = false;
                    if (condition && (
                        // Check single-value Conditions
                        (condition.value || (condition.value===false) || (condition.value===0))
                        // Check multi-value Conditions
                        || (condition.values && condition.values.length)
                    )) {
                        haveValue = true;
                    }
                    if (!haveValue) {
                        queryModel = false;
                        return false;
                    }
                });
                if (queryModel) {
                    modelsToQuery.push(model);
                }
            }
        });
        if (modelsToQuery.length) {
            skuid.model.updateData(modelsToQuery);
        }
    });
})(skuid);
Photo of Vineet

Vineet

  • 492 Points 250 badge 2x thumb
Zach, thanks for sharing the code. You are checking for false in the haveValue variable in the if condition. If we have 3 filters and first one is set and the other two are not wouldn't then the haveValue variable will always return false and rest of the code will not execute?
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Yes this code is saying "Unless we have values for all Filters' Conditions, then don't query the Model". You would need to adjust the logic if you wanted this behavior: "if we have values for ANY Conditions, then Query the Models".
Photo of Vineet

Vineet

  • 492 Points 250 badge 2x thumb
Ok, thanks for help resolving the problem. Posting the modified code in case someone needs it 'if any one filter is populated'.

(function(skuid){    
    var $ = skuid.$;
    $(document.body).on('pageload',function(){
       
        //
        // NAMES OF MODEL CONDITIONS NEEDING VALUES, KEYED BY MODEL NAME
        // 
        var modelConditionsNeedingValues = {
            'Account': ['Type','__autofilter__OwnerId'],
            'Contacts': ['__autofilter__AccountId','HasRelatedTasks','SomeOtherConditionName']
            
        };
        
        var modelsToQuery = [];
        $.each(modelConditionsNeedingValues,function(modelName,conditionNames){
            var model = skuid.$M(modelName),
                queryModel;
                
            if (model) {
                queryModel = false;
                $.each(conditionNames,function(i,conditionName){
                    var condition = model.getConditionByName(conditionName,true),
                        haveValue = false;
                       
                    if (condition && (
                        // Check single-value Conditions
                        (condition.value || (condition.value===false) || (condition.value===0))
                        // Check multi-value Conditions
                        || (condition.values && condition.values.length)
                    )) {
                        haveValue = true;
                    }
                    
                    
                    if (haveValue) {
                        queryModel = true;
                        return false;
                    }
                });
                if (queryModel) {
                    modelsToQuery.push(model);
                }
            }
        });
        if (modelsToQuery.length) {
            skuid.model.updateData(modelsToQuery);
        }
    });
})(skuid);
(Edited)