jqGrid method for posting specific cell - jqgrid

I have a grid with a method that calculates a value depending on different cells in the row.
I then set a specific cell to contain this value:
$("#gridID").setCell(rowid, "Cell_Name", myCalculatedValue);
I then want to finalize this by saving the value to the database but I cannot seem to find a method for this.
Basically what I want is
$("#gridID").saveCellToDataBase(rowid, "Cell_Name");
Since this cell is not the cell edited but a calculated value due to another cell being edited it's not saved by default.
I was hoping that methods that could solve my problem would be:
$("#gridID").jqGrid("saveRow", rowid, true);
or
$("#gridID").saveCell(rowid, "Cell_Name");
But none of these methods seem to do what I want.

If I understand correct you try to post a data to the server, but one (or more) field(s) is not editable and depend on calculation of other fields something like Quantity*UnitPrice.
As you know the not editable fields are not posted to the server and hence they can not be saved. In order to make this happen I see here two possible solutions.
Make the calculations at the server, without to post this value from jqGrid
Calculate the value in jqGrid and post it to the server
All of these have pro and cons, and I suppose that you can not make this calculation at server.
To post additional value to the server from editing depend on the editing method used - form edit, inline edit or cell edit.
Below I will describe a possible solution with inline edit and as exercise you will find it for other methods.
In jqGrid there is mechanism to post to post what you want to the server again with the edited fields. In our terms we name it serialization. This serialization happen before posting the data to the server and have different names depending on the editing module.
In case of inline edit this event is called serializeRowData and it is a grid parameter and not a parameter to the event.
In your case the code should like this:
$("jqGrid").jqGrid({
...
serializeRowData : function( data ) {
// suppose you have Qty and Unit field and want to post Total = Qty*Unit
if(data.Qty && data.Unit) {
data.Total = data.Qty*data.Unit;
}
return data;
}
Note the return at end of serializeRowData.
So this event execute every time you call saveRow, but this is well described in Guriddo documentation

Related

Passing additional cell data into JqGrid - while keeping sorting/grouping working

We are loading local data into jqgrid using the "jsonstring" datatype (I think this should behave the same as an AJAX request managed by jqgrid?). We are also using groupBy, sorting and custom cell formatting. I'm trying to get this working with structured cell data, like so:
var jqGridData = {"total":1,"page":1,"records":10,
"rows":[{
"id":"24_DisclosureFI",
"cell":["True", {"Value":"24_DisclosureFI","Source":"Input","OriginalValue":null}]
]} }
and so on. The idea being that some of our cellAttr function can use the additional info to make decisions on how to format that specific cell. However, adding this extended structure causes issues with grouping.
We've included a sortType function which gets the embedded cell value, and a customFormatter that does the same. These work, however when doing a groupBy it just takes the first cell value it finds and sticks everything in it. I've tried defining isInTheSameGroup() but it never gets called when we have the structured data (it works fine on non structured cell data).
If we turn off the structured data, all works fine (except we can't display the cell formatting we want). If we turn off the grouping, then the sorting/cell formatting works fine. But can't seem to find a combination where both works.
Any suggestions?

jqGrid: how to keep the 'Save' button enabled after adding a row

Im looking for a way to add a new row and keep the form data together with the Save button enabled, to have the chance to resend the same data already sent.
What im trying to achieve is to facilitate the submission of similar-but-not-equal rows. So, for example, if a merchant receives an order all of the same X product, but with slightly differences, he can keep the common fields after saving a product and only change the different properties to submit the next one, and so on.
Then the server will throw an error if exactly the same data is sent more than once.
As per our discussion I would refer to the following as an example of adding a custom button to the Add form:
How to add custom buttons to JqGrid add/edit forms?
as for saving the information you can use the the documentation as an example, I think the beforeSubmit event would work for saving the field data
In jqGrid 4.4.4, file jquery.jqGrid.min.js:
At line 279, after 'beforeSubmit' takes place, you will find the following statement: if(k[0]&&!b[d.p.id].processing), the second part of the test means something like 'if request is not being processed', then after the 'processing' variable is set to true, the request to the server is performed.
What prevents to resend data is that the processing variable is never set back to false 'afterSubmit' for example.
So, my solution was to do exactly that: b[d.p.id].processing=false; at the end of the if block performing the action, this is done on line 287, col 55.
This way i can resend slightly different 'products' and just let the server to manage errors.
I suppose it could be a bug in the library to not 'close' the processing state by setting back the variable to false.

Kendo UI Datasource and Arrays

I am sending over a series of array values from a posted form to an MVC3 Controller. I was hoping the default modelbinder would be able to parse this but I'm having some difficulty with it.
The array is in the following format:
order[0].[type]=some value.
I think this is the reason the model binder is not parsing my values because I'm not getting anything populated in my model.
What would be another way to handle this?
Probably need to post more of your code so I can see what you are doing exactly. However saying this you need to pass the model to the view/partial view on the response you are trying to retrieve on the post request.
If not you will have to iterate through the Form Collection that will be returned and the Actions Methods type e.g. ActionMethodName(FormCollection form), one issue is name versus id its the name of the Kendo UI control that is used to get the value not the id.
1As far as I remember the right format was:
orders[0].OrderID=13;
orders[0].Name="test";
orders[1].OrderID=15;
orders[1].Name="again test";
The indexing should start from 0 and increase by 1.
Check this out: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Form from another model in a view

So I'm trying to extend the Blog tutorial adding some comments:
Post hasMany Comments
I want to display the add comment form in the same view as the 'post view'. Thing is I don't know the best way to get this approach. I thought about three ways:
Creating a function in Comments Controller to handle the data.
Creating a function in Post Controller to handle the data.
Deal with the data in the same function that deals with the post views.
The main problem with the two first 'solutions' is that the validation errors doesn't show up in the form unless I do some messy hacking of saving the invalidated field in a session variable and then parsing the variable on the beforeFilter callback, like this:
function beforeFilter () {
if ($this->Session->check('comment_error')) {
$this->Post->Comment->validationErrors = $this->Session->read('comment_error');
$this->Session->delete('comment_error');
}
}
What I basically do is adapt the invalidated fields to the actual view and allow it to show properly. This works really well, but it seems so ugly to me. What would be the best approach?
Another related question: should a controller reflect a view? I mean on that example, I thought about only having a Comment Model and dealing with all the data in the controller where's the form to add a comment (even though it's in the Post Controller).
Sounds like you're looking for the Mutlivalidatable behaviour: http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
This allows you to define more than 1 validation ruleset per model. Use your controller to determine which one to apply upon posting something.
P.S. I have only ever used this on a Cake 1.3 project, not sure if it'll work on 2.0.
I see it this way:
Under every post there is an input box "Add comment" with a button to submit.
After submitting some text a form redirects to comments_controller where the comment is saved with this post_id, body, author, date etc.
After the comment is saved and all the logic is done it takes you back to the post.
Under each post there are all related comments displayed (having the same post_id sorted by date or whatever).

jqGrid : child-element ids can conflict when more than one grid appears on page: is there a setting to avoid this?

When you have more than one jqGrid on the page at the same time, their child-element ids can conflict. You might want to have three or four identical grids stacked one above the other, showing the same data but from different years.
Child-element ids should have the table-id prepended to them. Is there a "fully-qualified|verbose ids" setting that does this?
Thanks
First of all I find your question very interesting, so +1 for the question. What you find out can be really large problem which can be difficult diagnosed. So I recommend you to place the corresponding suggestion in the feature request on http://www.trirand.com/blog/?page_id=393/feature-request/.
Now about the workaround possibilities. The easiest way is to generate an unique Ids for different grids on the server side. But it is of cause not always possible. You can add an id_prefix as an additional parameter of your server code which produce JSON/XML reply from jqGrid. Then server should add this prefix to all ids before sending back to jqGrid. This workaround is not nice but it will work.
Another way which I see is the changing of id of all jqGrids rows with respect of jsonReader (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#jsonreader_as_function). If you add to the jqGrid the parameter like following:
jsonReader: { id: function(obj) { return "BlaBla" + obj.id; } }
then all ids in the table will receive prefix "BlaBla". If you use jqGrids on your page only to display data in the grid this can be your solution. You should don't forget about possible new problems which produce this workaround. If you use master/detail scenario, Edit/Delete etc you have to take in consideration, that ids of grids will have prefixes now. So you will have to include additional code in evens like onclickSubmit, serializeRowData, serializeGridData and so on to cut the prefix before the usage of id or before sending ids to the server.

Resources