26

I have below code that get values from a grid. Instead of printing the values, the grid is printing data in below object format. How to print values?

[object Object], [object Object], [object Object], [object Object], [object Object]

CODE:

$(document).ready(function () {
    $("#check").click(function(){
        var rows = $('#jqxgrid').jqxGrid('getrows');
        alert(rows);
    });
});        
4
  • Which values are that? How do you want them to be printed? Commented Oct 13, 2012 at 23:09
  • following format. [{"empId": "1","fname": "Henry","lname":"Rey","empcat":"Staff","dept":"IT","jTitle":"Software Engg","rAccess":"New Hire","lManager":"TigoAdmin","sDate":"2012-10-11","eDate":"2012-10-11"}] Commented Oct 13, 2012 at 23:15
  • That's JSON. Just use alert(JSON.stringify(rows)) - the JSON object is supported in every modern browser Commented Oct 13, 2012 at 23:55
  • sounds like JSOn structure isn't the same as plugin expects. Floow example docs and compare structures Commented Oct 14, 2012 at 3:05

4 Answers 4

59

A simple way is to use

JSON.stringify(rows)

i.e.

alert(JSON.stringify(rows))

Else, you would need to manually traverse the object's properties and print them accordingly. Give more details about input and desired output for an example.

An example in interactive Node.js:

> x = { 1: 2, 3:4 };
{ '1': 2, '3': 4 }

> x.toString();
'[object Object]'

> JSON.stringify(x)
'{"1":2,"3":4}'
Sign up to request clarification or add additional context in comments.

4 Comments

following format. [{"empId": "1","fname": "Henry","lname":"Rey","empcat":"Staff","dept":"IT","jTitle":"Software Engg","rAccess":"New Hire","lManager":"TigoAdmin","sDate":"2012-10-11","eDate":"2012-10-11"}]
exactly what stringify does, doesn't it? besides, you can add such additional information to the question itself, it makes the Q&A thread better to read.
do I need to include any related files for this? I am getting "JSON is undefined" error in IE.
Rudolf, your answer is precise and perfect! Thanks for the help.
1

In order to print value of objects in JQuery you can create a new array with values and print it:

var array =  $.map(object, function(value){
    return value;
})

Comments

0

Grid's getrows returns an array of rows. To get the first row: var row = rows[0]. To get the second row use: var row2 = rows[1] and so on.

To get the 'fname' cell value from the first row, you can do this:

var rows = $("#grid").jqxGrid('getrows');
var firstRow = rows[0];
var fnameValue = firstRow.fname;

Comments

0

Got help from stackoverflow, Include following jquery function:

   The result will be equivalent to the PHP function print_r.
   echo '<pre>' . print_r($data) . '</pre>';

   USAGE: 
   var data = [{'id':1,'name':'hello'},'world'];
   $('#element').print_r(data);

//==========================================
(function($) {

    $.fn.print_r = $.fn.print = function(variable){
        return this.each(function(){
        if(typeof variable == 'object'){
            var string = $.print_r.objectToString(variable,0);
            $(this).html(string);
        } else {
            $(this).html('<pre>'+variable.toString()+'</pre>');
        }
    });

    }

    $.print_r = {
            objectToString : function (variable,i){
              var string = '';
              if(typeof variable == 'object' && i < 3){ // 3 is to prevent endless recursion, set higher for more depth
                  string += 'Object ( <ul style="list-style:none;">';
                  var key;
                  for(key in variable) {
                      if (variable.hasOwnProperty(key)) {
                        string += '<li>['+key+'] => ';
                        string += $.print_r.objectToString(variable[key],i+1);
                        string += '</li>';
                      }
                  }
                  string += '</ul> )';
              } else {
                  string = variable.toString();
              }
              return string;
        }
    }

})(jQuery)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.