248 Posts
Giuse
7 years ago
Topic

Hi, I installed the custom (JS) link to be able to build flexible links, but... any basic directions on how to use it? I can build a URL string with javascript and then.. how to make it the link for the field? The plugin page gives no instructions.

thanks

Giuse

Get a Book for SEBLOD
4229 Posts
Kadministrator
7 years ago
18
Level 1

It just adds onclick event to link/button

https://www.w3schools.com/jsref/event_onclick.asp

248 Posts
Giuse
7 years ago
17
Level 2

Ok Klas, the JS code I put in the Javascript field is executed when user clicks on the link/button but... a couple of questions:

  • I tried to put   var a = $(‘#<field_name>’).text();  and it throws a syntax error (invalid or unexpected token): can we access fields this way inside the onclick event?
  • once I have built the link URL with the code, how to apply to the field..?

Thank you very much

Giuse

4229 Posts
Kadministrator
7 years ago
16
Level 3

This is meant to run very simple code inline or to fire some function that you defined elsewhere. In your case you probabl had a problem with unescaped quotes.

248 Posts
Giuse
7 years ago
15
Level 4

Unfortunately there seem no quotes error, the field name where I am using the JS link is lpt_product_user_prod_id and the JS link code now is simply

a = $('#lpt_product_user_prod_id').val();




and I get "Uncaught TypeError: Cannot read property 'val' of null at HTMLAnchorElement.onclick".

Should retrieving fields work inside this field?

4229 Posts
Kadministrator
7 years ago
14
Level 5

in custom js. link put just someFunction(); this will fire it on click.

Then define this function elsewhere (javascript code field or javascript field that can be found if you click on small arrow at the bottom of each field's configuration

function someFunction() {

var a = $('#lpt_product_user_prod_id').val();

...

}

248 Posts
Giuse
7 years ago
13
Level 6

Hi, I tried but it seems not to work.

In JS link just

someFunction();


and in JS area of the same field (or with a Code JS field) defined the function as

function someFunction() { var a = $('#lpt_product_user_prod_id').val(); console.log(a); }

In both cases, when I click on the link a I get an error

Uncaught ReferenceError: someFunction is not defined at HTMLAnchorElement.onclick

something is wrong?

4229 Posts
Kadministrator
7 years ago
12
Level 7

With code field use Raw option, otherwise Seblod wraps your code in document ready and since function is not visible from outside.

248 Posts
Giuse
7 years ago
11
Level 8

Ok, I put JS as RAW and now there is no more undefined function but... now the fields seems not to be visible anymore by the JS:

Cannot read property 'val' of null at someFunction

and the field #lpt_product_user_prod_id does exist in the list form... maybe raw prevents retrieving the fields?

4229 Posts
Kadministrator
7 years ago
10
Level 9

Either #lpt_product_user_prod_id does not exist or it does not have a value property, val() can only be used in input, select and similar form elements

248 Posts
Giuse
7 years ago
9
Level 10

Hi Klas, the field exists but $('#<field_name>') is null for every field: is it because I am using it in a list view? I use successfully $()... to retrieve fields in Admin/FrontEnd view, maybe in List view it does not work...?

thanks

4229 Posts
Kadministrator
7 years ago
8
Level 11

only one field can have one id on the page, so on the lsit view ids are prefixed, you will have ids like this cck179_label_art_created, so you will need to use some other jQuery selector to pick your field(s).

248 Posts
Giuse
7 years ago
7
Level 12

Yes, the problem is that normal way of retrieving fields in javascript does not work in list view... Also seb_table template does not even output field ids... the text of each field is just sorrounded by <td>...</td>. Is there a way with custom attributes to add something in the output page for each field so that there no just <td></td>? I tried to add id = "foo"  as custom attributes on a field but I see nothing in the output.

Anyway there is the problem of selecting the specific field for current item in the list: imagine a list of 10 rows/items, I can add a javascript code field in the list view with code to retrieve all fields and then scan through them, but how can the field know if it is related to 1st or 2nd... elements if the list to retrieve the corresponding field in the table...? Is Seblod adding a known sequence to ids?

I don't know if I have been clear.

4229 Posts
Kadministrator
7 years ago
0
Level 13

You would need to use selector to pick each row and loop trough them with some sort of counter, then inside each row loop trough all fields.

251 Posts
Viktor Iwan
7 years ago
5
Level 13

If you like to 'scrape' dom element you'll probably like to use .find() event:

For Example:

var rowData=jQuery('table tr');

for ( var i = 1; i < rowData.length-1; i++ ) 

productItem={} ;
selectedRow=rowData[i];
productID=cartcontent_ids[i-1];
productName=jQuery(selectedRow).find("td:nth-child(2) p").text();
productPrice=getPriceNumber(jQuery(selectedRow).find("td:nth-child(3) .av-pricenew").text());
productQty=jQuery(selectedRow).find("td:nth-child(4) .select2-selection__rendered").text();  productPriceTotal=getPriceNumber(jQuery(selectedRow).find("td:nth-child(5) .av-pricenew").text());
 productItem['name']=productName;
productItem['id']=productID;
productItem['price']=productPrice;
productItem['quantity']=productQty; cartList.push(productItem); 

}


If you look at my code i'm using find("td:nth-child(x)") to select value on specific dom element....

248 Posts
Giuse
7 years ago
4
Level 14

Thanks a lot Viktor (and Klas), but my question is how a field can know its "self-position". Imagine to have a list type with 2 fields: a TEXT FIELD and a BUTTON FIELD. Now the BUTTON has a JS link that depends on the value of the TEXT field: BUTTON of first row needs to read value of TEXT field1, BUTTON of second row needs to read value of TEXT field2. The code for scanning the table is the ne that Viktor has published but how to know if we are at first or second row to pick up the right value?

Have I succeeded to better explain the question?

cheers

Giuse

4229 Posts
Kadministrator
7 years ago
3
Level 15

As I wrote in my answer, you need to implement some counter, if you look at Viktor's code, there is variable i that tells you in which row you are. 

If you don't know how to do this yourself you can use Seblod service

248 Posts
Giuse
7 years ago
2
Level 16

Hi, it's surely my fault in explaining. I know how to scan a table and get the nth element in the table. My point was that the JS code needs to know which N to search for every time it is invoked.

I try with an example: we have a list view that outputs a table with N rows, each row has a text field and a button field which uses the JS custom link plugin:

  1. Cat - Button1
  2. Dog - Button2
  3. ....

Now, the JS code for Button field is able to retrieve the Nth element (Cat or Dog) but it needs to know that first button must search for N=1 and second button must search for N=2. So the question is: how the JS code for the Button field can know of being used for Button1 or for Button2? Is there any Seblod function to know that or must I define somehow a global counter that is incremented every time the field Button is used in the page?

Hope to have been clear this time :)

4229 Posts
Kadministrator
7 years ago
1
Level 17

First of all - use click event, not inline click javascript. But in any case you can solve this using jQuery - when button is clicked $(this) will give you button element. Then you can chain .parent to ti and you will be in the row, then you can easily find element in the row you need to change..eg.


$( ".dogbottonclass" ).click(function() { 
$( this ).parent().find('.targetelementclass').val('dog'); 
}); 

$( ".catbottonclass" ).click(function() { 
$( this ).parent().find('.targetelementclass').val('cat'); 
});

248 Posts
Giuse
7 years ago
0
Level 18

Thanks Klas!

Marked this thread as solved.

251 Posts
Viktor Iwan
7 years ago
0
Level 1

can you try to change:

$('#lpt_product_user_prod_id').val()

to

jQuery('#lpt_product_user_prod_id').val()

it sometimes solve a lot of js problem in Joomla

Get a Book for SEBLOD