mercredi 15 juin 2016

jQuery Datatable make each cell a link

I have four entities, let's say A,B,C,D which are interconnected (B depends on A, C depends on B, D depends on C). I want to display all the information in one table that can be easily searched and filtered.

So I created a view model of form:

public class MyViewModel {
    public Aname {get; set;}
    public Alink {get; set;}
    public Bname {get; set;}
    public Blink {get; set;}
    public Cname {get; set;}
    public Dname {get; set;}
    public Dlink {get; set;}
}

I want the table to have four columns to display the name of each entity and each data in a cell to be a hyperlink that leads to the details page of the selected entity (except entity C).

Here is the javascript

$('#myDataTable').DataTable({
    'bDestroy': true,
    "bInfo": true,
    "bProcessing": true,
    "bDeferRender": true,
    'iDisplayLength': 10,
    'sPaginationType': 'full_numbers',
    'sPageButtonActive': "paginate_active",
    'sPageButtonStaticDisabled': "paginate_button",
    'data': OptionsHandler.Data,
    "columnDefs": [
        {
            "render": function (data, type, row) {
                return "<a href=" + row[1] + "'>" + row[0] + "</a>";
            },
        },
    ]
});

But it complains that

Requested unknown parameter 0 for row 0. For more information about this error, please see http://datatables.net/tn/4

Data is in Json format:

data = [
       {"Aname":"PatriceBoyle",
        "Alink":"/A/Details/00014",
        "Bname":"Software Engineering",
        "Blink":"/B/Details/2",
        "Cname":"info",
        "Dname":"Database Design",
        "Dlink":"/D/Details/1"
       }, etc.]

How can I say: return "<a href=" + link + "'>" + name + "</a>"; for each cell?

Aucun commentaire:

Enregistrer un commentaire