Help slickGrid newbie getting column value of selected row (dynamic grid with CF)

5,745 views
Skip to first unread message

GammaX

unread,
Aug 30, 2012, 5:04:11 PM8/30/12
to slic...@googlegroups.com
I have a working grid in which the user will make row selections and would like to retrieve value from a column not the row id.

Is this possible?  If the column definition is as follows below, I want the user to be able to select multiple rows and be able to click a button on the page holding the grid.  When clicked, I want to get at the value of column "subscriber_id" as an array. 

So if the column definition is:

var columns = [
    {id: "subscriber_id", name: "Chart Me", field: "subscriber_id"  },
    {id: "month_num", name: "Month", field: "month_num"  },
    {id: "year_num", name: "Year", field: "year_num"}
]

Then using Coldfusion to create the data
$(function () {
    var data = [];

    <cfset count = 0>
    <cfoutput query="SEL_Usage_Report_for_SubscriberType">
    data[#count#] = {
    subscriber_id: '#subscriber_id#' ,
    month_num: '#month_num#',
    year_num: '#year_num#'}
   <cfset count = incrementValue(count)>
   </cfoutput>
})

GammaX

unread,
Aug 31, 2012, 9:54:08 AM8/31/12
to slic...@googlegroups.com
 Sorry, my post probably wasn't asking clearly - I can retrieve selected rows by some example code found in this forum:

var selectedData = [],
     selectedIndexes;
var selectedIndexes = grid.getSelectedRows();
jQuery.each(selectedIndexes, function (index, value) {
      selectedData.push(grid.getData()[value]);
 });

How do i get at the values in each row, in my case, I want the values of each first column, rather than the row object (here "subscriber_id") in my column definition: var columns = [
    {id: "subscriber_id", name: "ID", field: "admin_subscriber_id"  }, ...


 

Robert Stackhouse

unread,
Aug 31, 2012, 10:05:57 AM8/31/12
to slic...@googlegroups.com

var gridData = grid.getData(), //before .each function


selectedData.push(gridData[value]['admin_subscriber']);

Robert Stackhouse

unread,
Aug 31, 2012, 10:06:36 AM8/31/12
to slic...@googlegroups.com

*admin_subscriber_id

Steve Fazekas

unread,
Aug 31, 2012, 10:19:21 AM8/31/12
to slic...@googlegroups.com
Glad to see ColdFusion is still alive...  This may help you. 

The code below may be crude, since I was just getting started on using slickgrid a month or so ago...
Using the rowselection method, I loop thru the "names" in the row to get each value if needed or JUST the "Id" column.
You could just call it with eval("row.id") instead of looping through.
I use this click method to trigger getting a second or third child grid AJAX request based on that ID value.

Hope the code below works well enough for you. 
---
I have changed my next approach to use the cell selection method for editing.
I am now working on setting up input selectors using lookups, (phone, zip, date/time & numeric) formatters, and input masks (meiomask.js) for different types of inputs for editable grid elements.
This is really awesome because the entire grid will use nearly every possible editable feature one could think of.

Cheers,
Steve Fazekas

---
Since I have multiple grids, I have created #grid[id] for each one...

        grid.onClick.subscribe(function(e, args){ //args=row,cell,grid
          var obj = grid.getCellFromEvent(e); /*for (var name in obj) {if (obj.hasOwnProperty(name)) {alert(name)}}*/
            var row=dataView.getItem(obj.row)
            for (var name in row) {
              if (row.hasOwnProperty(name)) {
                    if (name=='Id') {
                        cellVal=eval("row."+name);
                  }
              }
          }
            var fxn = "updateFields('"+grid.partid+"','"+cellVal+"')";
            //alert("Position: " + obj.cell + ":" + obj.row + ", value: " + cellVal + " partID: " +grid.partid)
            log(fxn);
            eval(fxn);
        });

Robert Stackhouse

unread,
Aug 31, 2012, 10:39:26 AM8/31/12
to slic...@googlegroups.com
How do i get at the values in each row, in my case, I want the values of each first column, rather than the row object (here "subscriber_id") in my column definition: var columns = [
{id: "subscriber_id", name: "ID", field: "admin_subscriber_id" }, …
 
The field property of the column definition is what is used to get the value of the field for the row in question from the data item
 

GammaX

unread,
Aug 31, 2012, 11:13:50 AM8/31/12
to slic...@googlegroups.com
Thanks Steve - appreciate the code and reply.

Just as you posted here, I came up with my my own solution:

After the grid loads, I have a button which my users can click to get the subscriber ids of selected rows. The alert sends an array of the ids I need!  Thanks again!

$("#countselected").click(function() {

               var selectedData = [],
            selectedIndexes;
            var m, el;
            var selectedIndexes = grid.getSelectedRows();
               // alert(selectedIndexes);
            jQuery.each(selectedIndexes, function (index, value) {
                m = grid.getData()[value];
                // var keys = Object.keys(m);
                var el = m["admin_subscriber_id"];
                  selectedData.push( el );
            });
            alert(selectedData);
        });
Reply all
Reply to author
Forward
0 new messages