Fitler/Query Question

Fitler/Query Question

zgoforthzgoforth Posts: 493Questions: 98Answers: 2

Link to test case:

So I have been tasked with an oddly specific piece I need to add to the project I have been working on. I have a script running that when a new row is added to my DataTable (task list), it emails the user that the task is assigned to via a SP workflow. The table already filters to only show the current users tasks (active/inactive) on two separate tables, a current and complete table. In the email it includes a link to the current task table, but the table could show more than one current task. I want to send a link to the user that only shows the task they were just assigned. Is this possible via the custom search plugin? Or would I need something like a SharePoint url query/filter.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Or would I need something like a SharePoint url query/filter.

    This. You need to specify in the url what items you would like to show in the table. You would then read that parameter and apply a search to the table that would reduce the result set to just that item.

    Allan

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    @allan thanks!

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    UPDATE:

    So I am passing a custom parameter into the URL that is getting emailed to the assignee, and I am getting the value like so:

    const paramId = new URLSearchParams(window.location.search);
    paramId.get('MyCustomParameter');
    
    console.log(paramId.get('MyCustomParameter'));
    new Map(new URLSearchParams(location.search).entries());
    

    This is at the top of my document.ready function, then I have the following search/filter function below where it shows the all the users items, but I want to customize it to where when they click the link sent with the URL param, it only shows that list item with the same ID (which I have as a hidden column in the table). But when the user visits the link that doesn't contain the param, still shows all of their tasks instead of the one where the item id matches :

    var thisUserTitle = $().SPServices.SPGetCurrentUser({ 
        fieldName: "Title",
        debug: false
    });
    console.log(thisUserTitle)
    $.fn.dataTable.ext.search.push( 
        function(settings, searchData, index, rowData) {
            var tableUserTitle = searchData[0]; 
            var api = $.fn.dataTable.Api('#taskTable'); 
            if (tableUserTitle == thisUserTitle) { 
                return true;
            }
            
            return false; 
        }
    );
    

    To achieve what I mentioned above would I need a nested condition? something like:

    $.fn.dataTable.ext.search.push( 
        function(settings, searchData, index, rowData) {
            var tableUserTitle = searchData[0];
            var itemID = searchData[7];
            var api = $.fn.dataTable.Api('#taskTable'); 
            if (tableUserTitle == thisUserTitle) { 
                if(itemID == paramId.get('MyCustomParameter')){
                    return true;
                }
              return true;
            }
            
            return false; 
        }
    );
    
  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    I don't understand your requirement but you might want to change lines 7-9 to this:

                if(itemID != paramId.get('MyCustomParameter')){
                    return false;
                }
    

    Otherwise the if block in line 6 will always return true if tableUserTitle == thisUserTitle.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    So the requirement is:

    When the user gets the original email linking to the page, it will only show the table item which the itemID is the same as the paramId.get('MyCustomParameter').

    If the itemID != the paramId.get('MyCustomParameter'), then I want it to show all of the items that are true when tableUserTitle == thisUserTitle

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

    Something like this?

    // user gets the original email linking to the page, it will only show the table item which the itemID is the same as the paramId.get('MyCustomParameter')
    
    if ( itemID === paramId.get('MyCustomParameter') ) {
      return true;
    }
    
    // If the itemID != the paramId.get('MyCustomParameter'), then I want it to show all of the items that are true when tableUserTitle == thisUserTitle
    
    if ( itemID != the paramId.get('MyCustomParameter') && tableUserTitle == thisUserTitle ) {
      return true;
    }
    
    return false;
    

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    That is what I want to be the case. But it is showing all of the list items. I console

    let queryString = paramId.get('MyCustomParameter');
    console.log(queryString); //58
    

    After I created a new item, I console log the searchData[7] as well, and get the same result as queryString, but all results show. Not as intended... I create a new one with 59 as the item ID and query string.

    It is still showing the item with 58 as the ID when I click on the 59 link. If I cross out the other condition and have only this,

                if (itemID == queryString) {
                    return true;
                }
                // if(itemID != queryString && tableUserTitle == thisUserTitle) {
                //     return true;
                // }
    

    it filters the table as itended

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

    Glad you got it working.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    I didn't get it working. It only works with one condition? I am trying to recreate the issue in a fiddle to show you. I have two conditions that work at the same time, but when I remove the first one checking if user is a task assigner. the second and third which I included in my OP do not work together. It is either one or the other.... Kind of frustrating

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

    Nevermind, I fixed it, trying to upload the example now

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    Here is my minimal reproducible example, as you can see it shows both table items when it should show one https://jsfiddle.net/BeerusDev/ku4hcwfp/53/

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

    You have this:

          if (itemID == queryString) {
            return true;
          }
                if(itemID != queryString && tableUserTitle == thisUserTitle){
          return true;
          }
    

    Purely from a logic stand point the temID != queryString && is not needed. It won't get to this point unless temID != queryString is true because of the previous if statement. Remove it to help save processing cycles.

    In your test case tableUserTitle == thisUserTitle will always be true as both rows have Beerus Dev. Sorry, but I still don't understand the requirement.

    You have queryString and thisUserTitle which you want to use to filter the table. You have itemID and tableUserTitle columns in the table. Write down the rules of which rows will be displayed or removed based on comparing these values. Then write the if statements to match.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    I will keep attempting. But I want the itemID == queryString to take the higher precedence over the tableUserTitle == thisUserTitle. Because if the itemID == queryString, I only want that matching item to show. It is pulling the substring from a URL parameter from a new task item created, and when emailed to the user. I only want them to see the one just created. But at any other time when they visit the page without the parameter, it shows all of their different current tasks. Sorry, I am trying to be as specific as possible.

    Side note, in my actual application I have two tables. Is it possible to apply a different filter to each table id? Because right now it is applying the same filter to both

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    Answer ✓

    Sounds like you need to check for the existence of the URL parameter in the search plugin. If it exists then check itemID == queryString. If not then check tableUserTitle == thisUserTitle.

    Is it possible to apply a different filter to each table id

    See this thread.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    @kthorngren My brain doesn't like to function at max capacity this early in the morning. I don't know why I couldn't figure this out earlier. I was looking at the issue wrong.. So I have the following getting my queryString

    const paramId = new URLSearchParams(window.location.search);
    paramId.get('MyCustomParameter');
    
    let queryString = paramId.get('MyCustomParameter');
    

    if there is no parameter, then queryString is equal to null. So with the following code, the filter works as expected.

    if (itemID == queryString) {
            return true;
          }
          if (queryString === null && tableUserTitle == thisUserTitle){
            return true;
          }
          return false; 
    
Sign In or Register to comment.