使用Knockout.js将文本框数据临时添加到网格

时间:2018-09-25 07:18:59

标签: asp.net-mvc knockout.js

HTML代码

<label class="control-label">Contact Person</label>
<input class="form-control" type="text" placeholder="Enter Contact name" data-bind="value: ContactPerson" data-validate='{"required":"true"}'><br />

<label class="control-label">ContactNo</label>
<input class="form-control" type="tel" data-bind="value: ContactNo" placeholder="ContactNo" data-validate='{"required":"true"}'><br />

<label class="control-label">E-Mail</label>
<input class="form-control" type="email" data-bind="value: Email" placeholder="Email" data-validate='{"required":"true","email":"true"}'><br />

<table class="table table-hover table-bordered" id="listTable">
     <thead>
          <tr>
               <th>ContactPerson</th>
               <th>ContactNo</th>
               <th>Email</th>
               <th></th>
          </tr>
     </thead>
     <tbody data-bind="template:{name: 'Process-list', 
                                foreach: rootViewModel.BodyContent.ProcessList }">
     </tbody>
</table>

当我单击添加按钮时,三个文本框中的数据应绑定到grid, 当我单击网格中行的删除按钮时,它应该会为此消失,而我需要使用viewmodel。

谢谢

1 个答案:

答案 0 :(得分:0)

这是一个满足要求的快速查看模型。我选择ContactNo作为主键,因为它们应该是唯一的。您可以根据需要使用ID。在调用add方法之前运行验证也是一个好主意。

var viewModel = function(){
  var self = this;
  self.ContactPerson = ko.observable();
  self.ContactNo = ko.observable();
  self.Email = ko.observable();
  self.ProcessList = ko.observableArray();
  self.add = function(){
    self.ProcessList.push({
      ContactPerson: self.ContactPerson(),
      ContactNo: self.ContactNo(),
      Email: self.Email(),
    });
    self.ContactPerson('');
    self.ContactNo('');
    self.Email('');
  };
  self.delete = function(data, event){
    self.ProcessList.remove(function(listObject){
      return listObject.ContactNo === data.ContactNo;
    });
  };
};

ko.applyBindings(new viewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<label class="control-label">Contact Person</label>
<input class="form-control" type="text" placeholder="Enter Contact name" data-bind="value: ContactPerson" data-validate='{"required":"true"}'><br />

<label class="control-label">ContactNo</label>
<input class="form-control" type="tel" data-bind="value: ContactNo" placeholder="ContactNo" data-validate='{"required":"true"}'><br />

<label class="control-label">E-Mail</label>
<input class="form-control" type="email" data-bind="value: Email" placeholder="Email" data-validate='{"required":"true","email":"true"}'><br />


<button data-bind="click: add">Add Data</button>
<table class="table table-hover table-bordered" id="listTable">
     <thead>
          <tr>
               <th>ContactPerson</th>
               <th>ContactNo</th>
               <th>Email</th>
               <th></th>
          </tr>
     </thead>
     <tbody data-bind="template:{name: 'Process-list', 
                                foreach: ProcessList }">
          </tbody>
     </tbody>
</table>

<script type="text/html" id="Process-list">
  <tr>  
    <td data-bind="text: ContactPerson"></td>
    <td data-bind="text: ContactNo"></td>
    <td data-bind="text: Email"></td>
    <td><button data-bind="click: $root.delete">Delete</button></td>
  </tr>
</script>