MVC - Datagrid binding without a model - asp.net-mvc

Ok, I'll explain. I need to create a datagrid in MVC on the fly (potentially multiple datagrids on a view) depending on a XML file being read in. The file gets looped through and may contain multiple grids of data, the headers and rows are set in the file. The problem being that my application won't know the content of the XML file before reading it so I can't apply it to an IEnumerable model. The idea being to make it generic so that it can read in any XML file I pass to it set up as below and put the data in a sortable datagrid. Is this at all possible with current controls out there? I have tried Teleriks MVC grid and while I can read the data into the grid, I cannot sort the data as this feature will only work when passed a DTO. I have a feeling what I'm looking for can't be done (unless I write a custom HTMLHelper or something) but no harm in asking I guess
My xml will be as such
<xml>
<REPORT>
<HEADERS>
<HEAD>Col1</HEAD>
<HEAD>Col2</HEAD>
</HEADERS>
<ROWS>
<ROW>Data1</ROW>
<ROW>Data2</ROW>
</ROWS>
</REPORT>
</xml>
Thanks

I would advise using the jquery grid. Then write a class to parse the xml and generate the grid javascript in the view, and a second action to parse the xml (again) and generate the json result. MVCCrud may help with the idea there is a generic jquery grid in there but it works off an IQueryable list so would need to be adapted.

I haven't seen a helper extension out there that does what you need.
There are some good ones that work with generic collections (such as Telerik's or MVCContrib's). The sample you provide cannot be translated in to a collection that would be handled by these however: the row needs to have cells that can be matched to the header elements.
<xml>
<REPORT>
<HEADERS>
<HEAD>Col1</HEAD>
<HEAD>Col2</HEAD>
</HEADERS>
<ROWS>
<ROW><CELL>Data1</CELL><CELL>Data2</CELL></ROW>
<ROW><CELL>Data2</CELL><CELL>Data4</CELL></ROW>
</ROWS>
</REPORT>
</xml>
If the XML can be deserialized into a generic collection, it is easy to populate the grid.
Hope this helps.

I'd look into the JQuery Grid. You have to munge the data into the format that it wants, but it gives you a lot of flexibility and nice UI for free. You will still need to write sort code, though.

Related

struts2 and dojo grid

I need an example for populating the dojo grid with the data coming from server. I have struts2 and dojo application in which action class is setting a model variable of type hashmap as the result containing different values that needs to be shown in the form of grid.
Now how can i represent this hashmap in the form of grid data. can you please giving a sample example application which converts the hashmap to json and then uses this json to populate the grid.
I use Jackson to serialize to JSON. Does each item in the map represent a row in the grid? How complex is the data that you are serializing?
https://github.com/FasterXML/jackson-core
If the data structure is simple, then you can probably get away with just using Jackson.
When you want to begin serializing more complex data structures, then you might need to enhance your serialization engine.
I have written some stuff that can do this. Too much to explain here but you can check out these blog posts and see the code on git hub:
http://swingingcode.blogspot.com/2012/04/json-serialization-engine-part-i.html
http://swingingcode.blogspot.com/2012/04/json-serialization-engine-part-ii.html
https://github.com/cswing/evinceframework/tree/master/web/src/java/com/evinceframework/web/dojo/json

Are there data grid components in JSF/PrimeFaces?

I need an iterating component like a <h:datatable> which generates <div> elements on every iteration and adds pagination. Something like an <ui:repeat> with pagination.
Does it exist in PrimeFaces or any other library?
I think the p:dataTable element in PrimeFaces does what you want. It draws a pagination panel with first/prior/next/last control semantics. Here is the example for pagination.
What I recommend most is their "lazy data table loading" mode where you do not transfer any data from the server to the client that is not actually going to be displayed. In this way you can handle a data source with millions of records. Here is the example for lazy data loading.
If you want a grid and not a table, I think the PrimeFaces p:dataGrid implementation is the best in the business. Unfortunately my own application doesn't use it, but the example is here.

JQGrid with xml file as data source

How can I edit a xml file using JQGrid? I am using ASP.Net MVC 3.5 with C#. Is it possible to return a JSon object from controller method for this purpose?
Everything is possible. But the most work which you have to do is independ from the jqGrid. Moreover general XML file can be not good represented in a grid. If the deepness of the XML file not so long, you can use subgrid feature of the jqGrid. One more restriction existing in the jqGrid can make the work more comple: jqGrid not support data with attributs other as ids. So if you will have to map attributes to the sub-elements on the server side to be able to use jqGrid.
The ASP.NET MVC site can for example read the XML file with respect of some desirialization methods and initialize the object with the same information as the XML has. Then can use the object to fill the jqGrid and modify the data. Because th data are no mo represented as a XML, the server can use JSON to communicate with the jqGrid. At the end one can use one from wel known serialization methods so save modified data in the XML file back.

Binding to multple datasources in Silverlight with String.Format

I am trying to bind a label 2 (or more!) fields in a dataset in Silverlight 4. I get a localized string out of a resource file and do a String.Format on it like so:
<TextBlock Name="lblTotals" Text="{Binding TotalItems, StringFormat='You need \{0\} items and \{1\} products.'}" />
This works fine with 1 item but there's no way of doing multiple binds in SL4 it seems.
I found some blog posts on how to bind a single element to multiple fields but it does not seem to support the String.Format part which is critical.
The last caveat is that it is bound to an ObservableCollection, so when these fields change in the data the UI must update too.
Any suggestions? Thanks!
I found a solution here using a converter and binding to the whole object and passing in the string as a converter param.
Then the totals did not update when the grid values updated (despite being linked to OnPropertyChanged) - this was the solution hack here.

ASP.Net mvc building an xml data feed for a chart

I have to build an xml output that represents a data structured for a flex chart placed in the view.
I have several options:
have the controller create the xml (using data from the DB), and return it to a view that actually does nothing, since everything is ready.
have the view strongly typed to the data model from the DB, and render the xml declaratively in the view.
create an Html extension method that will contain the logic to create the xml and use it on the view.
in terms of separation of concern, what would be the best option?
in the future I don't expect many changes to the xml structure, maybe now and then.
I tend to select option 1 as it is more testable, and I feel more comfortable with the controller preparing the xml data.
I'd go for option number 1, as I feel it fits the MVC pattern the best. It's not the responsiblity of the View to create an XML file based on some data model. That's business logic and is therefor better off in the controller.
And equally important like you say, if you have your controller create the xml file you can create a unit test for it that asserts that the output xml is valid, contains all necessary nodes, etcetera.
Option 2 is the best. Your model has the data, your controller asks for it and offers it to the view. The view just has a tag to say where it goes. That to me is separation of concern.
Seeing Razzie's answer, I liked 1 as well, and I suppose that the model would have to provide some method for serializing some entity class (your chart results) into xml, in order for your strongly typed view to be able to make use of it.
Anyway I hope the answer helps, basically I don't think 3 is very good. :-)
I would say it depends on what you are more comfortable with as both options 1 and 2 are viable. People will say that option 1 is good as you can use an XmlWriter to make sure you've got valid xml to be returned and people will say option 2 is valid as mvc is all about having complete control on what is rendered in your view (which can be xml).
However, I would personally go with a variation on option 1 to keep the functionality independent of the controller and have it as a standalone utility method which takes in the data and outputs the xml. This will be easier to test but also be available to be called from other places in your code if this is needed in the future. In addition to this it also keeps the code in your controller cleaner.
And I agree with Mark I don't think option 3 is a good way to go.
That's just my thoughts, hope this helps :-)

Resources