سؤال

Good evening,

Initial situation

right now when creating a grid I pass parameters to the server using static parameters like:

  var $grid = jQuery("#grid-table").jqGrid({
    url: '?c=grid.skill.GridSkill&json&o=get-data&type=1',
    postData: {
      'person_ref': function () { return $('#filter-person_ref').val();},
      'country_ref': function () { return $('#filter-country_ref').val(); },
    },
    mtype: 'POST',
    datatype: "json",
    colNames: self.columnTitles,
    colModel: self.columnNames,
    ...
  });

which is pretty hard to maintain when using complex filter mechanisms (outside of the jqGrid controls).


Desired approach

Instead of the static approach, I tried setting the postData values using a function call:

/**
 * extract filter data from LeftBar additionally to the person filter
 */
self.preparePostData = function () {
  var arrPostData = {};

  // retrieve values from select boxes ...
  for (var i in LeftBar.arrAdditionalFilter) {
    arrPostData[LeftBar.arrAdditionalFilter[i].field] = $('#' + LeftBar.arrAdditionalFilter[i].inputField).val();
  }

  // ... and add checkbox values
  for (var i in LeftBar.arrCheckBoxFilter) {
    arrPostData[LeftBar.arrCheckBoxFilter[i].field] = $('#' + LeftBar.arrCheckBoxFilter[i].inputField).is(':checked') ? 1 : 0;
  }
  console.log(arrPostData);
  return arrPostData;
};

self.performLayout = function () {
  var $grid = jQuery("#grid-table").jqGrid({
    url: '?c=grid.skill.GridSkill&json&o=get-data&type=' + PageControl.skillTypeRef,
    postData: function () {
      return self.preparePostData();
    },
    mtype: 'POST',
    datatype: "json",
    colNames: self.columnTitles,
    colModel: self.columnNames,
    ...
  });

Modifying the previous method

If I try it using the function it never gets executed, so I used a try-and-error approach like:

...
  postData: {'data': function() { 
    return self.preparePostData();
  }},

which will give "data: [Object object]" as POST parameter.

هل كانت مفيدة؟

المحلول

As mentioned above, while struggling with the data and trying different approaches it helped me find a solution and I hope it is worth sharing.


My Solution

While writing this question down and playing around I found the solution to my own problem :-) I'll complete it anyway for future readers searching for a similar solution and to hopefully read about better approaches.

I am using a PHP-based web service to generate the grid data, what in the end gave me the post data I required was:

<?php
// NOTICE: debug code / contains demo statements from my tests

$objData= json_decode($_POST['data']);

// will give you an object of type StdClass ...
if ($objData) {

  // ... but instead of using dynamic property access like '$obj->{$field}'
  // convert it to a standard array, use it like using '$_POST' variables
  try {
    $arrPostFilter= get_object_vars($objData);
    print 'Hello Country-Ref: '.$arrPostFilter['country_ref'];
  } catch (Exception $ex) {
    print 'Malformed data retrieved, error was: '.$ex->getMessage();
  }
}

Thanks for reading till the end and for your comments (either because the solution is crappy or it could help :-) ).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top