andrux
Newbie
Posts: 26
|
|
« on: May 09, 2013, 07:11:05 PM » |
|
This one should be easy, I want to add a column to my datagrid where the field value is just a static string. I used the formatter option but it only works when you edit the grid - and I want it to always be there.
Anyone knows how to accomplish this via javascript initialization?
Attached is a screenshot of what I want, it's the first column the one I want to be always the same.
Thanks
|
|
|
Logged
|
|
|
|
joe
|
|
« Reply #1 on: May 09, 2013, 10:18:55 PM » |
|
$('#dg').datagrid({ data: [ {f1:'value11', f2:'value12'}, {f1:'value21', f2:'value22'} ] });
|
|
|
Logged
|
|
|
|
andrux
Newbie
Posts: 26
|
|
« Reply #2 on: May 10, 2013, 08:47:21 AM » |
|
Thank you, Joe, but I have all my data loaded from a MySQL query, I just want the first column with a static string, and since the query is dinamically selected, I can't just put the string inside the query... actually... I might be able to...
|
|
|
Logged
|
|
|
|
andrux
Newbie
Posts: 26
|
|
« Reply #3 on: May 10, 2013, 08:56:04 AM » |
|
yup,it worked... I just hope none of the queries break my little hack
I did it by doing a search and replacement in my query, like this: $query = str_replace( 'from', ', \'' . $_GET['id'] . '\' as queryId from', $options[ $_GET['id'] ]['query'] );
so that a query like "select * from wp_options" would end up like "select *, '12345' as queryId from wp_options"
Thanks for the help Joe, if you come up with another way of doing it via Javascript I'd appreciate it.
|
|
|
Logged
|
|
|
|
joe
|
|
« Reply #4 on: May 10, 2013, 10:26:55 AM » |
|
Option 1 - pass in your string param to your SQL and push it back out as json instead of using string replace. Option 2 - use the formatter:
<th data-options="field:'Action',width:30" formatter="rowAction">my static column</th>
function rowAction(){ string sMyStaticString = 'hello'; return sMyStaticString; }
|
|
|
Logged
|
|
|
|
andrux
Newbie
Posts: 26
|
|
« Reply #5 on: May 10, 2013, 10:32:48 AM » |
|
I don't understand what you mean for option 1
About option 2, I tried using the formatter but my static string only showed when in edit mode. Also, I'm loading via javascript
|
|
|
Logged
|
|
|
|
joe
|
|
« Reply #6 on: May 10, 2013, 10:46:56 AM » |
|
you are basically passing in a querying string param in your url. i.e. ...&myString=Hello, add the param to your select statement. i.e. select @myString ... and return it back to your grid.
try option 2, it should work.
|
|
|
Logged
|
|
|
|
Kevin
|
|
« Reply #7 on: May 12, 2013, 07:17:16 AM » |
|
I agree with Joe, option 2 (formatter) should work. Here is an example of this using jeasyui sample code. I just added the fixed column. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Google Maps Test</title> <link class="jeasycss" rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/default/easyui.css" /> <script src="http://code.jquery.com/jquery-1.8.0.min.js" type="text/javascript"></script> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script src="http://www.jeasyui.com/easyui/jquery.easyui.min.js" type="text/javascript"></script> <script type="text/javascript"> function fixcol() { return '1234'; } $(function () { $('#dg').datagrid({ url: 'datagrid_data1.json', height: 300, width: 700, fit: true, striped: true, singleSelect: true, idField: 'productid', columns: [[ { field: 'fixcol', title: 'Fixed Col', width: 80, formatter: fixcol }, { field: 'itemid', title: 'Item ID', width: 80}, { field: 'productid', title: 'Product', width: 100 }, { field: 'listprice', title: 'List Price', width: 80, align: 'right' }, { field: 'unitcost', title: 'Unit Cost', width: 80, align: 'right' }, { field: 'attr1', title: 'Attribute', width: 250}, { field: 'status', title: 'Status', width:60, align: 'center' } ]], });
}); </script> </head> <body> <table id='dg'></table> </body> </html>
|
|
|
Logged
|
|
|
|
andrux
Newbie
Posts: 26
|
|
« Reply #8 on: May 12, 2013, 09:21:27 AM » |
|
I just tested and it does work, I don't know why I had problems with this before, I probably just did something wrong... Anyway I removed my str_replace solution and I used this instead, adding the string to the $row object while fetching the record, it works just as good as the formatter options for me: $response = array(); $response['total'] = $row_count;
$items = array();
while ( $row = mysqli_fetch_object( $result ) ) { $row->queryId = '12345'; $items[] = $row; }
$response['rows'] = $items;
echo json_encode( $response );
Thank you, guys
|
|
|
Logged
|
|
|
|
|