Custom Filter Question

Custom Filter Question

zgoforthzgoforth Posts: 493Questions: 98Answers: 2

No test case yet, but I wanted to see if this was even possible.

https://datatables.net/manual/plug-ins/search

I want to make a custom filter, where I get the current user of SharePoint's ID/Title/Name and only display values in the DataTable that include their name as I am using a SharePoint person field to populate who each task is assigns a hidden user ID column to the table. If I can get that current user's id, can I apply it to the filter linked above?

if (currentUserId === AssignedTo.results[0].Id){
   return true;
}
return false;

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    There is nothing special about the search plugin. Its just a Javascript function the is called for each row. If you have the values you can use them in the function like any other function.

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    couldn't you add it to the server side in a where statement?

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    @montoyam unsure never done that?

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2
    edited August 2021

    @kthorngren so I did the following:

    var thisUserID = $().SPServices.SPGetCurrentUser({
        fieldName: "Id",
        debug: false
        });
    
        console.log(thisUserID); //shows 32
        $.fn.dataTable.ext.search.push(
            function(settings, searchData, index, rowData, counter, thisUserID) { 
                var tableUserId = searchData[8];
    
               console.log(tableUserId); //shows 32
                var api = $.fn.dataTable.Api('#taskTable'); 
                //var api = $.fn.dataTable.Api('#completedTaskTable'); 
    
                if(tableUserId === thisUserID) {
                    return true;
                }
                return false;
            }   
        );
    

    When I console thisUserID it is 32, when I check rowData[0] 32 is there but so is another ID of another user and I do not want to see their data, just mine.

    But when I implement it, I cannot see any of the data as it is all filtered out? I have created a filter before to show the current week items only and it worked fine. Am I missing something here?

  • montoyammontoyam Posts: 568Questions: 136Answers: 5
  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    @montoyam I am not using php though

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    But when I implement it, I cannot see any of the data as it is all filtered out?

    That suggests your if statement in line 15 is always false so the return in line 18 is executed which hides the row.

    Am I missing something here?

    Hard to say without seeing the data. Maybe your are comparing a string to an numeric value which won't work with ===. You can try ==. Please build a simple example, ie, leave out the RowGroup and other stuff we aren't troubleshooting, with a same of your data so we can take a look.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    @kthorngren tried that, still says no No matching records found. Creating a test case as we speak.

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    Are you using Editor? How are you getting the SharePoint data?

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    @montoyam REST API

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2
    edited August 2021
  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited August 2021

    You added thisUserID as a parameter for the search plugin function:

        $.fn.dataTable.ext.search.push(
            function(settings, searchData, index, rowData, counter, thisUserID, thisUserTitle) { 
    ...
    

    This is causing the variable to be undefined. See this example:
    https://jsfiddle.net/5ykj82d0/

    Remove the extra parameters , thisUserID, thisUserTitle from the function and your example will work. For example:
    https://jsfiddle.net/5ykj82d0/1/

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    @kthorngren Oh my goodness, thank you. Not sure why I tried passing them through the params when they are already globally defined....

Sign In or Register to comment.