Custom Filter Not Accepting One Condition

Custom Filter Not Accepting One Condition

zgoforthzgoforth Posts: 493Questions: 98Answers: 2

Link to test case: https://jsfiddle.net/BeerusDev/x054hvt3/53/

Hello,

So I am having this issue trying to add a condition to my custom table filter $.fn.dataTable.ext.search.push( ). I currently have one condition that works, where if the currentUserId is the same as the tableUserId, then it only shows that persons tasks. I have a condition outside of the table that determines if the current user is in the "Task Assigners" group, if they are it shows the assign a task button, if not it hides it. If the user is in that group, I want the table to show all items to them, but when I add the condition to the search.push. It still only follows the currentUserTitle condition and not both.

Here is the outside table filter:

    var assignButton = document.getElementById('button1');

    function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

    var currentContext = new SP.ClientContext.get_current();
    var currentWeb = currentContext.get_web();

    var currentUser = currentContext.get_web().get_currentUser(); //getting current user and groups for custom filtering of what people can see
    currentContext.load(currentUser);

    var allGroups = currentWeb.get_siteGroups();
    currentContext.load(allGroups);

    var group = allGroups.getByName(groupName);
    currentContext.load(group);

    var groupUsers = group.get_users();
    currentContext.load(groupUsers);

    currentContext.executeQueryAsync(OnSuccess,OnFailure);

    function OnSuccess(sender, args) { //success function for finding current user and group they are in.
        var userInGroup = false;
        var groupUserEnumerator = groupUsers.getEnumerator();
        while (groupUserEnumerator.moveNext()) {
            var groupUser = groupUserEnumerator.get_current();
            if (groupUser.get_id() == currentUser.get_id()) {
                userInGroup = true;
                break;
            }
        }  
        OnComplete(userInGroup);
    }

    function OnFailure(sender, args) {
        OnComplete(false);
    }    
    }

    function IsCurrentUserHasContribPerms() { //if current user is in group, show the assignButton (Task Assigner Group) to only those people.
        IsCurrentUserMemberOfGroup("TaskAssigners", function (isCurrentUserInGroup) {
            if(isCurrentUserInGroup)
            {
                assignButton.style.display = 'inline-block';
            } else {
                assignButton.style.display = 'none';

            }
        });

    }
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserHasContribPerms, 'SP.js');

The above works fine, then I try to carry it over to the search.push and nothing happens???

var thisUserTitle = $().SPServices.SPGetCurrentUser({ //get the current user Title field (AKA name field)
        fieldName: "Title",
        debug: false
    });
    
    $.fn.dataTable.ext.search.push( //custom filter to only show people 
        function(settings, searchData, index, rowData) {
            var tableUserTitle = searchData[0]; //getting the user name for each row of the datatable
            var api = $.fn.dataTable.Api('#taskTable'); //getting the table API
            //console.log(tableUserTitle);
            if (tableUserTitle == thisUserTitle) { //if the current user equals the same user in the task list row, then it only will show that users items to them so they cannot see others tasks
                return true;
            }
            if (isCurrentUserInGroup === "TaskAssigners") {
                return true;
            }
            return false; //if the current user doesn't equal the same user in the task list row, returns false boolean value and nothing shows in the table
        }
    );

Answers

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,953

    Are you saying this if statement is not working as expected?

                if (isCurrentUserInGroup === "TaskAssigners") {
                    return true;
                }
    

    Its not really clear what the problem is as it seems your test case is working. If your test case is showing the issue please provide details of what to look at.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    I will try my best to recreate the problem, but its hard because its specific to SP Environment

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,953

    Its even harder to debug code just by looking at some code snippets without the full context of what is going on :smile:

    My suggestion is to spend time debugging what is going on in the search plugin. Put breakpoints in the code statements you are interested so you can see what the values of the variables are. It doesn't sound like a Datatables issue.

    Kevin

Sign In or Register to comment.