如何组成一串自定义的expression/conditions?

How to form a string of custom expression/conditions?

我有一个存储不同条件的数组

$scope.conditions = ['a>12', 'b<13', 'NOT a', 'NOT b'];

我想允许用户使用 ANDOR operators/buttons.

在给定条件下形成自己的表达式

因此用户可以形成自己的表达方式,例如

 (a>12 AND b < 13) OR NOT a 
 a>12 AND (b<13 AND NOT a)

我该怎么做?任何人都可以指出正确的方向吗?

谢谢!

eval() 函数将帮助您将字符串转换为 Javascript 表达式,您只需要根据用户选择 (and/or).

对条件进行分组

以下内容可以帮助您入门:https://jsfiddle.net/nuettt29/3/

您可以看到,使用 dragula,您可以创建类似银行的功能,允许用户拖入元素。

var choices = [' a>12 ', ' b<13 ', ' NOT a ', ' NOT b '];

var connections = [' AND ', ' OR '];

choices.forEach(function(choice) {
    var li = document.createElement('li');
    li.appendChild(document.createTextNode(choice));
    document.getElementById('bank').appendChild(li);
});

connections.forEach(function(connection) {
    var li = document.createElement('li');
    li.appendChild(document.createTextNode(connection));
    document.getElementById('bank').appendChild(li);
});

dragula([document.getElementById('bank'), document.getElementById('userInput')], {
    copy: function (el, source) {
    return source.id == 'bank'
  },
  accepts: function (el, target) {
    return target.id !== 'bank'
  }
});