Simple question - javascript

sob_k

Member
Hi everyone,
I have this code in 3.js file in components/com_fabrik/js
JavaScript:
requirejs(['fab/fabrik'], function () {
  var formId = 3;
  Fabrik.getBlock('form_' + formId, false, function (block) {
   var field = block.elements.get('sprawa_17_repeat___notatki_0').get('value');  
   if (field==''){
 document.getElementById('group17_tab').style.display = 'none';
}
});
});

and this work fine in form but I want do that in details view. So when I change to:

JavaScript:
requirejs(['fab/fabrik'], function () {
  var formId = 3;
  Fabrik.getBlock('details_' + formId, false, function (block) {
   var field = block.elements.get('sprawa_17_repeat___notatki_ro_0').get('value'); 
   if (field==''){
document.getElementById('group17_tab').style.display = 'none';
}
});
});
It doesn't work, in debug I have 'field' is "null". What am I doing wrong?
What I want to do is hide the group17_tab in display view when the value of 'sprawa_17_repeat___notatki' in this tab is empty
Cheers, Chris
 
What I have done is create a function getForm as:
Code:
function getForm()
{
    var form = Fabrik.getBlock("form_8");
    if (form === false) {
        form = Fabrik.getBlock("details_8");
    }
    return form;
}

The rest would be something like:
Code:
var form = getForm();
var field = form.formElements.get('sprawa_17_repeat___notatki_ro_0').get('value');
if (field = "" ) {
    form.formElements.get('group17_tab').hide();
}

I haven't tested this directly but it should be close. Change the form/details id to match your requirement or pass it into the getForm function.
 
Thanks for replay
So I try this: (in 3.js)
JavaScript:
requirejs(['fab/fabrik'], function () {
function getForm()
{
    var form = Fabrik.getBlock("form_3");
    if (form === false) {
        form = Fabrik.getBlock("details_3");
    }
    return form;

}
var form = getForm();
var field = form.elements.get('sprawa_17_repeat___notatki_ro_0');
var vaule =field.get('value');
//alert(value);
if (field = "" ) {
//alert(field);
    form.formElements.get('group17_tab').hide();
}
});
But hen a have error like : cannot read property 'get' or undefinded
The true is that i hate javascript, but sometimes I have to do something. :)
 
form.elements.get should be form.formElements.get and your if should be on the value not the field. Also check spelling of value.
 
Yes I change form formElements.get to elements.get beacuse first i try with: formElements.get and i have "cannot read property 'get' or undefinded" then as You see
try with elements.get and change also:
var field = form.formElements.get('sprawa_17_repeat___notatki_ro_0').get('value');
to
var field = form.elements.get('sprawa_17_repeat___notatki_ro_0');
var vaule =field.get('value');
Unfortunately none of these codes work.
 
Turn on the developers console and then put a console.log(xxx); in at each step and make sure you are getting what you think you are getting.
for example:
var form = getForm();
console.log(form);
var field = form.elements.get('sprawa_17_repeat___notatki_ro_0');
console.log(field);
etc.
 
Form elements won't exist yet. The requirejs(['fab/fabrik'], ...) runs your code during page load as soon as the main Fabrik module loads. But because of the async nature of JS, that's before the Fabrik form block is created and/or all the elements added.

So what you probably need to do is ...

Code:
requirejs(['fab/fabrik'], function() {
   Fabrik.addEvent('fabrik.form.elements.added', (function(form) {
      // your code here
   });
});

That event will fire and run your code once the form is created and the elements added.

NOTE - because that event passes you a reference to the form object, you don't need to worry about doing the getBlock(). Just use the 'form' you get passed.

-- hugh
 
I would like to correct code:
JavaScript:
requirejs(['fab/fabrik'], function() {
   Fabrik.addEvent('fabrik.form.elements.added', function(form) {
      // your code here
   });
});
 
Ok, so I modified as you suggested to:
JavaScript:
requirejs(['fab/fabrik'], function() {
   Fabrik.addEvent('fabrik.form.elements.added', function(form) {
  
    var field = form.formElements.get('sprawa_17_repeat___notatki_ro_0').get('value');
    if (field = '') {
    alert(field);
    form.formElements.get('group17_tab').hide();
}
   });
});
but I still have
upload_2017-10-15_21-49-32.pngupload_2017-10-15_21-49-32.png
 
hmm very strange, this is what I have in my database:

upload_2017-10-15_22-25-46.png
and this in debug - I do not see: sprawa_17_repeat___notatki
upload_2017-10-15_22-22-52.png
 
Do you have at least one created row in the repeating group?
Try with some other element of the form to see that everything works.
Otherwise, your syntax is correct, but I do not think you have a repeating row.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top