knockout.js - 在knockoutJS中使用按钮删除表格行

标签 knockout.js

大家。我是 KnockoutJS 的新手。
我不想做学生表。可以在表中添加或删除新学生。
这是功能

function Friend(a, b){
}

会观察学生的细节。 applyBinding 的另一个函数
function functionViewModel()

如果它将被删除,则代码工作正常,但使用此功能代码将无法在
this.deleteRow=function(){
fn.friends.remove(this);
};

如何从函数“functionViewModel”调用“fn”变量到函数“Friend”。
如果有更好的主意,请建议我。
<table border="1">
    <thead>
        <th>Full Name</th>
        <th>Address</th>
        <th>Graduate ?</th>
        <th>Subject</th>
        <th>Remove User</th>
    </thead>
    <tbody   data-bind="foreach:friends">
        <tr>
        <td data-bind="text:fullName"></td>
        <td data-bind="text:address"></td>
        <td><input type ="checkbox" data-bind="checked:graduate"></input></td>
        <td><input type ="text" data-bind="value:subjects, visible:graduate"></input></td>
        <td><input type= "button" data-bind="click:deleteRow" value="X"></input></td>
        </tr>
    </tbody>
</table>
<button data-bind="click:addUser">Add User</button>
<script src="D:\KnockoutJS\knockout-3.2.0.js"></script>
<script>

    function Friend(a, b){  
        this.fullName=a;
        this.address=b;
        this.graduate=ko.observable(false);
        this.subjects=ko.observable('');

        //Remove Row from Table
        this.deleteRow=function(){
        fn.friends.remove(this);
        };
    }

    function functionViewModel(){
        var fn={friends:ko.observableArray([new Friend("Sofia Smith", "London"), new Friend("Liam Taylor","New York")])};
        fn.addUser=function(){fn.friends.push(new Friend("Thomas Miller", "California"));};
        return fn;
        };
    ko.applyBindings(functionViewModel());
</script>

最佳答案

我认为您必须执行以下任一操作。

  • 将 removeuser 函数移动到主 View 模型并根据索引删除。如果你想这样做,那么

    http://jsfiddle.net/chLa93du/2/

  • 在 Html 中(查看)
    <table border="1">
        <thead>
            <th>Full Name</th>
            <th>Address</th>
            <th>Graduate ?</th>
            <th>Subject</th>
            <th>Remove User</th>
        </thead>
        <tbody   data-bind="foreach:friends">
            <tr>
            <td data-bind="text:fullName"></td>
            <td data-bind="text:address"></td>
            <td><input type ="checkbox" data-bind="checked:graduate"></input></td>
            <td><input type ="text" data-bind="value:subjects, visible:graduate"></input></td>
            <td><input type= "button" data-bind="click:$parent.removeUser" value="X"></input></td>
            </tr>
        </tbody>
    </table>
    <button data-bind="click:addUser">Add User</button>
    

    你的脚本:
        function Friend(a, b){  
            this.fullName=a;
            this.address=b;
            this.graduate=ko.observable(false);
            this.subjects=ko.observable('');
        }
    
        function functionViewModel(){
            var fn={friends:ko.observableArray([new Friend("Sofia Smith", "London"), new Friend("Liam Taylor","New York")])};
            fn.addUser=function(){fn.friends.push(new Friend("Thomas Miller", "California"));};
            fn.removeUser = function(item){
                  fn.friends.remove(item);
            };
            return fn;
            };
        ko.applyBindings(functionViewModel());
    
  • 您可以将主 View 模型存储在全局变量中然后访问。
    http://jsfiddle.net/chLa93du/
     var viewModel;
    
    function Friend(a, b){  
    this.fullName=a;
    this.address=b;
    this.graduate=ko.observable(false);
    this.subjects=ko.observable('');
    this.deleteRow=function(){
        viewModel.friends.remove(this);
    };
    }
    
    function functionViewModel(){
    var fn={friends:ko.observableArray([new Friend("Sofia Smith", "London"), new Friend("Liam Taylor","New York")])};
    fn.addUser=function(){fn.friends.push(new Friend("Thomas Miller", "California"));};
    return fn;
    };
    viewModel = new functionViewModel();ko.applyBindings(viewModel);
    
  • 关于knockout.js - 在knockoutJS中使用按钮删除表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684868/

    相关文章:

    javascript - 使用 JavaScript 或 jQuery 伪造滚动条

    knockout.js - knockout 虚拟滚动绑定(bind)

    javascript - 如果以编程方式更改模型,ko 不会更新绑定(bind)控件

    javascript - KnockoutJS foreach 绑定(bind)的内存泄漏

    javascript - Uncaught Error : Unable to parse bindings Knockout

    javascript - 添加/删除 Knockout observableArray 嵌套元素

    javascript - 使用 Knockout Js 进行 Ajax 数据绑定(bind)

    javascript - knockout koGrid 中的计算值

    javascript - RenderPartial 忽略周围的 Knockout foreach