Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
5 Trả lời
25260 Lượt xem

I Have website field in tree view, need to create link onclick of the wesite field...

 widget="url" is working fine in form view, but not in tree view

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi narayan

First download and install the Web/URL widget module and use widget="url"

 

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

If the email address format doesn't contains "http://", it doesnt work

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

This is maybe not perfect example but if it doesn't work you can fix it or built something what will work :)

As an example based on existing widget I've built for myself a new widget to create a link to skype so you can run skype straight from Odoo (Customer page or Human resorces :) ) just clicking on link to contact with your customers, also i made vendor support and local support page links based on this widget so it is possible and quiet easy - I'm not a programmer too ;)

So in my case I added at the end of /addons/web/static/js/view_form.js line somwhere around 6400 after "instance.web.form.widgets = new instance.web.Registry({"

this line to create a new instance in registry

" 'skype' : 'instance.web.form.FieldSkype', "

and sowhere around line 2600 before:

"instance.web.form.FieldUrl = instance.web.form.FieldChar.extend({"

this block of code:

"instance.web.form.FieldSkype = instance.web.form.FieldChar.extend({

template: 'FieldUrl',

initialize_content: function() {

this._super();

var $button = this.$el.find('button');

$button.click(this.on_button_clicked);

this.setupFocus($button);

},

render_value: function() {

if (!this.get("effective_readonly")) {

this._super();

} else {

var tmp = this.get('value');

var s = /(\w+):(.+)|^\.{0,2}\//.exec(tmp);

if (!s) {

tmp = "http://" + this.get('value');

}

var suf = document.getElementById('id');

if (suf = 'skype'){ var suf_x = 'skype:'; tmp = suf_x + this.get('value') + '?call';}

var text = this.get('value') ? this.node.attrs.text || tmp : '';

this.$el.find('a').attr('href', tmp).text(this.get('value'));

}

},

on_button_clicked: function() {

if (!this.get('value')) {

this.do_warn(_t("Resource Error"), _t("This resource is empty"));

} else {

var url = $.trim(this.get('value'));

if(/^www\./i.test(url))

url = 'skype:'+url+'?call';

window.close(url);

}

}

});"

and in XML file:

<field name="your_field_name" id="skype" widget="skype"/> - it will create a input box where you have to put skype nick.

(of course "your_field_name" needs to be defined in your_module.py)

'skype': fields.char('Skype'), - old API

skype = fields.Char('Skype') - new API

This solution works perfect for me. Same way you can create other useful widget extensions ;)

For example i created a simple connection to ebayshop, product name is automatically pulled from field "name" and added to the link making a search key on ebayshop (http://stores.ebay.co.uk/CharlesComputers/_i.html?_nkw="here is added a product name"). This solution is not the most elegant but it works :)

Example of Instance for ebayshop:

"instance.web.form.FieldEbay = instance.web.form.FieldChar.extend({

template: 'FieldUrl',

initialize_content: function() {

this._super();

var $button = this.$el.find('button');

$button.click(this.on_button_clicked);

this.setupFocus($button); },

render_value: function() {

if (!this.get("effective_readonly")) {

this._super();  } else {  var tmp = this.get('value');

var s = /(\w+):(.+)|^\.{0,2}\//.exec(tmp);

if (!s) {  tmp = "http://" + this.get('value'); }

var suf = document.getElementById('id');

if (suf = 'ebay'){ var suf_x = 'http://stores.ebay.co.uk/Your_Ebay_shop_name/_i.html?_nkw='; tmp = suf_x + this.get('value');}

var text = this.get('value') ? this.node.attrs.text || tmp : '';

this.$el.find('a').attr('href', tmp).text(this.get('value')); } },

on_button_clicked: function() { if (!this.get('value')) {

this.do_warn(_t("Resource Error"), _t("This resource is empty"));

} else {  var url = $.trim(this.get('value'));

if(/^www\./i.test(url))

url = 'http://'+url;

window.open(url);

} } });"

Check this code you may find how it works and try change it for your purpose.

Remember this is just an example.

Same Way you can create url widget for list.

In view_list.js insert this code somwhere around line 2400:

instance.web.list.Url = instance.web.list.Column.extend({

PROTOCOL_REGEX: /^(?!\w+:?\/\/)/,

  _format: function(row_data, options) {

var value = row_data[this.id].value;

if (value) {

return _.template("<a href='<%-href%>' target='_blank'><%-text%></a>", {

href: value.trim().replace(this.PROTOCOL_REGEX, '//'),

text: value  });

}

return this._super(row_data, options);

} });

This definitely work, tested it.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

I'm not really a programmer yet I have installed both this module and the WEB module with no success to show the clickable link in the tree view.

Does anyone know where there are examples of the code?

Should I be setting up the custom field or script in a certain way?

How to use widget="url" in tree view
My script is <field name="x_showworkurl2" widget="url"/>

Ảnh đại diện
Huỷ bỏ

Check example above, you may find it useful

Widget directly may not work so maybe you should try with button.

Tác giả

It works, Install "Web/URL widget" Module, it is different from "web" module

Câu trả lời hay nhất

Hi,

Check this PR https://github.com/odoo/odoo/pull/3592

 

Ảnh đại diện
Huỷ bỏ