I am currently implementing a client-side paging solution using ASP.NET, jQuery and JSON.
I have been following the excellent article from encosia: http://encosia.com/2008/08/20/easily-build-powerful-client-side-ajax-paging-using-jquery/
In my Web Method I retrieve my data from the database as a DataTable:
DataTable categoryProducts = ProductViewerAccess.GetCategoryProducts
("AA", 4, 0, Page.ToString(), out howManyPages, "FALSE", 0, "CostPrice", "asc", destinationList);
I then retrieve the data from the DataTable into an anonymous type:
var feeds =
from feed in categoryProducts.AsEnumerable()
select new
{
Description = feed.Field<string>("description"),
MfPartNo = feed.Field<string>("MfPN"),
Inventory = feed.Field<Int32>("Inventory")
};
The anonymous type is then returned from the Web Method to the client-side:
return feeds.Take(PageSize);
A template then extracts and displays the fields:
<tbody>
{#foreach $T.d as post}
<tr>
<td>
{$T.post.Description}
<p>Mfr#: {$T.post.MfPartNo}</p>
</td>
<td>{$T.post.Inventory}</td>
</tr>
{#/for}
</tbody>
This all works great.
However, I would like to extend the code to perform some evaluation checks (e.g., check that various columns in the DataTable are not NULL) and other pre-processing (e.g., call various functions to build the image URL based on the image ID - which is another column in the DataTable not shown in the code fragment) before I return the resulting rows of the DataTable as an anonymous type to the client-side.
Basically, I want to iterate through the DataTable, perform the evaluation checks and pre-processing, while building my anonymous type manually as I go. Or maybe there is a better way to achieve this?
Is there anyway I can achieve this?
Kind Regards
Walter
I think that checking for nulls is probably something that makes sense on the server-side. The approach that Douglas describes is one workable one. Another is to handle the null issue as you're building the collection of anonymous types:
var feeds =
from feed in categoryProducts.AsEnumerable()
select new
{
Description = feed.Field<string>("description"),
MfPartNo = feed.Field<string>("MfPN"),
// Return 0 if the inventory value is null.
Inventory = (int?)feed.Field("Inventory") ?? 0
};
ScottGu has a good post on using the null coalescing operator to handle nulls concisely, as shown above.
As for building the links, that's something I would probably suggest doing in the client-side template. You can eliminate quite a bit of redundant data sent in the JSON that way. Something like this, for example:
<tbody>
{#foreach $T.d as post}
<tr>
<td>
<a href="/url/to/details.aspx?id={$T.post.ID}">{$T.post.Description}</a>
<p>Mfr#: {$T.post.MfPartNo}</p>
</td>
<td>{$T.post.Inventory}</td>
</tr>
{#/for}
</tbody>
Sometimes I find it helpful to use the "let" keyword using LINQ to store value I need to use later in the query. I can use this variable later for simple null checks or other things. For example:
var feeds =
from feed in categoryProducts.AsEnumerable()
let MfPN = feed.Field<string>("MfPN")
// Get image url if MfPN is not null, else return default image url.
let Url = !string.IsNullOrEmpty(MfPN) ? GetImgUrl(MfPN) : “DefaultImage.jpg”
select new
{
Description = feed.Field<string>("description"),
MfPartNo = MfPN,
Inventory = feed.Field<Int32>("Inventory"),
ImageUrl = Url
};
The only other thing I can think if is too simple perform your pre-processing on the DataTable before calling the LINQ Query.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With