如何将外部 jQuery 插件添加到 Odoo 上的列表视图?

2024-01-09

我正在使用 Odoo 10e。我想将 jquery 插件集成到我的模块中。

我想集成 jQuery 插件jquery 可调整大小的列 https://github.com/dobtco/jquery-resizable-columns。它很简单,可以帮助用户动态调整表格列的大小,我想将其应用到特定模型的列表视图上

我应该扩展哪种方法才能添加插件?


我认为你应该在网络模块中扩展(也许包括)一些小部件。如果你去文件/addons/web/static/src/js/view_list.js,您可以看到呈现表格的小部件:

instance.web.ListView = instance.web.View.extend( /** @lends instance.web.ListView# */ {
    _template: 'ListView',
    display_name: _lt('List'),
    defaults: {
        // records can be selected one by one
        'selectable': true,
        // list rows can be deleted
        'deletable': false,
        // whether the column headers should be displayed
        'header': true,
        // display addition button, with that label
        'addable': _lt("Create"),
        // whether the list view can be sorted, note that once a view has been
        // sorted it can not be reordered anymore
        'sortable': true,
        // whether the view rows can be reordered (via vertical drag & drop)
        'reorderable': true,
        'action_buttons': true,
        //whether the editable property of the view has to be disabled
        'disable_editable_mode': false,
    },
    view_type: 'tree',
    events: {
        'click thead th.oe_sortable[data-id]': 'sort_by_column'
    },

    // [...]

    sort_by_column: function (e) {
        e.stopPropagation();
        var $column = $(e.currentTarget);
        var col_name = $column.data('id');
        var field = this.fields_view.fields[col_name];
        // test whether the field is sortable
        if (field && !field.sortable) {
            return false;
        }
        this.dataset.sort(col_name);
        if($column.hasClass("sortdown") || $column.hasClass("sortup"))  {
            $column.toggleClass("sortup sortdown");
        } else {
            $column.addClass("sortdown");
        }
        $column.siblings('.oe_sortable').removeClass("sortup sortdown");

        this.reload_content();
    },

如您所见,有一个事件声明为sort_by_column,所以你必须以类似的方式添加你想要的插件。

如果您对继承和修改小部件有任何疑问,您可以访问Odoo 文档 http://www.odoo.com/documentation/10.0/reference/javascript.html

如果您使用的是版本 10,您可以检查它是如何构建的here https://github.com/OCA/OCB/blob/10.0/addons/web/static/src/js/views/list_view.js#L37 /addons/web/static/src/js/views/list_view.js

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将外部 jQuery 插件添加到 Odoo 上的列表视图? 的相关文章

随机推荐