Help

2

How to use widget="url" in tree view

Avatar
Narayanamurthy

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

Avatar
Discard
5 Answers
2
Avatar
Praveen Kumar
Best Answer

Hi narayan

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

 

Avatar
Discard
1
Avatar
Vinay
Best Answer

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

Avatar
Discard
0
Avatar
Dr Obx
Best Answer

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='' target='_blank'></a>", {

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

text: value  });

}

return this._super(row_data, options);

} });

This definitely work, tested it.

Avatar
Discard
0
Avatar
Anthony Gardiner
Best Answer

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"/>

3 Comments
Avatar
Discard
Avatar
Dr Obx
-

Check example above, you may find it useful

Avatar
Dr Obx
-

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

Avatar
Narayanamurthy
-

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

0
Avatar
Carlos Lopez
Best Answer

Hi,

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

 

Avatar
Discard