Getting native json data from api

Getting native json data from api

AMKDPAMKDP Posts: 2Questions: 1Answers: 0

Hello.

Consider I have this for datatable initialization:

listDataTable = $('#MembersTable').dataTable({
    "serverSide": true,
    "initComplete": initComplete,
    "ajax": {
        "url": "/Circular/JdtGetJson",
        "type": "POST"
    },
    "columns": [
    {
        "name": "Title",
        "data": "Title",
        "title": "Title",
        "individualColumnInfo": {
            "filterMode": 1,
            "useRemoteData": false,
            "filterList": ["94", "93"]
        }
    },
    {
        "name": "Action",
        "data": "Action",
        "orderable": false,
        "searchable": false,
        "title": "Actions",
        "width": "100px",
        "render": _dataTablesActionTemplate
    }],
});

Now I want to iterate over all columns in my table using api() :

listDataTable.api().columns().every(function (colIndx) {
    var that = this;
    
    //var individualColumnInfo = ????? this.column(1).individualColumnInfo ?????
});

I want to access individualColumnInfo in my source json.

How can I do this? Is it possible throgh api()?

Thank you.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin
    Answer ✓

    You could use api().settings()[0].aoColumns[ colIndx ].individualColumnInfo.

    That will work and will continue to work, but I would urge caution when using settings(). The properties in the settings object are considered to be private (e.g. aoColumns). They can, will and do change between releases. That one is unlikely to change in fairness, but we need to be careful.

    Having said that there is no public API to access properties like you want...

    Allan

  • AMKDPAMKDP Posts: 2Questions: 1Answers: 0
    edited December 2015

    Thank You for the answer.

    I've used dataTable.fnSettings().aoColumns[colIndx].individualColumnInfo
    I think your's is better.

    "there is no public API to access properties like you want" but why? I needed this, because I working on an lightweight - model bound datatables library on ASP.net MVC, and I was faced this problem when I wanted to write some -Individual column filtering- to provide a mechanism to users to have specific column filter or anything else on individual columns in their view-model, like this:

    public class MyViewModel : JdtViewModelBase{
        [JdtIndividualColumnFilter(FilterMode = JdtColumnFilterMode.Text, PlaceHolder = "Filter names...")]
        public string Name { get; set; }
    
        [JdtIndividualColumnFilter(FilterMode = JdtColumnFilterMode.List, FilterList = new object[] { new[] { "(No filtering)", "" }, new[] { "Year 2015", "2015" } , new[] { "Year 2014", "2014" } })]
        public string YearName { get; set; } // which is immediately-loaded as a option list.
    
        [JdtIndividualColumnFilter(FilterMode = JdtColumnFilterMode.List, UseRemoteData = true, RemoteDataUrl = "/OptionsController/GetOptions")]
        public string Options { get; set; } // which is lazy-loaded as a option list.
    }
    

    And its get rendered on view this way:

    @Html.JqueryDataTables("MembersTable", new JqueryDataTablesOptions
    {
        DeclareVaiable = "listDataTable",
        Processing = true,
        ServerSide = true,
        //Responsive = true,
        Info = true,
        PagingType = JqueryDataTablesPagingTypes.FullNumbers,
        PaginationType = "bootstrap",
        InitComplete = new JsFunctionListCall {
             new JdtIndividualColumnFiltererInitializer()
        },
        Ajax = new JqueryAjaxOptions
        {
            Url = Url.Action("JdtGetJson"),
            Type = "POST"
        },
        Columns = JqueryDataTablesColumnInfo.GetColumnsOf<MyViewModel>(),
        Language = new JqueryDataTablesFrenchLanguagePack()
    })
    

    I can rewrite this, and make JdtIndividualColumnFiltererInitializer to depend on JqueryDataTables which was parent object of it, but I think it may become so complicated to users to understand on first practices.

    I think its better to you to keep this way or provide a reliable api to access source json which sent by server for each column.

    Thank you

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    "there is no public API to access properties like you want" but why?

    Because it hasn't really been needed until now. I don't plan to change the method you have used - it will keep working with the 1.x releases of DataTables, and in all likelihood v2.x as well.

    It is something that will be useful - I can see that, and I will add it in future, but it doesn't exist yet (other than using the method you have used).

    Allan

This discussion has been closed.