How can I use areConditionsMet/filterRowsByConditions in skuid.utils?

  • 1
  • Question
  • Updated 2 years ago
  • Answered
  • (Edited)
areConditionsMet 
is displayed in the official documentation but doesn't have any comments yet.


filterRowsByConditions 
sounds more useful for what I want to do (client-side filtering) but is not displayed in the official documentation.


Has anyone used this? Or can an employee pitch in an unofficial mention of the number of parameters and their types?


Thanks!
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb

Posted 2 years ago

  • 1
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb
I had to solve this one quickly so I wrote a function that checks conditions for only boolean type condition/values.

//only handles true and false values now
checkcondition = function(row,condition){
	
	//used in the eval expression. If we used '=' instead of '==' we'd get an assignment error.
	operatermap = {}; 
	operatermap['='] = '==';
	operatermap['!='] = '!=';
	
	condVal = condition.value;
	//convert to string since condition values are of string type
	rowVal = row[condition.field].toString(); 
	
	if(condVal != "true" && condVal!= "false"){
		
		throw new Error("The condition provided is not a boolean type. Currently only boolean types are supported for checking.");
		
	}
	if(rowVal != "true" && rowVal!= "false"){
		
		throw new Error("The row field provided is not a boolean type. Currently only boolean types are supported for checking.");
		
	}

	return eval(rowVal + operatermap[condition.operator] + condVal);
		
}
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb
Also some complimentary functions are:

//checks multiple conditions for a row and returns the row if it matches all conditions given
//'row' is a single skuid row
//'conditions' is an array of skuid conditions
checkconditions = function(row,conditions){
		
	checkconditionsWrapper = function(cond){
		
		return checkcondition(row,cond);
		
	};
	
	return conditions.every(checkconditionsWrapper);
	
};


//checks multiple conditions on multiple rows and returns all rows that match all conditions
//'rows' is an array of skuid data rows
//'conditions' is an array of skuid conditions (you can put an array of a single condition if necessary)
filterRowsByConditions = function(rows,conditions){

	rowFilterWrapper  = function(row){
			
		return checkconditions(row,conditions);
		
	};
	
	return rows.filter(rowFilterWrapper);
	
};