Git Product home page Git Product logo

datatables.mvc's Introduction

Datatables.Mvc

ASP.NET MVC 5 classes to bind DataTables (1.10 and above with the new camelCase API) with your controllers.

But why another project for this?

First, it's not just another project. This one provides binding for ASP.NET MVC with DataTables 1.10.

Second, it's all about the new DataTables API (camelCase).
So far, DataTables was using the Hungarian Notation and that was, for me, a major pain. It all comes down to a developer's choice.

Finally, there was no binder, so far, for this scenario. Of course we could simply get the request parameters manually (after all, that's what the DataTables.Mvc is doing, right?) but this should help you avoid type-casting parameters, detecting, filtering and more. Also, this should help your focus on business rules instead of HTTP parameter checking.

What about demo?

It's as simple as it gets:

public ActionResult MyActionResult([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
{
    // do your stuff...
	var paged = myFilteredData.Skip(requestModel.Start).Take(requestModel.Length);
    return View(new DataTablesResponse(requestModel.Draw, paged, myFilteredData.Count(), myOriginalDataSet.Count()));
}

// Or if you'd like to return a JsonResult, try this:

public JsonResult MyActionResult([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
{
    // do your stuff...
	var paged = myFilteredData.Skip(requestModel.Start).Take(requestModel.Length);
	return Json(new DataTablesResponse(requestModel.Draw, paged, myFilteredData.Count(), myOriginalDataSet.Count()));
}

What about filtering/ordering?

It's a no brainer too.

Filter/sort info from each column is, well, included on each column.

To help you out, there are two methods on ColumnCollection:
IDataTablesRequest.Columns.GetSortedColumns() will return an ordered enumeration of sorted columns.
IDataTablesRequest.Columns.GetFilteredColumns() will return an enumeration of columns which were actually filtered on client-side.

Sample:

```C# // Apply filter to your dataset based only on the columns that actually have a search value. var filteredColumns = requestParameters.Columns.GetFilteredColumns(); foreach(var column in filteredColumns) Filter(column.Data, column.Search.Value, column.Search.IsRegexValue);

// Set your dataset on the same order as requested from client-side either directly on your SQL code or easily // into any type or enumeration. var sortedColumns = requestParameters.Columns.GetSortedColumns(); var isSorted = false; foreach(var column in sortedColumns) { if (!isSorted) { Sort(column.Data, column.SortDirection); isSorted = true; } else { SortAgain(column.Data, column.SortDirection); } }

<h3>Is it possible to add custom parameters sent with my request?</h3>
<p>
	Sure! It's a piece of cake now. Override <code>BindModel</code> and make it call <code>Bind</code> with your custom implementation of <code>IDataTablesRequest</code>.<br />
	Than, override the <code>MapAditionalProperties</code> to map your extra info into your custom type.<br />
	Here's a sample:
</p>
```C#
// Create a custom type from DataTablesBinder.
public class MyBinder : DataTablesBinder
{
    // Override the default BindModel called by ASP.NET and make it call Bind passing the type of your
    // implementation of IDataTablesRequest:
    public override object BindModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
    {
        return Bind(controllerContext, bindingContext, typeof(MyCustomRequest));
    }
	
	// Override MapAditionalProperties so you can set your aditional data into the model:
    protected override void MapAditionalColumns(IDataTablesRequest requestModel, System.Collections.Specialized.NameValueCollection requestParameters)
    {
            var myModel = (MyCustomRequest)requestModel;
            myModel.MyCustomProp = Get<string>(requestParameters, "myCustomProp");
    }
}

// You'll need a custom request model, of course.
// Just derive from DefaultDataTablesRequest and you're fine :)
// You can choose to implement IDataTablesRequest too, if you like.
public class MyCustomRequest : DefaultDataTablesRequest
{
    public string MyCustomProp { get; set; }
}

// Than, on your controller/action, decorate with:
public ActionResult MyActionResult([ModelBinder(typeof(MyBinder))] MyCustomRequest requestModel)

Any issues?

If you do find any issues, please, submit then and I'll fix it ASAP.

datatables.mvc's People

Contributors

almma avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.