Next and previous item by ID

Home Forums Classic DFFS Next and previous item by ID

Viewing 8 reply threads
  • Author
    Posts
    • #11751
      Zaruba.Ivan
      Participant

        Hello Alexander,
        is it possible to add buttons for next and previous items from the list? Could be sorted by ID. I would like it for dispform and editform.

        Thank You
        Ivan

      • #11801
        Alexander Bautz
        Keymaster

          Hi,
          This code will work if you don’t have any holes in your number series (no deleted list items). Add this to a HTML section in DFFS:

          <input type="button" onclick="traverseListElements(false)" value="Previous item" /><input type="button" onclick="traverseListElements(true)" value="Next item" />

          And put this in the Custom JS:

          function traverseListElements(up){
              var id = Number(GetUrlKeyValue("ID")), url;
              if(up){
                  url = location.pathname+"?ID="+(id+1);
              }else{
                  url= location.pathname+"?ID="+(id-1);      
              }
              if(GetUrlKeyValue("IsDlg") === "1"){
                 url += "&IsDlg=1";
              }
              location.href = url;
          }

          Alexander

        • #11807
          Zaruba.Ivan
          Participant

            Hi Alexander,

            excellent work, that’s what I (and users of course) dreamed about.

            Thank you!

            Ivan

          • #20479
            Brandon W Green
            Participant

              Hate to resurrect an old thread but it is applicable to my request.

              Can this same logic be applied to a specific view rather than by ID?

            • #20485
              Alexander Bautz
              Keymaster

                Not sure what you mean by view – are you in a form enhanced with DFFS, or in a list view?

                Alexander

              • #20518
                Brandon W Green
                Participant

                  Display form enhanced by DFFS that is being accessed from a specific list view. This view only display list items based on specific column data.

                  Remedial Example: Status field with Open, Pending, Completed

                  Display form is opened from a list view of items where Status = Open

                  Next / Previous button will navigate through those items *only* rather than by ID increment.

                • #20591
                  Alexander Bautz
                  Keymaster

                    Sorry for the late reply. You can do it like this:

                    var arrOfIDsInView = [];
                    var res = spjs.utility.queryItems({ "listName": _spPageContextInfo.pageListId, "viewName": "{8E016900-8594-45D0-8837-4AFD952C3375}", "viewFields": ["ID"] });
                    jQuery.each(res.items, function (i, item) {
                        arrOfIDsInView.push(item.ID);
                    });
                    
                    function traverseListElements(up) {
                        var id = GetUrlKeyValue("ID"), newId, url;
                        if (up) {
                            newId = arrOfIDsInView[jQuery.inArray(id, arrOfIDsInView) + 1];
                        } else {
                            newId = arrOfIDsInView[jQuery.inArray(id, arrOfIDsInView) - 1];
                        }
                        if(newId !== undefined){
                            url = location.pathname + "?ID=" + newId;
                            if (GetUrlKeyValue("IsDlg") === "1") {
                                url += "&IsDlg=1";
                            }
                            location.href = url;
                        }else{
                            alert("No item found");
                        }
                    }

                    You must replace “{8E016900-8594-45D0-8837-4AFD952C3375}” with the view GUID of your list view. To find it, you can bring up the dev console (hit F12) when you look at the list view, and then type in this:

                    ctx.view

                    Alexander

                    • This reply was modified 6 years, 8 months ago by Alexander Bautz. Reason: fixed code snippet
                  • #20628
                    Zaruba.Ivan
                    Participant

                      Hi,
                      I have a lot of document libraries, and I need to traverse documents and edit custom fields in EditForms. Can I get GUID of the current list view as variable?

                      var res = spjs.utility.queryItems({ "listName": _spPageContextInfo.pageListId, "viewName": "{8E016900-8594-45D0-8837-4AFD952C3375}", "viewFields": ["ID"] });

                      Thank You.

                      Ivan

                    • #20647
                      Alexander Bautz
                      Keymaster

                        Yes, use ctx.view like this:

                        var res = spjs.utility.queryItems({ "listName": _spPageContextInfo.pageListId, "viewName": ctx.view, "viewFields": ["ID"] });

                        Alexander

                    Viewing 8 reply threads
                    • You must be logged in to reply to this topic.