我们如何在jQuery中使用USER ROLE?

时间:2017-12-11 14:32:53

标签: javascript php jquery

我正在使用下面的函数在popup中显示项目信息,工作正常。第二个函数用于仅删除该特定项目的文档文件。我想为具有角色"的用户禁用此功能。管理器"

function openShowProjectModal(id) {
    $.post("/get-project-info", {ID: id}, function (res) {
        var project = JSON.parse(res);
        $("#job-name-show").text(project.JobName);
        $("#job-number-show").text(project.JobNumber);
        $("#location-show").text(project.Location);

        var files = "";
        for (var i = 0; i < project.Docs.length; i++) {
            var filename =  project.Docs[i].Document;
            var id = project.Docs[i].ID;


            // I want to disable the Delete function or to hide this Trash icon.

            files += i+1 + ". " +('<a href="/lib/document/'+filename +'">'+filename+'</a>' ) +"   " + '<a href="#" onclick=deleteDoc('+ id +')><i  class="fa fa-trash-o"    aria-hidden="true"></i> </a> <br/>';

            $("#project-files").html(files);

        }

    });
}


function deleteDoc(id) {
    if(confirm("Are you sure to delete?")){
        $.post("/remove-doc",
        {
            ID :id
        }, function (res) {
            if (res == "removed") {
                window.location.href = '/projects';

            } else {
                console.log("not removed");
            }
        }); 
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道你在哪里存储角色。也许这样的事情会起作用:

function deleteDoc(id) {
if(user.role === "Manager") return;
  if(confirm("Are you sure to delete?")){
  $.post("/remove-doc",
   {
     ID :id
    }, function (res) {
     if (res == "removed") {
      window.location.href = '/projects';
     } else {
        console.log("not removed");
     }
 }); 
}