ALMMa / datatables.aspnet

Microsoft AspNet bindings and automatic parsing for jQuery DataTables along with extension methods to help on data queries.
MIT License
301 stars 136 forks source link

Add support for column.data as an object to use different data for the different data types requested by DataTables #59

Open kmd1970 opened 7 years ago

kmd1970 commented 7 years ago

Currently there doesn't seem to be support for the following DataTables feature:

Description:

Use different data for the different data types requested by DataTables (filter, display, type or sort). The property names of the object is the data type the property refers to and the value can defined using an integer, string or function using the same rules as columns.data normally does.

Note that an _ option must be specified. This is the default value to use if you haven't specified a value for the data type requested by DataTables.

As an example you might use:

$('#example').dataTable( {
serverSide: true,
ajax: "/home/pagedata",
"columns": [
 { data : "id" },
 { data : { "_": "phone", "filter": "phone_filter", "display": "phone_display", "sort" : "phone_sort" } }
]
});
kmd1970 commented 7 years ago

I was able to implement this myself 🥇

DataTables.AspNet.Core\NameConvention\IRequestNameConvention.cs

//added
 string ColumnField_ { get; }
 string ColumnFieldSort { get; }

DataTables.AspNet.Mvc5\NameConvention\HungarianNotationRequestNameConvention.cs

//added, but these need to be corrected later
public string ColumnField_ { get { return "mDataProp_{0}"; } }
public string ColumnFieldDisplay { get { return "mDataProp_{0}"; } }
public string ColumnFieldSort { get { return "mDataProp_{0}"; } }

DataTables.AspNet.Mvc5\NameConvention\CamelCaseRequestNameConvention.cs

//added
public string ColumnField_ { get { return "columns[{0}][data][_]"; } }
public string ColumnFieldSort { get { return "columns[{0}][data][sort]"; } }
public string ColumnFieldDisplay { get { return "columns[{0}][data][display]"; } }

DataTables.AspNet.Mvc5\Column.cs

public class Column : IColumn
{
public string Field { get; private set; }
public string FieldSort { get; private set; }
public string Name { get; private set; }
public ISearch Search { get; private set; }
public bool IsSearchable { get; private set; }
public ISort Sort { get; private set; }
public bool IsSortable { get; private set; }

public Column(string name, string field, string fieldSort, bool searchable, bool sortable, ISearch search)
{
    Name = name;
    Field = field;
    FieldSort = fieldSort; 
    IsSortable = sortable;

    IsSearchable = searchable;
    if (!IsSearchable) Search = null;
    else Search = search ?? new Search(field);
}

public bool SetSort(int order, string direction)
{
    if (!IsSortable) return false;

    if (FieldSort == null)
    {
      Sort = new Sort(Field, order, direction);
    } else {
      Sort = new Sort(FieldSort, order, direction);
    }

    return true;
}

DataTables.AspNet.Mvc5\ModelBinder.cs

private static IEnumerable<IColumn> ParseColumns(IValueProvider values, IRequestNameConvention names)
{
var columns = new List<IColumn>();

int counter = 0;
while(true)
{
/// for column.data._ field value
var columnField_ = values.GetValue(String.Format(names.ColumnField_, counter));
string _columnField_ = null;
Parse<string>(columnField_, out _columnField_);

/// for column.data.sort value
var columnFieldSort = values.GetValue(String.Format(names.ColumnFieldSort, counter));
string _columnFieldSort = null;
Parse<string>(columnFieldSort, out _columnFieldSort);

/// Parses Field value.
var columnField = values.GetValue(String.Format(names.ColumnField, counter));
if (_columnField_ != null)
{
columnField = columnField_;
}
string _columnField = null;
if (!Parse<string>(columnField, out _columnField)) break;

/// line 178
/// added  extra _columnFieldSort to Column()
var column = new Column(_columnName, _columnField, _columnFieldSort , _columnSearchable, _columnSortable, search);

TO TEST

Add new columns to DataTables.AspNet.Samples.Mvc5.BasicIntegration\Models\SampleEntity.cs

public class SampleEntity
{
public int Id { get; set; }
///added new fields
public int Id_sort { get; set; }
public string Id_display { get; set; }
public string Name { get; set; }

public SampleEntity() { }
public SampleEntity(int id, string name)
{
    Id = id;
    Id_sort = id;
    // to verify proper column is used 
    Id_display = "# = " + id.ToString();
    Name = name;
}