码迷,mamicode.com
首页 > 其他好文 > 详细

easyui.treegrid与ko的绑定

时间:2015-05-31 06:43:04      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

一、自定义ko绑定属性

ko.bindingHandlers.etreegrid = {
    editing: false,
    editIndex: 0,
    init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
        var self = ko.bindingHandlers.etreegrid;
        var opts = valueAccessor();
        $(element).treegrid({
            url: opts.url,
            treeField: opts.treeField,
            idField: opts.idField,
            onDblClickRow: function (node) {
                if (viewModel.add) editRow(node);
            },
            onClickCell: function () {
                if (viewModel.add) {
                    saveRow();
                }
            }
        });
        viewModel.add = function () {
            addRow(getSelectedRow());
        };
        viewModel.edit = function () {
            editRow(getSelectedRow());
        };
        viewModel.remove = function () {
            removeRow(getSelectedRow());
        };
        viewModel.cancel = function () {
            var node = getSelectedRow();
            if (viewModel.onCancel) {
                viewModel.onCancel(node);
            }
            $(element).treegrid("unselectAll");
            try {
                if (self.editing) {
                    $(element).treegrid("cancelEdit", self.editIndex);
                    $(element).treegrid("refresh", self.editIndex);
                    self.editing = false;
                }
            } catch (ex) {
                self.editing = false;
                self.editIndex = 0;
            }
        };

        function getSelectedRow() {
            return $(element).treegrid("getSelected");
        }
        function saveRow() {
            var rowIndex = self.editIndex;
            if (rowIndex == 0) {
                return;
            }
            self.editIndex = 0;
            self.editing = false;
            $(element).treegrid("endEdit", rowIndex);
            $.ajax({
                async: false,
                url: opts.saveUrl,
                method: "POST",
                data: $(element).treegrid("find", rowIndex),
                success: function (data) {
                    $(element).treegrid("refresh", rowIndex);
                    if (viewModel.onSaved) {
                        viewModel.onSaved(data, rowIndex);
                    }
                },
                error: function () {}
            });
        }
        function editRow(node) {
            if (!node || node[opts.idField] == self.editIndex) return;
            saveRow();
            self.editing = true;
            self.editIndex = node[opts.idField];
            $(element).treegrid(‘select‘, self.editIndex);
            $(element).treegrid("beginEdit", self.editIndex);
            if (viewModel.onEditing) {
                viewModel.onEditing(node);
            }
        }
        function addRow(parentNode) {
            if (self.editing) return;
            var parentId = parentNode ? parentNode[opts.idField] : 0;
            var newNode = viewModel.newNode(parentId);
            $(element).treegrid("append", {
                parent: parentId,
                data: [newNode]
            });
            editRow(newNode);
        }
        function removeRow(node) {
            if (!node) return;
            if (viewModel.onRemoving && !viewModel.onRemoving(node)) {
                return;
            }
            $.messager.confirm(‘确认‘, ‘你确定要删除吗?‘, function (r) {
                if (!r) return;
                $.post(opts.deleteUrl + "?id=" + node[opts.idField], function (data, statusText) {
                    if (viewModel.onRemoved) {
                        viewModel.onRemoved(data);
                    } else {
                        if (statusText == "success") {
                            $(element).treegrid("remove", node[opts.idField]);
                        } else {
                            $.messager.alert(‘警告‘, "删除失败", ‘warning‘);
                        }
                    }
                });
            });
        }
    }
};

二、页面内容

<div id="area-form">
    <div id="area-tool-bar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" data-bind="click: add">新增</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" data-bind="click: edit">编辑</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" data-bind="click: remove">删除</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" plain="true" data-bind="click: cancel">取消</a>
    </div>
    <table id="area-list" rownumbers="true" title="区域列表" border="0" toolbar="#area-tool-bar" data-bind="etreegrid: tree">
        <thead>
            <tr>
                <th field="Name" width="500" data-options="editor:{type:‘validatebox‘}">地区名称</th>
                <th field="Code" width="120" data-options="editor:{type:‘validatebox‘}">地区编码</th>
            </tr>
        </thead>
    </table>
</div>

三、viewModel

(function () {
    var newId = 9999999;
    var listId = "#area-list";

    function AreaModel() {
        this.tree = {
            url: "/area/gettree",
            treeField: "Name",
            idField: "ID",
            saveUrl: "/area/save",
            deleteUrl: "/area/delete",
        };
        this.newNode = function (parentId) {
            return {
                ID: newId,
                Name: ‘‘,
                Code: ‘‘,
                ParentID: parentId
            };
        };
        this.add = ko.observable();
        this.edit = ko.observable();
        this.remove = ko.observable();
        this.cancel = ko.observable();
        this.validate = function (node) {
            return !!node.Name;
        };
        this.onCancel = function (node) {
            if (node && node.ID == newId) {
                $(listId).treegrid("remove", node.ID);
            }
        };
        this.onSaved = function (data, editIndex) {
            if (data.result) {
                var row = data.data;
                if (editIndex == newId) {
                    $(listId).treegrid("remove", editIndex);
                    $(listId).treegrid("append", {
                        parent: row.ParentID,
                        data: [data.data]
                    });
                }
            }
        };
        this.onRemoving = function (node) {
            if (node.ID == newId) {
                $(listId).treegrid("remove", node.ID);
                return false;
            } else {
                if (!node.children || node.children.length > 0) {
                    $.messager.alert(‘删除失败‘, "该地区有下属分类,不可直接删除!", ‘warning‘);
                    return false;
                }
            }
            return true;
        };
    };
    ko.applyBindings(new AreaModel(), document.getElementById("area-form"));
})();

这个例子在网上比较常见了,不过确实比较经典。

easyui.treegrid与ko的绑定

标签:

原文地址:http://www.cnblogs.com/hispring/p/4541394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!