What's the best data source for me to use?

What's the best data source for me to use?

BobSCABobSCA Posts: 3Questions: 0Answers: 0
edited May 2012 in General
Hello!
I just staring using the datatable and love it. The default sorting, paging and filtering has saved me countless hours. Currently I get my data on the page and dynamic build the HTML table and that works perfect. My test table has Project Name, Date, Contact Person and Status. Although the users could use the default filter box for these fields, there are a bunch of Status options which they would not know all the values to type into the default filter box. So I prefer to create a dropdown box with the status options in it from which they can pick. Once they select a status in this dropdown I want to reload the data so that I can continue to use the default paging, filtering and sorting. This will not be a large list of projects (< 200) so was not thinking I need server side processing. Is there a way to just rebuild the table in the DOM and have the table regenerate?

Thanks!

Replies

  • snarf2larfsnarf2larf Posts: 64Questions: 0Answers: 0
    If you put the hidden data in the table you should be able to filter it using:

    http://datatables.net/api#fnFilter
  • BobSCABobSCA Posts: 3Questions: 0Answers: 0
    Thank you but not exactly sure how to get access to the table from my dropdown change event. I tried just enter in as oTable.fnFilter but assuming it couldn't access oTable so then tried to grab a hold of the reference to the table using the code below. Here's that change event:
    // Status dropdown change.
    $("#status-select").change(function () {
    // We have a new filter so get the new data.
    alert('In the status-select change event');

    // Get the current status.
    var currentStatus = $('#status-select').val();

    // Get the table.
    $('.sortable').each(function (i) {
    // DataTable config
    var table = $(this),
    oTable = table.dataTable({
    oTable.fnFilter('Lead');
    });
    };
    });
  • BobSCABobSCA Posts: 3Questions: 0Answers: 0
    Figured it out with a lot of help from forum posts (thank you). I ended up switching over to load using ajax and then using the fnReloadAjax api. This required including the following function:

    // Customized fnReloadAJAX to allow for new source.
    $.fn.dataTableExt.oApi.fnReloadAjax = function (oSettings, sNewSource) {

    if (typeof sNewSource != 'undefined') {
    oSettings.sAjaxSource = sNewSource;
    }

    this.fnClearTable(this);
    this.oApi._fnProcessingDisplay(oSettings, true);

    $.getJSON(oSettings.sAjaxSource, null, $.proxy(function (json) {

    for (var i = 0; i < json.aaData.length; i++) {
    this.oApi._fnAddData(oSettings, json.aaData[i]);
    }

    oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
    this.fnDraw(this);
    this.oApi._fnProcessingDisplay(oSettings, false);
    }, this));
    }

    I then defined my table as:
    // Setup grid and use AJAX as the source.
    $('#OppTable').dataTable({
    "bProcessing": false,
    "sAjaxSource": '/home/ReloadGrid',
    "sDom": '<"block-controls"<"controls-buttons"p>>rti<"block-footer clearfix"lf>'
    });

    Then in my dropdown event handler I call the fnReloadAjax function with my URL and parameter of the selected status:
    $("#status-select").change(function () {
    // We have a new filter so get the new data.

    // Get the current status.
    var currentStatus = $('#status-select').val();
    $('#OppTable').dataTable().fnReloadAjax('/home/ReloadGrid/?FilterValue=' + currentStatus);
    });

    With this I can use the paging, filtering and sorting all on the client side.

    Thanks!
This discussion has been closed.