2016-06-08 62 views
-1

嗨我在這裏面臨一個小問題。這裏是所有的輸出。但是,如果我刪除它們也會從數據庫中刪除。我只想從html頁面關閉這個contnt,而不是從數據庫中刪除它。 我使用的是那個javascript。任何幫助對我都會有很大的幫助。關閉一個特定的內容,而不從數據庫中刪除

HTML

<html> 
    <head> 
     <meta charset="utf-8"> 
     <title>My Parse App</title> 
     <meta name="description" content="My Parse App"> 
     <meta name="viewport" content="width=device-width"> 
     <!-- <link rel="stylesheet" href="css/reset.css">--> 
     <!-- <link rel="stylesheet" href="css/styles.css">--> 
     <!-- 
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
      --> 
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
     <script type="text/javascript" src="http://www.parsecdn.com/js/parse-latest.js"></script> 
     <style> 
      /* my CSS here */ 
     </style> 
    </head> 
    <body bgcolor="#45a049"> 
     <div> 
      <form id=post-form> 
       <label for="fname">First Name :</label> 
       <input type="text" id="post-fname" name="firstname"><br> 
       <label for="lname">Last Name :</label> 
       <input type="text" id="post-lname" name="lastname"><br> 
       <label for="image">Select Image :</label> 
       <input type="file" id="post-image" ><br> 
       <input type="submit" value="Submit"> 
      </form> 
     </div> 
     <list id="list-posts" style="list-style-type:circle; width:50%" border="2" cellspacing="1" cellpadding="100" > 
     </list> 
     <script type="text/javascript"> 
      /* my JS here */ 
     </script> 
    </body> 
</html> 

CSS

input[type=text], 
select { 
    width: 50%; 
    /*align-content:center;*/ 

    align-self: center; 
    padding: 5px 20px; 
    margin: 30px 0; 
    display: inline-block; 
    border: 1px solid #ccc; 
    border-radius: 4px; 
    box-sizing: border-box; 
} 
input[type=file], 
select { 
    /*padding: 20px 5px;*/ 

    padding-right: -5px; 
    padding-left: 50px; 
    margin: 20px 10px; 
} 
input[type=submit] { 
    width: 40%; 
    /*align-self: center;*/ 

    background-color: #4CAF50; 
    color: white; 
    padding: 7px 30px; 
    /*align:center;*/ 

    margin: 20px 10px; 
    margin-left: 5px; 
    margin-right: 5px; 
    text-align: center; 
    border: none; 
    border-radius: 10px; 
    cursor: pointer; 
} 
input[type=submit]:hover { 
    background-color: #45a049; 
} 
div { 
    border-radius: 10px; 
    background-color: #f2f2f2; 
    background-position: center; 
    background-size: contain; 
    /*background-position: 30px 60px;*/ 

    position: absolute; 
    padding: 10px 10px; 
    margin: 80px 500px; 
    text-align: center; 
    align-self: center; 
    left=50%; 
    top=50%; 
} 

JS

Parse.initialize("", ""); 


var Post = Parse.Object.extend("UpdateDelete"); 
$("#post-form").submit(function(event) { 
    event.preventDefault(); 
    var Firstname = $("#post-fname").val(); 
    var Lastname = $("#post-lname").val(); 
    var Image = $("#post-image").val(); 

    var new_post = new Post; 

    new_post.set("Firstname", Firstname); 
    new_post.set("Lastname", Lastname); 
    var fileElement = $("#post-image")[0]; 
    var filePath = $("#post-image").val(); 
    var fileName = filePath.split("\\").pop(); 
    console.log("sumbit for post clicked"); 

    if (fileElement.files.length > 0) { 
     var file = fileElement.files[0]; 
     var newFile = new Parse.File(fileName, file); 
     newFile.save({ 
      success: function() { 
       console.log("Instered"); 
      }, 
      error: function(file, error) { 
       console.log("Error"); 
      } 

     }).then(function(theFile) { 

      new_post.set("Image", theFile); 
      new_post.save({ 
       success: function() { 
        console.log("Image Saved"); 
       }, 
       error: function(error) { 
        console.log("Post Save with file error " + error.message); 
       } 
      }); 
     }); 

    } else { 

     new_post.save(Firstname, Lastname, { 
      success: function(new_post) { 
       console.log("All posted"); 
       getPosts(); 
      }, 
      error: function(new_post, error) { 
       console.log("Error" + error.message); 
      } 
     }); 

    } 
}); 


// deleting item form list 
$("#list-posts").on("click", "#delitem", function() { 

    close(); 
    var id = $(this).attr("href"); 
    console.log("ID Reteived" + id); 
    var query = new Parse.Query(Post); 
    var delobj = id; 

    query.equalTo("objectId", id); 
    query.get(delobj, { 

     success: function(delobj) { 

      delobj.destroy({}); 
      location.reload(); 
      console.log("Object deleted succesfully"); 

     }, 
     error: function(delobj, error) { 
      console.log("error deleting"); 
     } 

    }); 
}); 

function showDialog() { 
    $("#list-posts").on("click", "#showDialog()", function(e) { 
     e.preventDefault(); 
     var x = document.getElementById("myDialog"); 

     function showDialog() { 
      x.show(); 
     } 
    }) 
} 


// Retriving 
function getPosts() { 
    var query = new Parse.Query(Post); 
    query.find({ 

     success: function(results) { 
      console.log("Retriving"); 
      var output = ""; 
      for (var i in results) { 

       var Firstname = results[i].get("Firstname"); 
       var Lastname = results[i].get("Lastname"); 

       var id = results[i].id; 
       //      console.log(results[i].get("file")); 
       var img = ""; 


       if (results[i].get("Image")) { 
        var file = results[i].get("Image"); 
        var url = file.url(); 
        console.log("url:" + url); 

        img = "<img src='" + url + "' style='width:170px;height:170px; margin:20px 10px'/>"; 
       } 
       output += "<li style= 'list-style-type:circle' reversed='reversed' >"; 
       output += "<h3 id='Firstname' href='" + id + "'>" + Firstname + "</h3>"; 

       output += "<li style= 'list-style-type:circle' reversed='reversed' >"; 
       output += "<h3 id='Lastname' href='" + id + "' >" + Lastname + "</h3>"; 
       output += img; 
       output += "<button id='delitem' href='" + id + "' style='display:inline-block; margin:20px 10px'>delete</button>"; 
       output += "<button id='mydialog' style='display:inline-block; margin:20px 10px'>User Info</button>"; 
       output += "<dialog id='myDialog'> This is dialoge window</dialog>"; 


       output += "<button id='modals_close' style='display:inline-block; margin:20px 10px'>Close Info</button>"; 
       output += "</li>"; 


       $("#list-posts").html(output); 
      }; 
     }, 
     error: function(error) { 
      console.log("Query Error" + error.message); 
     } 

    }); 

} 
getPosts(); 
+0

描述*你想要隱藏/關閉什麼。它是否是一些特定的元素? – Chris

+2

您可以簡單地將'display:none'動態地添加到他們的css中......他們不會在頁面上顯示,並且還會保留在數據庫中。 –

+0

我想顯示這些結果,但是當有人點擊關閉時,它會從我的頁面關閉。 –

回答

0

首先:你通過項目列表迭代併爲每個項目創建HTML。該HTML包含具有ID的DOM元素。所以一個ID在你的HTML中多次出現,這是不允許的。改用類。

關閉<LI>標籤的數量並不一樣。

我創建了一個小提琴從您提供的代碼:https://jsfiddle.net/1muc0460/
但由於訪問api.parse.com未經授權(由於初始化失敗,因爲我們需要一個應用程序鍵和JS-ID),我們不能與它相差甚遠。

它有很多錯誤。包含特殊字符的選擇器。函數定義中有相同名稱的函數定義。那些添加事件處理程序,但從未被調用(初始化)。

我開始進行一些修正:https://jsfiddle.net/1muc0460/1/
但我放棄了。我不知道你想要追求什麼邏輯,所以很難修復。 我在JS創建時添加了一個測試項目,但是沒有任何按鈕有任何影響。

只要您提供適當的功能代碼,我可以更新此答案。