我有一个评估学生成绩的系统。我应该能够在JSON对象中动态定义评估标准。例如,如果出现以下情况,学生将通过Java编程模块:
(courseWorkMarks > 30 && inClassTest > 40) || examMarks > 40 // pass
条件因模块而异,因此每个模块都有一个描述评估标准的json对象。我想过使用Jquery QueryBuilder来做这件事。我可以使用它的UI构建公式并将其保存为JSON,并且我在JSON对象中有一个模块的学生标记列表,如下所示:
{courseWorkMarks: 50, inClassTest: 35, examMarks: 45}
如何使用jQuery QueryBuilder生成的JSON检查此学生是否符合通过条件?
我想分别显示每个条件的通过/失败标准。上述案例示例:
我可以使用JSON路径执行此操作,但它没有像jQuery QueryBuilder这样的图形查询构建器。我遇到的主要问题是我不明白如何使用jQuery QueryBuilder生成的JSON输出来评估我的数据。是否有可能用jQuery QueryBuilder做我想做的事情?还有其他任何javascript工具吗?
答案 0 :(得分:1)
我回答这个问题有点迟了,希望你现在有一个解决方案。
最近,我还在我的一个项目中使用了jQuery QueryBuilder,并找到了以下解决方案。
var lastOperator = [];
var rulesLengths = [];
var ruleLevel = 0;
var equationString = '';
var rulesData = {
"condition": "AND",
"rules": [
{
"id": "genderSelect",
"field": "genderSelect",
"type": "string",
"input": "select",
"operator": "equal",
"value": "Male"
},
{
"id": "ageSelect",
"field": "ageSelect",
"type": "string",
"input": "select",
"operator": "equal",
"value": "18-26"
}
],
"valid": true,
};
var expr = checkDisplayLogic(rulesData);
var flag = eval(expr);
/** Recursive function to convert json data to expression **/
function checkDisplayLogic(rulesData) {
if (rulesData['rules'] !== undefined) {
ruleLevel++;
var operator = rulesData['condition'];
lastOperator.push(operator);
rulesLengths.push(rulesData.rules.length);
equationString += '(';
$.each(rulesData.rules, function (idx, elem) {
checkDisplayLogic(elem);
if (rulesLengths[rulesLengths.length - 1] == idx + 1) {
equationString += ')';;
rulesLengths.pop();
ruleLevel--;
lastOperator.pop();
}
else {
if (lastOperator.length !== 0)
equationString += ' ' + (operator === 'AND' ? '&&' : '||' ) + ' ';
}
});
}
else {
var inputType = rulesData.input;
switch (inputType) {
case 'select':
var ctrl = $('#' + rulesData.id);
//Below logic will change depending upon the operator selected in QueryBuilder.
//In my case operator is 'equal' that's why I am using '===' operator for comparison
equationString += (ctrl.val() === rulesData.value).toString();
break;
/****** Similarly create cases for other input types QueryBuilder provide ********/
}
}
return equationString;
}
快乐的编码!