【问题标题】:knockout js, add additional elements to an array淘汰js,向数组添加其他元素
【发布时间】:2017-10-05 03:45:16
【问题描述】:

我有一个应用程序,我想向数组中的项目添加额外的元素,我有 created a fiddle 我想要实现的目标。

所以用户启动应用程序,他们单击下一步填写项目的一些详细信息和描述,然后他们应该单击下一步并填写项目的价格(这是我卡住的地方)。这个例子在一定程度上起作用,就像每当我尝试实现价格部分时,我似乎都弄错了。一旦我单击下一步,您就会看到项目被添加到数组中,但是如何向数组中的当前项目添加更多元素。如果我没有理解逻辑应该是:

开始 第一页(输入名称和描述) 第二页(输入价格)

我知道我可以在一个页面上完成所有这些操作,但我不想让淘汰赛能够在名称和描述页面之后添加价格。我还希望能够返回并修改该项目。有人能帮忙看看我接下来应该做什么吗?

当我插入名称和描述时,我是否还必须在数组中为价格创建一个空元素?然后,一旦输入价格,以某种方式将其插入到数组中的价格元素中?不确定如何/是否应该实施。

这是我的第一个淘汰赛应用程序,所以如果我实际所做的事情是正确的,我会很惊讶地收到任何帮助或指点 :)

我的代码如下:

$(function() {
	var main = new stuff();
	ko.applyBindings(main, $('#ko')[0]);
});

function Item(data) {
	this.itemname = ko.observable(data.itemname);
	this.itemdescription = ko.observable(data.itemdescription);
	this.itemenabled = ko.observable(data.itemenabled);
}

function stuff() {
	var self = this;

	self.items = ko.observableArray([]);
	self.newItemName = ko.observable();
	self.newItemDescription = ko.observable();
	self.newItemEnabled = ko.observable();

	// Operations
	self.addItem = function() {
		self.items.push(new Item({
			itemname: this.newItemName(),
			itemdescription: this.newItemDescription(),
			itemenabled: this.newItemEnabled()
		}));
	};
};

//jquery stuff
$('#start').on('click', function(e) {
	$("#page1").show(500);
	$('#panel1').css('visibility', 'visible').hide().fadeIn().removeClass('hidden');
	$("#start").hide(500);
})

$('#back').on('click', function(e) {
	$("#panel1").hide(500);			 
	$("#page1").hide(500);			 
	$("#start").show(500);
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<button type="submit" class="btn btn-primary" id="start">Start</button>

<div id="ko">
	<div class="panel panel-default hidden" id="panel1">
		<div id="page1">
			<form data-bind="submit: addItem">
				<div class="panel panel-default" style="padding: 15px 0px 0px 0px;" id="panel2">
					<div class="container">
						<div class="form-group row">
							<label for="inputItemName" class="col-lg-3 col-form-label">Enter Item Name</label>
							<div class="col-lg-8">
								<input type="text" class="form-control form-control-lg" id="inputItemName" data-bind="value: newItemName" placeholder="">
							</div>
						</div>
						<div class="form-group row">
							<label for="textareaItemDescription" class="col-lg-3 col-form-label">Enter Item Description</label>
							<div class="col-lg-8">
								<textarea class="form-control" id="textareaItemDescription" rows="3" data-bind="value: newItemDescription"></textarea>
							</div>
						</div>
						<div class="form-group row">
							<div class="col-sm-offset-3 col-sm-9">
								<label class="custom-control custom-checkbox checkbox-inline no_indent" style="font-weight:bold;">
									<input type="checkbox" class="custom-control-input" checked="checked" data-bind="checked: newItemEnabled">
									<span class="custom-control-indicator"></span>
									<span class="custom-control-description" style="padding-bottom: 2px;">Enabled</span>
								</label>
							</div>
						</div>
					</div>
				</div>
				<button type="button" class="btn btn-primary" id="back">Back</button>
				<div class="pull-right">
					<button type="submit" class="btn btn-primary" id="next1">Next</button>
				</div>
			</form>
		</div>
	</div>

	<ul data-bind="foreach: items, visible: items().length > 0">
		<li>
			<input data-bind="value: itemname" style="color: black;" />
		</li>
		<li>
			<input data-bind="value: itemdescription" style="color: black;" />
		</li>
		<li>
			<input type="checkbox" data-bind="checked: itemenabled" />
		</li>
	</ul>
</div>

【问题讨论】:

  • 我注意到您正在使用 jquery 进行点击事件。您应该使用 KO 绑定。它们更好,因为从 HTML 中可以清楚地看到发生了什么
  • “如何向数组中的当前项添加更多元素”是什么意思。目前尚不清楚您的问题是什么
  • 您是说希望能够编辑当前值吗?
  • 还有一些变量名是驼峰式的,有些不是。尽量保持一致

标签: javascript jquery arrays knockout.js


【解决方案1】:

好的,所以我添加了一个新的 observable 来跟踪页码以及增加/减少数字的基本功能,然后我只需在添加价格后添加新项目。

$(function() {
  var main = new stuff();
  ko.applyBindings(main, $('#ko')[0]);
});

function Item(data) {
  this.itemname = ko.observable(data.itemname);
  this.itemdescription = ko.observable(data.itemdescription);
  this.itemenabled = ko.observable(data.itemenabled);
  this.price = ko.observable(data.itemprice);
}

function stuff() {
  var self = this;

  self.items = ko.observableArray([]);
  self.newItemName = ko.observable();
  self.newItemDescription = ko.observable();
  self.newItemEnabled = ko.observable();
  self.newItemPrice = ko.observable();
  self.page = ko.observable(1);
  // Operations
  self.next = function() {
    if (self.page() === 2) {
      self.addItem();
    }
    self.page(self.page() + 1)
  }

  self.back = function() {
    if (self.page() == 1) {
      $("#panel1").hide(500);
      $("#page1").hide(500);
      $("#start").show(500);
    }
    self.page(self.page() - 1);
  }

  self.addItem = function() {
    self.items.push(new Item({
      itemname: this.newItemName(),
      itemdescription: this.newItemDescription(),
      itemenabled: this.newItemEnabled(),
      itemprice: this.newItemPrice(),
    }));
  };
};

//jquery stuff
$('#start').on('click', function(e) {
  $("#page1").show(500);
  $('#panel1').css('visibility', 'visible').hide().fadeIn().removeClass('hidden');
  $("#start").hide(500);
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<button type="submit" class="btn btn-primary" id="start">Start</button>

<div id="ko">
  <div class="panel panel-default hidden" id="panel1">
    <div id="page1">
      <form data-bind="submit: next">
        <div class="panel panel-default" style="padding: 15px 0px 0px 0px;" id="panel2">
          <div class="container" data-bind="if: page() == 1">
            <div class="form-group row">
              <label for="inputItemName" class="col-lg-3 col-form-label">Enter Item Name</label>
              <div class="col-lg-8">
                <input type="text" class="form-control form-control-lg" id="inputItemName" data-bind="value: newItemName" placeholder="">
              </div>
            </div>
            <div class="form-group row">
              <label for="textareaItemDescription" class="col-lg-3 col-form-label">Enter Item Description</label>
              <div class="col-lg-8">
                <textarea class="form-control" id="textareaItemDescription" rows="3" data-bind="value: newItemDescription"></textarea>
              </div>
            </div>
            <div class="form-group row">
              <div class="col-sm-offset-3 col-sm-9">
                <label class="custom-control custom-checkbox checkbox-inline no_indent" style="font-weight:bold;">
									<input type="checkbox" class="custom-control-input" checked="checked" data-bind="checked: newItemEnabled">
									<span class="custom-control-indicator"></span>
									<span class="custom-control-description" style="padding-bottom: 2px;">Enabled</span>
								</label>
              </div>
            </div>
          </div>
        </div>
        <div class="container" data-bind="if: page() == 2">
          <div class="form-group row">
            <label for="inputPrice" class="col-lg-3 col-form-label">Enter Price</label>
            <div class="col-lg-8">
              <input type="text" class="form-control form-control-lg" id="inputPrice" data-bind="value: newItemPrice" placeholder="">
            </div>
          </div>
        </div>
        <button type="button" data-bind="click: back" class="btn btn-primary" id="back">Back</button>
        <div class="pull-right">
          <button type="submit" class="btn btn-primary" id="next1">Next</button>
        </div>
      </form>
    </div>
  </div>

  <ul data-bind="foreach: items, visible: items().length > 0">
    <li>
      <input data-bind="value: itemname" style="color: black;" />
    </li>
    <li>
      <input data-bind="value: itemdescription" style="color: black;" />
    </li>
    <li>
      <label for="price">Price</label>
      <input id="price" data-bind="value: price" style="color: black;" />
    </li>
    <li>
      <input type="checkbox" data-bind="checked: itemenabled" />
    </li>
  </ul>
</div>

【讨论】:

  • 这几乎是我所追求的,但是说用户在创建项目过程结束时决定他们想要返回并更改项目名称他们将如何做到这一点?如果他们单击上一个按钮,它不会加载数组中的项目?
  • 您可以在第一阶段创建新项目并将价格设置为0
  • 您好,我又来了,似乎在绕圈子。但是,让我们说在我什至使用“创建项目”应用程序之前,我想将一些项目添加到我从数据库中获得的数组中,我该怎么做?我已经从数据库中收集了很好的项目,只是我似乎不能只是去 .addtem 并将其添加到我的数组 ifswim
  • 我不明白为什么这样做会有任何问题。创建一个新问题,我会帮你看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 2017-10-10
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多