Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I found this great tutorial to use REST calls to retrieve SharePoint list items as an array of JSON objects posted by Mark.Rackley (Mark Rackley.). It is working, but I would like to add a new functionality, but I don't get it to work.

function LoadAIP(state)
{
var call = $.ajax({
url: "https://xxxx/xxxx/_vti_bin/listdata.svc/AIPList?  
$select=ROCode,AIP,PIC&$filter=(ROCode eq '"+state+"')&$top=5000",

type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});

call.done(function (data,textStatus, jqXHR){
$('#example').dataTable( {
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "aaData": data.d.results,
"aoColumns": [
{ "mData": "ROCode" },
{ "mData": "AIP" },
{ "mData": "PIC" }],"bRetrieve": true,

    "sDom": '<"H"Tfr>t<"F"ip>',
    "oTableTools": {
    "aButtons": [ "xls"], 
"sSwfPath": ../js/datatables/TableTools/media/swf/copy_csv_xls_pdf.swf",
    "bFilter": true}
} );
  });
call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving Tasks: " + jqXHR.responseText);
});
}

They way this is set up, initially the table is empty, how can I change this to show all the records by default when the page is loaded, and then apply the filters? Thanks!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Here's something that will get you started.

function GetKeywordFromUrl() {
    var hashValue = window.location.hash;
    if (hashValue) {
        var k = hashValue.split('#k=')[1];
        if (k)
            return decodeURIComponent(k.replace(/\+/g, " "));

        //For executing keyword/filter search from a textbox
        var txtSearch = $("#your-textbox-id-here").val();
        if (txtSearch)
            return txtSearch.replace(/[^\w\s]/gi, '');
    }
    return null;
}


$(document).ready(function () {
    var resultsDiv = $('#ResultsDiv');
    resultsDiv.fadeOut('slow').delay(500).fadeIn();

    ExecuteOrDelayUntilScriptLoaded(LoadAIP, "sp.js");
    // For 2013 publishing pages use
    // SP.SOD.executeFunc('sp.js', 'SP.ClientContext', ExecuteListSearch);

    // To-Do: 
    // Change the hash value (k=) in the url, passing in the textbox value
    // Using hash value will allow to use Browser's back button to get back to previous results' page.

    $(window).bind('hashchange', function () {
        phoneBookDiv.fadeOut('slow').delay(400).fadeIn();
        //Re-execute search
        LoadAIP();
    });
});

function LoadAIP() {
  var state = GetKeywordFromUrl();
  var query = "/listdata.svc/AIPList?$select=ROCode,AIP,PIC&$top=5000";
  if(state)
    query = "/listdata.svc/AIPList?$select=ROCode,AIP,PIC&$filter=(ROCode eq '"+state+"')&$top=5000";

var call = $.ajax({
    url: _spPageContextInfo.webServerRelativeUrl + query,
    type: "GET",
    dataType: "json",
    headers: {
      Accept: "application/json;odata=verbose"
    }
  });

  call.done(function (data,textStatus, jqXHR){
    $('#example').dataTable({
        "bJQueryUI": true,
        "bDestroy": true, //Needed, to destroy and re-create table when filter/query changes
        "sPaginationType": "full_numbers",
        "aaData": data.d.results,
        "aoColumns": [
            { "mData": "ROCode" },
            { "mData": "AIP" },
            { "mData": "PIC" }
          ],
        "oLanguage": {
                "sLoadingRecords": "Loading...",
                "sProcessing": "Loading...",
                "sZeroRecords": "No records found,
                "sSearch": "<span>Filter within results:</span> _INPUT_", //Filter 
                "sInfoEmpty": "",
                "oPaginate": {
                    "sNext": "next",
                    "sLast": "last",
                    "sFirst": "first",
                    "sPrevious": "previous"
                }
            }
      });
  });

  call.fail(function (jqXHR,textStatus,errorThrown){
    alert("Error retrieving Tasks: " + jqXHR.responseText);
  });
}
share|improve this answer
    
Thanks, it is a good lead. –  cubanGuy Apr 11 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.