function storeDataWithExpiry(data, expiryMinutes) {
    var item = {
        data: data,
        expiry: new Date().getTime() + (expiryMinutes * 60 * 1000)
    };
    localStorage.setItem('myDataWithExpiry', JSON.stringify(item));
}
function getDataWithExpiry() {
    var itemStr = localStorage.getItem('myDataWithExpiry');
    if (!itemStr) {
        return null;
    }
    var item = JSON.parse(itemStr);
    var now = new Date().getTime();
    if (now > item.expiry) {
        localStorage.removeItem('myDataWithExpiry');
        return null;
    }
    return item.data;
}
 
function list(data){
var html='';
data.forEach(function (item,index) {
html+=`<li><a href="mkDetail.html?dpid=${item.deptId}"><h4>${item.deptName}</h4><p><span>${item.cityName?item.cityName:''}</span><span>${item.depttype}</span></p><p>${item.address}</p></a></li>`;
})
if(data.length!=0){$("#lista").html(html);$(".notip").hide();}else{$("#lista").html('');$(".notip").show();}
}

function getDate(){
var md5Hash = $.md5('sign字符串').toUpperCase();
//console.log(md5Hash);  
$.ajax({
 url:"接口地址",
 dataType:"json",
 //data:{deptId:'00001',sign:md5Hash},
 async:true,
 type:"POST",
success:function(r){
   //storeDataWithExpiry(r, 360);
   list(r.result);
   localStorage.setItem('myDataWithExpiry', JSON.stringify(r));
},
 error:function(){
  alert('网络错误~');
}
})
}
getDate();
//数据本地化
var itemStr = localStorage.getItem('myDataWithExpiry');
var jsonData = [];  
if(itemStr){
var mydata= JSON.parse(itemStr);
    jsonData = mydata.result;
}

// 初始化渲染所有数据,jsonData是json数据
  list(jsonData);
//数据筛选
  var currentFilter = {cityName: "all",depttype: "all",mktSize:"all"};
  $(".filter-cityName li").click(function() {
    currentFilter.cityName = $(this).data("value");
    filterData();
  });
  $(".filter-depttype li").click(function() {
    currentFilter.depttype = $(this).data("value");
    filterData();
  });
  $(".filter-mktSize li").click(function() {
    currentFilter.mktSize = $(this).data("value");
    filterData();
  });
  function filterData() {
      var filteredData = jsonData.filter(function(item) {
      var levelMatch = currentFilter.cityName === "all" || item.cityName === currentFilter.cityName;
      var typeMatch = currentFilter.depttype === "all" || item.depttype === currentFilter.depttype;
      var mktMatch = true;
      if (currentFilter.mktSize !== "all") {
        switch(currentFilter.mktSize) {
          case "small":
            mktMatch = item.mktSize >= 1 && item.mktSize <= 49;
            break;
          case "medium":
            mktMatch = item.mktSize >= 50 && item.mktSize <= 99;
            break;
          case "large":
            mktMatch = item.mktSize >= 100;
            break;
        }
      }
      
      return levelMatch && typeMatch && mktMatch;
    });
    list(filteredData);
  }
   
//搜索
$("#searchBtn").on("click", function() {
    performSearch();
  });
$("#deptSearch").on("keypress", function(e) {
  if (e.which === 13) {
    performSearch();
  }
});
function performSearch() {
  var searchTerm = $("#deptSearch").val().trim().toLowerCase();
  if (searchTerm === "") {
    list(jsonData);
    return;
  }
  var filteredData = jsonData.filter(function(item) {
    return item.deptName.toLowerCase().includes(searchTerm);
  });
  list(filteredData);
}
 

标签: none

您需要先登录才能发表评论。