1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Retrieve values from db using JS

Discussion in 'Community' started by andre00silva, Jun 8, 2018.

  1. andre00silva

    andre00silva Member

    Level: Community
    Hello!
    I'm my list "Pedidos de Or?amento" I have a php button which, when clicked, inserts into my "Or?amentos" list the respective row values (which are quotation requests and in the moment the button is pressed, they become quotations too). This button also creates a new dir and a new excel file inside of it in a specific path (moldes/Molde_{table_name___id}/Orcamento161.xlsm). Every quotation request excel file path is stored in my list "Settings" which has some elements: "file_name", "file_path" and an inner join with the "Pedidos de Or?amento" ... etc.
    In the list "Or?amentos" I have another php button with an excel logo that allows, when clicked, to open the excel file using JS:

    Code (Text):
    jQuery.each(rows, function(rowid, row) {
      window.open('../'+row.fzmms_settings___file_path+'/'+row.fzmms_settings___file_name);
    })


    The problem is, I can't seem to retrieve the values of that list. Using this I can only retrieve the values of the list "Or?amentos"... When I do this that you're seeing, the path becomes "unavailable/unavailable". How can I retrieve those values from the db and use them in the link?
     
  2. andre00silva

    andre00silva Member

    Level: Community
    friendly bump :)
     
  3. startpoint

    startpoint Active Member

    Level: Community
    Code (Javascript):
    requirejs(['fab/fabrik'], function() {
        Fabrik.addEvent('fabrik.list.loaded', function(list) {
            var data = list.options.data;
            // data is an array of groups, each group is an object of rows, each row has a data object, each data object has a  __pk_val item which will have the row id
            data.each(function (group) {
                group.each(function (row) {
                    // do whatever you need with row.data.__pk_val
                    console.log(row.data.__pk_val); //row id
                });
            });
        });
    });
     
  4. andre00silva

    andre00silva Member

    Level: Community
    So if I want to call the window.open method all I need to do is:


    Code (Javascript):
    requirejs(['fab/fabrik'], function() {
        Fabrik.addEvent('fabrik.list.loaded', function(list) {
            var data = list.options.data;
            data.each(function (group) {
                group.each(function (row) {
                    console.log(row.data.__pk_val);
                    window.open();
                });
            });
        });
    });
    But, what do I put inside my window.open() method? Sorry, I'm noob in Fabrik ahah
     
  5. startpoint

    startpoint Active Member

    Level: Community
    Maybe:
    Code (Javascript):
    window.open('../'+row.data.fzmms_settings___file_path+'/'+row.data.fzmms_settings___file_name);
     
  6. andre00silva

    andre00silva Member

    Level: Community
    D
    Does not work. I've debugged and it won't pass the addEvent
     
  7. startpoint

    startpoint Active Member

    Level: Community
    I don't understand what you mean and I can't help you.
     
  8. andre00silva

    andre00silva Member

    Level: Community
    I have a php button in my list called "Pedidos de Or?amento". That php button basically sends to my list "Settings" a value (path of a file "moldes/moldes_x/orcamento.xlsm"). The values stay stored in that "Settings" list.
    In other list that I have, called "Or?amentos", I have another php button which basically allows the user to open the path that was stored in Settings previously. How can I open that path that is stored in "Settings" using window.open() when clicking in that php button that I have in "Or?amentos"?
     
  9. startpoint

    startpoint Active Member

    Level: Community
    Use js plugin instead php plugin.
    Create your js file and select from plugin.
    In js file write ajax query to retrieve datas from your db.
    Get Selected row id:
    Code (Javascript):
    var selected_row_id;
    jQuery.each(rows, function(rowid, row) {
      console.log(rowid);
    selected_row_id = rowid;
    })
    Code (Javascript):
    var url = "index.php?option=com_fabrik&format=raw&task=plugin.userAjax&method=get_file_data&row_id=" + selected_row_id;
    new Request({url:url,
        dataType:'json',
        onComplete:function(r){
            console.log('result: ', r);
            var file_name = r.file_name;
            var file_path = r.file_path;
            //format your result then create your url
            var url = ....;
            window.open(url);
        }  
    }).send();
    In user_ajax.php create your function to get file data.
    For example:
    PHP:
        public function get_file_data()
        {
            $db = FabrikWorker::getDbo();
            $app = JFactory::getApplication();
            $input = $app->input;
            $row_id = $input->get('row_id, '');
            $query = "SELECT `file_path`,`file_name` FROM `YOUR_TABLE` WHERE `id` = "  . $db->quote($row_id);
            $db->setQuery($query);
            $result = $db->loadObjectList();
            //echo '
    <pre>' . var_export($result, true) . '</pre>';
            echo json_encode($result);
    }
     
    Last edited: Jun 26, 2018

Share This Page