从表格选择填充表格与淘汰赛js

时间:2014-01-15 22:13:34

标签: c# asp.net asp.net-mvc asp.net-mvc-4 knockout.js

我在表格中有一个值列表,其中包含使用knockout.js的编辑按钮,当单击编辑按钮时,我希望列表上方的表格填充表格中的值。我如何使用当前的淘汰视图模型执行此操作?

这是html

    <script src="~/scripts/appjs/GeneAssay.js"></script>
<script type="text/javascript">
    $(function () {
        //Dump raw data into one place 
        var data = @Html.Raw(ViewBag.Data);

        //initialize view model
        var viewModel = new ViewModel(data);
        ko.applyBindings(viewModel);
    });
</script>
<div class="row geneassayform">
<div class="col-md-4">
    <form>
        <div class="form-group">
            <label for="form_geneassaycode">Gene Assay Code:</label>
            <input type="text" class="form-control" id="form_geneassaycode" placeholder="Enter Gene Assay Code">         
        </div>
        <div class="form-group">
            <label for="form_geneassayname">Gene Assay Name:</label>
            <input type="text" class="form-control" id="form_geneassayname" placeholder="Enter Gene Assay Name">         
        </div>
        <div class="form-group">
            <label for="form_geneassaycomments">Gene Assay Comments:</label>          
            <textarea class="form-control" id="form_geneassaycomments" placeholder="Enter Gene Assay Comments(optional)"></textarea>
        </div>
        <button type="submit" class="btn btn-primary" disabled="disabled">Save</button>    
    </form>
</div>
</div>
<div class="row">
<div class="col-md-6">
    <div class="panel panel-default">
        <div class="panel-heading">
            <span>Current Gene Assays</span>
        </div>
        <div class="panel-body">
            <!-- ko if: Assays().length > 0 -->                
                <table class="table table-condensed table-hover">
                    <tr> 
                        <th>Assay Code</th>
                        <th>Assay Name</th>
                        <th>Comments</th>          
                        <th></th>
                    </tr>                
                    <tbody>
                        <!-- ko foreach: Assays --> 
                        <tr>
                            <td data-bind="text: AssayCode"></td>
                            <td data-bind="text: AssayName"></td>  
                            <td data-bind="text: Comments"></td>            
                            <td><button class="btn btn-xs btn-danger" data-bind="click:     $parent.editAssay">edit</button></td>
                        </tr>
                        <!-- /ko -->
                    </tbody>                
                </table>
            <!-- /ko -->
            <!-- ko ifnot: Assays().length > 0 -->
            <div class="alert alert-info">
                <p>There are no Gene Assays. Add an Assay using the form above.</p>
            </div>
            <!-- /ko -->
        </div>
    </div>
</div>

这是GeneAssay.js

function ViewModel(data) {
var self = this;

//Unpack raw data
self.Assays = ko.mapping.fromJS(data.assays);    

}

Form and table list

2 个答案:

答案 0 :(得分:2)

执行此操作的关键是将selectedAssay对象作为viewModel的一部分并将其绑定到编辑按钮单击:

    var assayData = '[{"AssayCode":"0001C","AssayName":"Assay A","Comments":"Here is the comment"},{"AssayCode":"0002D","AssayName":"Assay B","Comments":"Here is the comment for assay b"}]';

console.log(assayData);

//using the ko.mapping.fromJSON to make your rawData properties into observables, so the list updates with any changes
//download from http://knockoutjs.com/documentation/plugins-mapping.html
var assays = ko.mapping.fromJSON(assayData);
console.log(assays());
var viewModel ={};

var selectedAssay=ko.observable(); //initialize the selectedAssay which will be set by the edit button click, but we need to initialize it first


//just for debugging, subscribe to the selectedAssay so we can see when it's been changed
selectedAssay.subscribe(function(){
    console.log('selected assay changed');
    console.log(selectedAssay().AssayName());
});

var addNewAssay=function(){
    //create a new Assay initializing each property to a ko.observable so the data in the list updates when the data changes in the edit screen
    var newAssay ={"AssayCode":ko.observable(""),"AssayName":ko.observable(""),"Comments":ko.observable("")};
    assays.push(newAssay);
    selectedAssay(newAssay);
}

viewModel.Assays=assays;
viewModel.selectedAssay=selectedAssay;
viewModel.addNewAssay=addNewAssay;

ko.applyBindings(viewModel);

这是一个演示使用

的小提琴

http://jsfiddle.net/jiggle/2cr2f/

我已在viewModel中添加了一个“添加新分析”按钮,连接到addNewAssay函数,以便您从那里开始。

默认情况下,在启动时会创建一个新的检测(空白字段),并在编辑表单中设置(这样您就可以开始在新的检测中输入数据)。

另一种选择是使用淘汰赛“with”绑定(http://knockoutjs.com/documentation/with-binding.html)仅在有selectedAssay(新的或用户点击编辑)时显示编辑表单:

 <form data-bind="with:selectedAssay">
    <div class="form-group" >
        <div><span class="h1" data-bind="text:AssayName"></span></div>
        <label for="form_geneassaycode">Gene Assay Code:</label>
        <input type="text" class="form-control" id="form_geneassaycode" placeholder="Enter Gene Assay Code" data-bind="value:AssayCode"/>         

etc.

这是展示这种方式的另一个小提琴:

http://jsfiddle.net/jiggle/Meke2/1/

希望有助于你移动?

答案 1 :(得分:1)

好的,我就是这样做的。

// create an object
var Assay = function (AssayCode, AssayName, Comments) {
        self = this;
        self.AssayCode = AssayCode;
        self.AssayName = AssayName;
        self.Comments = Comments;
    }

    // create a mapping
    var mapping = {
        'Assays': {
            create: function(options) {
                return new Assay(options.data.AssayCode, options.data.AssayName, options.data.Comments);
            }
        }
    }

    // Create the view model
    function AViewModel()
    {
      var self = this;  
      self.Assays = ko.observableArray();
      self.editAssay = ko.observable(new Assay());

    // populate and map the Assays
    $.ajax({
       url:"/echo/json/",
       data:data,
       type:"POST",
       success:function(response)
       {
         self.Assays = ko.mapping.fromJS(response, mapping, self);
       }
    });


}

var viewModel = new AViewModel();
ko.applyBindings(viewModel);

使用editAssay和编辑表单元素绑定您的data-bind="with:editAssay",其值为data-bind="value: AssayCode"

选择修改已经正确,即data-bind="click: $parent.editAssay"

使用模拟json调用jsFiddle进行演示:

http://jsfiddle.net/hutchonoid/Tnyqp/13/