最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery and jqGrid - For ... Each Loop To Extract Data - Stack Overflow

programmeradmin1浏览0评论

Environment:
* jquery-2.0.0.js
* jqSuitePHP_4_4_4_0

I have a grid populated with data; for every row in the grid I would like to extract only two of the columns, the same two columns for each row. I don't need the jgGrid ID of the row as I have my own which is one of the two fields I am after. The second field I want holds a select menu - I want the value as opposed to the text of the field.

In my experimenting, the best I could e up with was capturing the entirety of each row and JSON encoding it to submit.

jQuery('#myGrid').jqGrid().jqGrid
(
    'navButtonAdd', '#myPager', 
    {
        ...

        onClickButton: function()
        {
            var grid = $('#myGrid');
            var data = JSON.stringify(grid.jqGrid('getGridParam', 'data'));

            request = $.ajax({
                url: 'process.php',
                type: 'post',
                data: data
            });
        }
    }
);

The problem is I don't want to do that. So I'm stuck on how to for ... each to build my 'data' value that can be seen in the sample code in the AJAX POST. I want POST as 1=this&2=that where the integers are my row IDs (the first field I want to capture) and 'this' and 'that' are the values of the select menus (the second field I want) and do away with the JSON.

Thanks for your help.

=== EDIT ===
* Adding sample grid
* Highlighting the portion of code that is challenging me

+---------------------------------------------+
| Column 1 | * Column 2 | Column 3 | Column 4 |
|----------|------------|----------|----------|
| 1        | This       | Other    | Stuff    |
|----------|------------|----------|----------|
| 2        | That       | More     | Stuff    |
+---------------------------------------------+

*Column 2 is actually a select menu so rather than the text that appears to the user in the grid, I want the underlying value that they set. For conversation, we'll say 'This' = 100 and 'That' = 999. So from the sample above I want to be able to AJAX POST:

1=100
2=999

My AJAX request...

request = $.ajax({
    url: 'process.php',
    type: 'post',
    data: data
});

How do I get 1=100 and 2=999 into the variable 'data' to be used in the above AJAX request? I think I'm supposed to be using $.each() but not sure and even if that is correct I don't know what beyond that. I don't want Column 3 or 4.

Thanks for your time and thought.

Environment:
* jquery-2.0.0.js
* jqSuitePHP_4_4_4_0

I have a grid populated with data; for every row in the grid I would like to extract only two of the columns, the same two columns for each row. I don't need the jgGrid ID of the row as I have my own which is one of the two fields I am after. The second field I want holds a select menu - I want the value as opposed to the text of the field.

In my experimenting, the best I could e up with was capturing the entirety of each row and JSON encoding it to submit.

jQuery('#myGrid').jqGrid().jqGrid
(
    'navButtonAdd', '#myPager', 
    {
        ...

        onClickButton: function()
        {
            var grid = $('#myGrid');
            var data = JSON.stringify(grid.jqGrid('getGridParam', 'data'));

            request = $.ajax({
                url: 'process.php',
                type: 'post',
                data: data
            });
        }
    }
);

The problem is I don't want to do that. So I'm stuck on how to for ... each to build my 'data' value that can be seen in the sample code in the AJAX POST. I want POST as 1=this&2=that where the integers are my row IDs (the first field I want to capture) and 'this' and 'that' are the values of the select menus (the second field I want) and do away with the JSON.

Thanks for your help.

=== EDIT ===
* Adding sample grid
* Highlighting the portion of code that is challenging me

+---------------------------------------------+
| Column 1 | * Column 2 | Column 3 | Column 4 |
|----------|------------|----------|----------|
| 1        | This       | Other    | Stuff    |
|----------|------------|----------|----------|
| 2        | That       | More     | Stuff    |
+---------------------------------------------+

*Column 2 is actually a select menu so rather than the text that appears to the user in the grid, I want the underlying value that they set. For conversation, we'll say 'This' = 100 and 'That' = 999. So from the sample above I want to be able to AJAX POST:

1=100
2=999

My AJAX request...

request = $.ajax({
    url: 'process.php',
    type: 'post',
    data: data
});

How do I get 1=100 and 2=999 into the variable 'data' to be used in the above AJAX request? I think I'm supposed to be using $.each() but not sure and even if that is correct I don't know what beyond that. I don't want Column 3 or 4.

Thanks for your time and thought.

Share Improve this question edited Dec 17, 2013 at 14:48 user1801810 asked Dec 16, 2013 at 21:36 user1801810user1801810 6141 gold badge11 silver badges30 bronze badges 2
  • Hmmm, I wonder why I am not getting any responses. I provided environment info, well formatted and clear code that I currently have working, described where I want to get to and what my problem is. *scratches head – user1801810 Commented Dec 17, 2013 at 6:54
  • See my solution to my question below. Thanks! – user1801810 Commented Dec 18, 2013 at 18:17
Add a ment  | 

1 Answer 1

Reset to default 2

So here's what I came up with on my own and it works nicely.

onClickButton: function()
{
    var data = {};
    var grid = $('#myGrid');
    var rows = grid.jqGrid('getDataIDs');

    for (i = 0; i < rows.length; i++)
    {
        var rowData = grid.jqGrid('getRowData', rows[i]);
        data[rowData['field1']] = rowData['field2'];
    }

    request = $.ajax({
        url: 'process.php',
        type: 'post',
        data: {data: data}
    });
}

This yields a POST I can work with; based on my sample data the parameters and values are...

  • data[1]=This
  • data[2]=That

...which when POSTed is...

data%5B1%5D=This&data%5B2%5D=That

...which PHP sees as an array.

发布评论

评论列表(0)

  1. 暂无评论