Author Topic: Passing Variable to Grid  (Read 5536 times)

mjg77025

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 11
    • View Profile
Passing Variable to Grid
« on: February 04, 2015, 11:47:56 pm »
I need to pass a variable from my application's PHP code to a grid.   The javascript is an include in my main PHP file and the grid is called using the standard method.

Example:

We have a grid that stores items for a customer's order invoice.   The Primary PHP has all of the customer and invoice fields at the top of the page and the grid for items underneath.
Primary PHP script generates a variable that needs to be passed to a field (lets call this field CustomerInvoiceKey) in the grid for each new row.   This variable is generated from user input from a previous form page and is unique to the user and invoice that is being proccessed.

How would I pass that variable to the grid so that it shows up in the proper field on each line in the grid?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Passing Variable to Grid
« Reply #1 on: February 05, 2015, 12:16:28 pm »
Grid data is an array of row objects and you want to pass a unique variable to every row which can be simply done as.

Code: [Select]
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); //assuming you get rows from database via PDO.
        foreach ($rows as $i=> &$row)
        {
            $row[ 'CustomerInvoiceKey' ] = $value[ $i ]; //this is where you add a new field to every row
        }

Also add a column with dataIndx: CustomerInvoiceKey in colModel so that it shows up in the grid.
« Last Edit: February 05, 2015, 12:20:12 pm by paramquery »

mjg77025

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Passing Variable to Grid
« Reply #2 on: February 06, 2015, 12:18:31 pm »
I have that setup in colModel already.   Here is a little better explanation of what I'm trying to do.


In my PHP file that is for my application I call the datagrid JS file and then call the grid in my php file using this:


<div class="tabContent" id="receiverdetail" >
       <div id="dsdreceiverdetailgrid" >
       </div>
</div>

The variable is created in my PHP file for my application before I call the grid.

so....

$retail_grid_combo key=$vendor.$invoice.$invoice_date;

This variable is directly before the <div> to call the grid into my application page.

How would I pass that variable into the php file that inserts the data from the grid?   Or would I need to pass the variable to the javascript file first?

Thanks!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Passing Variable to Grid
« Reply #3 on: February 06, 2015, 08:55:04 pm »
you can't pass a variable to div from PHP. Having said that though you can add an attribute to div from PHP

<div id="dsdreceiverdetailgrid" data-name = "some data here" >
</div>

and on the client side you would need to read that attribute value with jQuery.

But it seems you need to associate a variable with every row, so it would be better to do it at the same place where you define the row data for pqgrid.
« Last Edit: February 06, 2015, 10:36:36 pm by paramquery »

mjg77025

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Passing Variable to Grid
« Reply #4 on: February 10, 2015, 09:55:03 am »
I need it as part of my SQL query that pulls certain data from the database.

 $sql = "Select dsd_detail_id, dsd_detail_combo_key, dsd_detail_item, dsd_detail_item_units,
        dsd_detail_item_cases, dsd_detail_item_case_cost, dsd_detail_item_unit_cost, dsd_detail_item_allowance_one,
        dsd_detail_item_allowance_two from dsd_detail where dsd_detail_combo_key='$dsd_combo_key'";
        $dbh = new PDO("pgsql:host=localhost;dbname=fredhost","xxxx123","xxxx179");
        $stmt = $dbh->prepare($sql);
        $stmt->execute();
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

However, since the .js file is calling the .php file that does the select the problem is getting the data from the main page and then to either the .js or .php files for building the grid.

Any thoughts?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Passing Variable to Grid
« Reply #5 on: February 10, 2015, 07:58:54 pm »
Your question is not understood precisely and doesn't seem directly related to the grid.

I assume you need to pass some data from some main page to your PHP script where you build select query for the grid.

Can't you send data from main page to PHP script using GET/POST with Ajax?
« Last Edit: February 10, 2015, 08:01:20 pm by paramquery »

mjg77025

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Passing Variable to Grid
« Reply #6 on: February 10, 2015, 10:58:56 pm »
My issue does relate to the grid as it involves getting the proper data to appear in the grid.   Let me see if I can explain this a little better.

There are three files
receiverdetail.php                  This file is my application file and where the grid is called in a div.   This file also contains a variable $dsd_combo_key
receiverdetailgrid.php             This file is the .php file for the grid and is called by receiverdetailgrid.js
receiverdetailgrid.js                This is the javascript file for the grid


The variable $dsd_combo_key is a combination of two variables $dsd_vendor and $dsd_vendor_invoice_number.  This number is created from a form that calls receiverdetail.php upon submit. In order to make sure the grid is pulling the right information I need to be able to query the database and pull only items with the correct $dsd_combo_key in the grid on the receiverdetail.php.

Here is my dilemma... receiverdetail.php has both the variable and the code for the div.   When the div is called it then executes receiverdetailgrid.js which calls receiverdetailgrid.php and executes SQL depending on whether the action is select, insert, or delete.   On the select in receiverdetailgrid.php I need to be able to give that SQL statement a where   ( where dsd_detail_combo_key='$dsd_combo_key') that uses the variable from the initial page that contains the div.  That Where clause will only display information that has the same $dsd_combo_key.

So my question is how would I get that data to the grid?   I'm sure there are other people out there that would like to use the grid dynamically depending on data from a form to create the grid.  But I can't seem to understand how to pass that variable from .php to .js to .php so that I can get the proper variable data for my select statement.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Passing Variable to Grid
« Reply #7 on: February 10, 2015, 11:27:22 pm »
Thanks for the explanation, your question makes perfect sense.

Basically it boils down to sending custom data from grid to server which can be done either by implementing dataModel.getUrl callback
or

use of postData/postDataOnce options, especially the callback variant is more suitable to your use case.

http://paramquery.com/pro/api#option-dataModel-postData

postData: function( ui ){
   //here you read the variables from the other control and return them.
   return { dsd_vendor: dsd_vendor_value, dsd_vendor_invoice_number: dsd_vendor_invoice_number_value };
}

on the server side PHP script, those variables can be accessed as $_GET / $_POST["dsd_vendor"] and $_GET/ $_POST["dsd_vendor_invoice_number"]
« Last Edit: February 10, 2015, 11:34:48 pm by paramquery »