1

I am having a SharePoint 2010 custom list with three columns EmployeeName, Districts and Created. I am showing the data of this list in a page using JQuery data table. I have a textbox and dropdown box upon selecting the dropdown value I should show the data from list in the page. If I enter the text in textbox which is employee name it should populate data of that in table from list.I got it working for entering employee name in textbox and pulling data but I am unable to figure how to work with dropdown here. please find my attached code.

function Init() {
  _clientContext = new SP.ClientContext.get_current();
  _web = _clientContext.get_web();

  $("#btnGetResults").click(function () {
      EmployeeDataSub = [];
       Name = $('#txtEmp').val();
       DisDep = $('#txtDisDep').val();
       fromDate = $('#EmployeefrmDate').val();
       GetReports(Name, fromDate);

  });
}

$(document).ready(function () {

  $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(Init, "sp.js"); });

});

function GetReports(empName, fromDate, EmployeeBy) {
  var listEmployee = _web.get_lists().getByTitle("Shout outs");
  var camlQuery = new SP.CamlQuery();
  //camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name="ShoutOutTo" /><Value Type="User">' + empName + '</Value></Eq><Eq><FieldRef Name="Created" /><Value IncludeTimeValue="FALSE" Type="DateTime">' + fromDate + '</Value></Eq></And></Where><OrderBy><FieldRef Name="Modified" Ascending="True" /></OrderBy></Query></View>');
  //camlQuery.set_viewXml('<View><Query><Where><Neq><FieldRef Name="ID" /><Value Type="Number">0</Value></Neq></Where><OrderBy><FieldRef Name="Title"/></OrderBy></Query></View>');
  camlQuery.set_viewXml('<View><Query><Where><And><Neq><FieldRef Name="ID" /><Value Type="Number">0</Value></Neq><Eq><FieldRef Name="_ModerationStatus" /><Value Type="ModStat">0</Value></Eq></And></Where><OrderBy><FieldRef Name="_ModerationStatus" Ascending="True" /></OrderBy></Query></View>');
  //camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="_ModerationStatus" /><Value Type="ModStat">0</Value></Eq></And></Where><OrderBy><FieldRef Name="_ModerationStatus" Ascending="True" /></OrderBy></Query></View>');
  //camlQuery.set_viewXml('<View><Query><Where><And><And><Neq><FieldRef Name="ID" /><Value Type="Number">0</Value></Neq><Eq><FieldRef Name="_ModerationStatus" /><Value Type="ModStat">0</Value></Eq><FieldRef Name="Author", LookupId="TRUE" /><Value Type="Integer">' + EmployeeBy +'</Value></And></And></Where><OrderBy><FieldRef Name="_ModerationStatus" Ascending="True" /></OrderBy></Query></View>');
  this.EmployeeListItemCol = listEmployee.getItems(camlQuery);
  _clientContext.load(EmployeeListItemCol);
  _clientContext.executeQueryAsync(Function.createDelegate(this, this.onGetReportsSuccess), Function.createDelegate(this, this.onGetReportsFailed));

}

function onGetReportsSuccess() {

  try {
      var empName;
      //var appstatus = "";
      //var mod;
      var enumerator = EmployeeListItemCol.getEnumerator();
      while (enumerator.moveNext()) {
        var listItem = enumerator.get_current();
        empName = listItem.get_item("ShoutOutTo").get_lookupValue();
        //var action = listItem.get_item("_ModerationStatus");
        var Title = listItem.get_item("Title");
        var createdOn = listItem.get_item("Created");
        var distDeput = listItem.get_item("DistrictDeputate");
        //action = "_ModStat";
        var appStatus = ["Approved", "Denied", "Pending", "Draft", "Scheduled"];
        var modStatAsNumber = 0;
        var modStatAsText = appStatus[modStatAsNumber];
        var EmployeeBy = listItem.get_item("Author").get_lookupValue();
        var anony = ["Anonymous", "No"];
        var anonynumb = 0;
        var anonyasText = anony[anonynumb];
        var anonymous = listItem.get_item("AnonymousCheck");
        //if (anonymous === 1) {
        //EmployeeBy = "Anonymous";
        //}
        //set approval status to Approved (0)
        //ListItem.set_item('_ModerationStatus', 0);



        if (empName.toUpperCase().indexOf(Name.toUpperCase()) > -1) {
            var newItem = {};
            newItem["Employee"] = empName;
            if (anonymous == 1) {
                newItem["Author"] = anonyasText;
            }
            else {
                newItem["Author"] = EmployeeBy;
            }
            newItem["Title"] = "<a href='https://examplesite.com/sites/RECOGNITION/Employee/Pages/Employeedetail.aspx?itmId=" + listItem.get_id() + "'>" + Title + "</a>";
            //newItem["Title"] = Title;
            newItem["CreatedOn"] = createdOn;
            newItem["District/Deputate"] = distDeput;
            //newItem["_ModerationStatus"] = modStatAsText;
            EmployeeDataSub.push(newItem);
        }
        else {
            if (distDeput === DisDep) {
                var newItem = {};
                newItem["Employee"] = empName;
                if (anonymous == 1) {
                    newItem["Author"] = anonyasText;
                }
                else {
                    newItem["Author"] = EmployeeBy;
                }
                newItem["Title"] = "<a href='https://examplesite.com/sites/RECOGNITION/Employee/Pages/Employeedetail.aspx?itmId=" + listItem.get_id() + "'>" + Title + "</a>";
                //newItem["Title"] = Title;
                newItem["CreatedOn"] = createdOn;
                newItem["District/Deputate"] = distDeput;
                //newItem["_ModerationStatus"] = modStatAsText;
                EmployeeDataSub.push(newItem);
            }

        }
     }
     $("#tblEmployeeReport").empty();
      var shootOut = $("#tblEmployeeReport").DataTable({
        "aaData": EmployeeDataSub,
        "bLengthChange": false,
        "bPaginate": false,
        "oLanguage": {
            "sEmptyTable": "No Items to show",
            "sInfoEmpty": ""
        },
        "sDom": '<"top">f<"bottom"><"clear">',
        "aoColumns":  [{ "sTitle": "Title", "mData": "Title", "sWidth": "10%", "render": function (data, type, row, meta) {
                        return '<a onclick="alert(' + data + ');" href="#">' + data + '</a>';
                    }
                    },  
                      { "sTitle": "District/Deputy", "mData": "District/Deputate", "sWidth": "10%" },           
                      { "sTitle": "Shootout To", "mData": "Employee", "sWidth": "10%" },
                      { "sTitle": "Employee By", "mData": "Author", "sWidth": "10%" },
                    { "sTitle": "Employee Date", "mData": "CreatedOn", "sWidth": "8%" }
                    ],
        "bJQueryUI": true,
        "bSort": true,
        "bAutoWidth": true,
        "iDisplayLength": 100,
        "sPaginationType": "full_numbers",
        "bStateSave": true,
        "bDestroy": true
    });

  }
  catch (err) {
      alert(err);
  }
}

function onGetReportsFailed(sender, args) {
  alert("Failed to load results..");

}

Your Answer

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

Browse other questions tagged or ask your own question.