diff --git a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/template/querybuilder.controller.js b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/template/querybuilder.controller.js index 65c2744df2..7fd15bd715 100644 --- a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/template/querybuilder.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/template/querybuilder.controller.js @@ -53,12 +53,16 @@ angular.module("umbraco").controller('Umbraco.Dialogs.Template.QueryBuilderContr }); }; - $scope.$watch("query", function(value) { - + var throttledFunc = _.throttle(function() { + $http.post("backoffice/UmbracoApi/TemplateQuery/PostTemplateQuery", $scope.query).then(function (response) { $scope.result = response.data; }); + }, 200); + + $scope.$watch("query", function(value) { + throttledFunc(); }, true); $scope.getPropertyOperators = function (property) { @@ -71,18 +75,12 @@ angular.module("umbraco").controller('Umbraco.Dialogs.Template.QueryBuilderContr }; - $scope.addFilter = function(query){ + $scope.addFilter = function(query){ + query.filters.push({}); + }; - var f = { - property:{ - alias: "meh", - name: "Meh" - }, - operator: "IS", - value: "nothing" - }; - - query.filters.push(f); + $scope.trashFilter = function (query) { + query.filters.splice(query,1); }; $scope.changeSortOrder = function(query){ diff --git a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/template/querybuilder.html b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/template/querybuilder.html index 7e279eaeca..1c0aaf7209 100644 --- a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/template/querybuilder.html +++ b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/template/querybuilder.html @@ -50,46 +50,59 @@ -
- Where +
+ + Where + And + + - - -
- -
- - {{filter.term.name}} - - - -
- - -
-
+
Order by diff --git a/src/Umbraco.Web/Editors/TemplateQuery/TemplateQueryController.cs b/src/Umbraco.Web/Editors/TemplateQuery/TemplateQueryController.cs index c798fb411b..eb36101f4b 100644 --- a/src/Umbraco.Web/Editors/TemplateQuery/TemplateQueryController.cs +++ b/src/Umbraco.Web/Editors/TemplateQuery/TemplateQueryController.cs @@ -142,72 +142,78 @@ namespace Umbraco.Web.Editors // WHERE var token = 0; - foreach (var condition in model.Filters) + + if (model != null) { - if(string.IsNullOrEmpty( condition.ConstraintValue)) continue; + model.Filters = model.Filters.Where(x => x.ConstraintValue != null); + + foreach (var condition in model.Filters) + { + if(string.IsNullOrEmpty( condition.ConstraintValue)) continue; - var operation = condition.BuildCondition(token); + var operation = condition.BuildCondition(token); - clause = string.IsNullOrEmpty(clause) ? operation : string.Concat(new[] { clause, " && ", operation }); + clause = string.IsNullOrEmpty(clause) ? operation : string.Concat(new[] { clause, " && ", operation }); - token++; - } + token++; + } - if (string.IsNullOrEmpty(clause) == false) - { + if (string.IsNullOrEmpty(clause) == false) + { - timer.Start(); + timer.Start(); - //clause = "Visible && " + clause; + //clause = "Visible && " + clause; - contents = contents.AsQueryable().Where(clause, model.Filters.Select(GetConstraintValue).ToArray()); - // contents = contents.Where(clause, values.ToArray()); - contents = contents.Where(x => x.IsVisible()); + contents = contents.AsQueryable().Where(clause, model.Filters.Select(this.GetConstraintValue).ToArray()); + // contents = contents.Where(clause, values.ToArray()); + contents = contents.Where(x => x.IsVisible()); - timer.Stop(); + timer.Stop(); - clause = string.Format("\"Visible && {0}\",{1}", clause, - string.Join(",", model.Filters.Select(x => x.Property.Type == "string" ? - string.Format("\"{0}\"", x.ConstraintValue) : x.ConstraintValue).ToArray())); + clause = string.Format("\"Visible && {0}\",{1}", clause, + string.Join(",", model.Filters.Select(x => x.Property.Type == "string" ? + string.Format("\"{0}\"", x.ConstraintValue) : x.ConstraintValue).ToArray())); - sb.AppendFormat(".Where({0})", clause); - } - else - { - timer.Start(); + sb.AppendFormat(".Where({0})", clause); + } + else + { + timer.Start(); - contents = contents.Where(x => x.IsVisible()); + contents = contents.Where(x => x.IsVisible()); - timer.Stop(); + timer.Stop(); - sb.Append(".Where(\"Visible\")"); + sb.Append(".Where(\"Visible\")"); - } + } - if (model.Sort != null && string.IsNullOrEmpty(model.Sort.Property.Alias) == false) - { - timer.Start(); + if (model.Sort != null && string.IsNullOrEmpty(model.Sort.Property.Alias) == false) + { + timer.Start(); - contents = SortByDefaultPropertyValue(contents, model.Sort); + contents = this.SortByDefaultPropertyValue(contents, model.Sort); - timer.Stop(); + timer.Stop(); - var direction = model.Sort.Direction == "ascending" ? string.Empty : " desc"; + var direction = model.Sort.Direction == "ascending" ? string.Empty : " desc"; - sb.AppendFormat(".OrderBy(\"{0}{1}\")", model.Sort.Property.Name, direction); - } + sb.AppendFormat(".OrderBy(\"{0}{1}\")", model.Sort.Property.Name, direction); + } - if (model.Take > 0) - { - timer.Start(); + if (model.Take > 0) + { + timer.Start(); - contents = contents.Take(model.Take); + contents = contents.Take(model.Take); - timer.Stop(); + timer.Stop(); - sb.AppendFormat(".Take({0})", model.Take); + sb.AppendFormat(".Take({0})", model.Take); + } } queryResult.QueryExpression = sb.ToString();