简体   繁体   English

单击按钮从后端表中获取确定的数据

[英]Getting definite data from backend table by clicking on button

I'm new at full stack programming and I stucked on this situation.我是全栈编程的新手,我坚持这种情况。 So point of my problem is, I got backend table with data, which I'm receiving it in JSON format as an array of objects.所以我的问题是,我得到了带有数据的后端表,我以 JSON 格式将它作为对象数组接收。 I need to show on my HTML page the definite data when I click definite button.当我单击确定按钮时,我需要在我的 HTML 页面上显示确定数据。 For example, when I click "food" button function should show all rows from table with category_id = 2 which assigned to all posts with "foot" category.例如,当我单击“食物”按钮时,函数应该显示表中 category_id = 2 的所有行,这些行分配给所有具有“脚”类别的帖子。 Please help!请帮忙!

Objects I get from backend look like this:我从后端获取的对象如下所示:

{"catName":[
    {"id":"1","title":"title ","description":"post","date":"2021-10-20 20:20:41","category_id":"1","author_id":"1","name":"lifestyle"},
    {"id":"2","title":"title 5","description":"post 5","date":"2021-10-20 20:22:57","category_id":"1","author_id":"1","name":"lifestyle"},
    {"id":"3","title":"title 1","description":"post 1","date":"2021-10-20 20:21:42","category_id":"2","author_id":"1","name":"food"},
    {"id":"4","title":"title 7","description":"post 7","date":"2021-10-20 20:23:20","category_id":"2","author_id":"1","name":"food"},
    {"id":"2","title":"title 10","description":"post 10","date":"2021-10-26 17:23:12","category_id":"2","author_id":"1","name":"food"}
] 

Here's my function, for now it returns only rows with category_id == 1 , know that I need to do something with e.category_id == 1 and put some conditional statements, but seems like everything I do doesn't works on it.这是我的函数,现在它只返回category_id == 1行,知道我需要用e.category_id == 1做一些事情并放置一些条件语句,但似乎我所做的一切都不起作用。

function getDefCategories() {
  $.ajax({
    url: `getcategory.php`
  }).done(function(res) {
    res = JSON.parse(res)
    showDefCategories(res.catName)
  })
}

function showDefCategories(categories) {
  let outputOption = ``
  let output = Object.values(categories).filter(function(e) {
    return e.category_id == 1
  })
  for (let each of output) {
    outputOption += `
                <div class="col-sm-5">            
                <div class='card mb-3'>
                <img src='https://preview.colorlib.com/theme/wordify/images/ximg_5.jpg.pagespeed.ic.efC6YUsPoM.webp' class='card-img-top' alt='...''>
                <div class='card-body'>
                <h3>${each.title}</h3>
                <p class='card-text'>${each.description}</p>
                <p class='card-text' style='font-size: 10px'>Posted: ${each.date}</p>
                <p class='card-text'>Category: ${each.name}</p>
                </div>
            </div>
            </div>
            `
    $('#defPost').html(outputOption)
  }

and my buttons:和我的按钮:

<ul class="sub-menu">
    <li><a href="#!" id="lifestyle">Lifestyle</a></li>
    <li><a href="#!" id="food">Food</a></li>
    <li><a href="#!" id="adventure">Adventure</a></li>
    <li><a href="#!" id="travel">Travel</a></li>
    <li><a href="#!" id="business">Business</a></li>
</ul>

Add a data-category attribute to the links.向链接添加data-category属性。 Then in the event listener, get the value of this attribute $(this).data("category") .然后在事件侦听器中,获取此属性$(this).data("category") You can pass this to getDefCategories() , which can pass it to showDefCategories() , and use that instead of the hard-coded category number.您可以将其传递给getDefCategories() ,后者可以将其传递给showDefCategories() ,并使用它代替硬编码的类别编号。

 $(".sub-menu a").click(function(e) { e.preventDefault(); getDefCategories($(this).data("category")); }); function getDefCategories(selectedCat) { $.ajax({ url: `getcategory.php` }).done(function(res) { res = JSON.parse(res) showDefCategories(res.catName, selectedCat) }) } function showDefCategories(categories, selectedCat) { let outputOption = `` let output = Object.values(categories).filter(function(e) { return e.category_id == selectedCat }) for (let each of output) { outputOption += ` <div class="col-sm-5"> <div class='card mb-3'> <img src='https://preview.colorlib.com/theme/wordify/images/ximg_5.jpg.pagespeed.ic.efC6YUsPoM.webp' class='card-img-top' alt='...''> <div class='card-body'> <h3>${each.title}</h3> <p class='card-text'>${each.description}</p> <p class='card-text' style='font-size: 10px'>Posted: ${each.date}</p> <p class='card-text'>Category: ${each.name}</p> </div> </div> </div> ` $('#defPost').html(outputOption) } }
 <ul class="sub-menu"> <li><a href="" id="lifestyle" data-category="1">Lifestyle</a></li> <li><a href="" id="food" data-category="2">Food</a></li> <li><a href="" id="adventure" data-category="3">Adventure</a></li> <li><a href="" id="travel" data-category="4">Travel</a></li> <li><a href="" id="business" data-category="5">Business</a></li> </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM