1

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!

1 Answer 1

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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