Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

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

Open
kmd1970 opened this issue Jun 23, 2017 · 1 comment
Assignees
Labels
enhancement Describes an enhancement or an existing feature
Milestone

Comments

@kmd1970
Copy link

kmd1970 commented Jun 23, 2017

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
Copy link
Author

kmd1970 commented Jun 26, 2017

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;
}

@ALMMa ALMMa self-assigned this Jun 17, 2021
@ALMMa ALMMa added the enhancement Describes an enhancement or an existing feature label Jun 17, 2021
@ALMMa ALMMa added this to the 3.0.0 milestone Jun 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Describes an enhancement or an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants