Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   Проблема с изучением knockout js (https://javascript.ru/forum/dom-window/46155-problema-s-izucheniem-knockout-js.html)

maxandsoft 30.03.2014 22:42

Проблема с изучением knockout js
 
Пытаюсь использовать фильтр всю голову сломал, подскажите что не так, в чем моя ошибка (фильтра закоментированы)

пробовал и в биндах и как функцию ViewModel

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Документ без названия</title>
<!--script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script-->
<script type="text/javascript" src="http://rniemeyer.github.io/KnockMeOut/Scripts/jquery.tmpl.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<style>
.group{
    background-color: #fff;
    padding: 10px;
    border-radius: 5px;
    border: solid 1px #666;
}

.group-conditions{
    margin-left: 20px;
}

.condition, .group{
    margin-top: 10px;
    margin-bottom: 1px;
}
.container {
max-width: 100%;
}
.textbox1 {
	border: 1px solid #c4c4c4;
	height: 25px;
	font-size: 13px;
	padding: 1px 1px 1px 1px;
	border-radius: 4px;
	-moz-border-radius: 4px;
	-webkit-border-radius: 4px;
	box-shadow: 0px 0px 8px #d9d9d9;
	-moz-box-shadow: 0px 0px 8px #d9d9d9;
	-webkit-box-shadow: 0px 0px 8px #d9d9d9;
	color: black;
}
.textbox1:focus {
	outline: none;
	border: 1px solid #7bc1f7;
	box-shadow: 0px 0px 8px #7bc1f7;
	-moz-box-shadow: 0px 0px 8px #7bc1f7;
	-webkit-box-shadow: 0px 0px 8px #7bc1f7;
}
.alert-warning1 {
	color: #c09853;
}
.alert1 {
	padding: 15px;
	margin-bottom: 20px;
	border: 1px solid transparent;
	border-radius: 4px;
}
.alert-group1 {
	margin-top: 10px;
	margin-bottom: 2px;
	border-color: rgb(220, 200, 150);
}
</style>

</head>
<body>
<!-- HTML контейнер -->
<div class="container">
  <!--div class="alert alert-info"> <span data-bind="text: text"></span> </div-->
  <div data-bind="with: group">
    <div data-bind="template: templateName"></div>
  </div>
</div>

<!-- HTML шаблон для условий --> 
<script id="condition-template" type="text/html">
    <div class="condition">
      <select class="textbox1" data-bind="options: entarea, optionsText : &apos;displayName&apos;, optionsValue: &apos;sqlAreaId&apos;"></select>
      <!--select class="textbox1" data-bind="options: fields, optionsText : &apos;fieldDisplayName&apos;, optionsValue: &apos;fieldId&apos;/*, template: { name: 'condition-template', foreach: ko.utils.arrayFilter(fields(), function(p){ return p.sqlAreaId() == 1;}
)}*/"></select-->
      <select class="textbox1" data-bind="options: fields, optionsText : &apos;fieldDisplayName&apos;, optionsValue: &apos;fieldId&apos;"></select>

      <select class="textbox1" data-bind="options: comparisons, optionsText : &apos;comparsionDisplayName&apos;, optionsValue: &apos;fieldType&apos;"></select>
      <input class="textbox1" type="text" data-bind="value: value"></input>
      <button class="btn btn-danger btn-xs" data-bind="click: $parent.removeChild"><span class="glyphicon glyphicon-minus-sign"></span></button>
    </div>
  </script> 

<!-- HTML шаблон для групп --> 
<script id="group-template" type="text/html">
    <div class="alert1 alert-warning1 alert-group1">
      <select class="textbox1" data-bind="options: logoper, optionsText : &apos;logicalOperatorDisplayName&apos;, Value: &apos;logicalOperatorDisplayName&apos;"></select>
      <button class="btn btn-xs btn-success" data-bind="click: addCondition"><span class="glyphicon glyphicon-plus-sign"></span> Добавить условие</button>
      <button class="btn btn-xs btn-success" data-bind="click: addGroup"><span class="glyphicon glyphicon-plus-sign"></span> Добавить группу</button>
      <button class="btn btn-xs btn-danger" data-bind="click: $parent.removeChild"><span class="glyphicon glyphicon-minus-sign"></span> Удалить группу</button>
      <div class="group-conditions">
        <div data-bind="foreach: children">
          <div data-bind="template: templateName"></div>
        </div>
      </div>
    </div>
  </script> 
<script>

window.QueryBuilder = (function (exports, ko) {

    function Condition() {
        var self = this;

        var sqlArea = function (sqlAreaId, displayName) {
            this.displayName = displayName;
            this.sqlAreaId = sqlAreaId;
        };

        var field = function (fieldId, fieldDisplayName, sqlAreaId, fieldType) {
            this.fieldId = fieldId;
			this.fieldDisplayName = fieldDisplayName;
            this.sqlAreaId = sqlAreaId;
            this.fieldType = fieldType;
        };

        var comparison = function ( comparsionId, comparsionDisplayName, fieldType) {
			this.comparsionId = comparsionId;
			this.comparsionDisplayName = comparsionDisplayName;
			this.fieldType = fieldType;
		};

        self.templateName = "condition-template";


        self.entarea = ko.observableArray([new sqlArea(1, "Клиенты"), 
                                           new sqlArea(2, "Заказы")]);

        self.selectedArea = ko.observable();

        self.fields = ko.observableArray([new field( 1, "Наименование клиента", 1, 1), 
           new field( 2, "Адрес клиента", 1, 1), 
           new field( 3, "Телефон клиента", 1, 1), 
           new field( 4, "Руководитель", 1, 1), 
           new field( 5, "Тип клиента", 1, 2), 
           new field( 6, "Номер заказа", 2, 2), 
           new field( 7, "Сумма заказа", 2, 2), 
           new field( 8, "Дата заказа", 2, 3)]); 
										  
        self.selectedField = ko.observable(self.fields());										  

        self.comparisons = ko.observableArray([new comparison( 1,"больше или равно", 2), 
           new comparison( 2,"больше чем", 2), 
           new comparison( 3,"до", 3), 
           new comparison( 4,"любая дата из", 3), 
           new comparison( 5,"любая строка из", 1), 
	   new comparison( 6,"любое кроме", 2),
           new comparison( 7,"любое число из", 2), 
	   new comparison( 8,"меньше", 2), 
           new comparison( 9,"меньше или равно", 2), 
	   new comparison(10,"начиная с", 3), 
           new comparison(11,"не равно", 2), 
	   new comparison(12,"по", 3), 
	   new comparison(13,"после", 3), 
           new comparison(14,"равно", 2), 
	   new comparison(15,"содержит выражение", 1), 
           new comparison(16,"точное совпадение", 1)]);

        self.selectedComparison = ko.observable(self.comparisons());

        self.value = ko.observable(0);

        self.text = ko.computed(function () {
            return self.selectedField() +
                " " +
                self.selectedComparison() +
                " " +
                self.value();
        });
    }

    exports.Condition = Condition;
    return exports;

})(window.QueryBuilder || {}, window.ko);



window.QueryBuilder = (function (exports, ko) {

    var Condition = exports.Condition;

    function Group() {
        var self = this;

        var logicalOperator = function (logicalOperatorId, logicalOperatorDisplayName) {
            this.logicalOperatorDisplayName = logicalOperatorDisplayName;
            this.logicalOperatorId = logicalOperatorId;
        };


        self.templateName = "group-template";

        self.children = ko.observableArray();


        self.logoper = ko.observableArray([new logicalOperator(1, "И"), new logicalOperator(2, "ИЛИ")]);

        self.logicalOperatorId = ko.observable(2);

        self.children.push(new Condition());

        self.addCondition = function () {
            self.children.push(new Condition());
        };

        self.addGroup = function () {
            self.children.push(new Group());
        };

        self.removeChild = function (child) {
            self.children.remove(child);
        };

        self.text = ko.computed(function () {
            var result = "(";
            var op = "";
            for (var i = 0; i < self.children()
                .length; i++) {
                var child = self.children()[i];
                result += op + child.text();
                op = " " + self.logoper.status + " ";
            }
            return result += ")";
        });
    }

    exports.Group = Group;
    return exports;

})(window.QueryBuilder || {}, window.ko);

window.QueryBuilder = (function (exports, ko) {

    var Group = exports.Group;

    var self = this;

    function ViewModel() {
        var self = this;
        self.group = ko.observable(new Group());

        // the text() function is just an example to show output
        self.text = ko.computed(function () {
            return self.group()
                .text();
        });
    }
                  /*  ViewModel.fields = ko.dependentObservable(function() {
                        var field = this.selectedArea(), sqlAreaId;
                        if (sqlAreaId) {
                            countryId = country.countryId();
                            return ko.utils.arrayFilter(this.states(), function(state) {
                                return state.countryId() === countryId;
                            });
                        }

                        return [];
                    }, ViewModel);*/

    exports.ViewModel = ViewModel;
	
    return exports;

})(window.QueryBuilder || {}, window.ko);

</script> 
<script> window.addEventListener("load", function (){ko.applyBindings(new QueryBuilder.ViewModel());}, true);</script>
</body>
</html>


Часовой пояс GMT +3, время: 09:22.