-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-rent.js
189 lines (175 loc) · 6.54 KB
/
script-rent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var filters = {bachelors:true,job:true,business:true};
function currency(x) {
x=x.toString();
var lastThree = x.substring(x.length-3);
var otherNumbers = x.substring(0,x.length-3);
if(otherNumbers != '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
return res;
}
function filter(data) {
if(filters.name && data.name.toLowerCase().indexOf(filters.name.toLowerCase())<=-1)
return false;
if(filters.minRent && Number(filters.minRent)>Number(data.rent))
return false;
if(filters.maxRent && Number(filters.maxRent)<Number(data.rent))
return false;
if(filters.area && (isNaN(Number(filters.area)) || data.unit!="sq.yd." || Number(data.area)<Number(filters.area) || Number(data.area)>(Number(filters.area)+100)))
return false;
if(filters.bhk && (filters.bhk != "Any" && data.bhk != filters.bhk))
return false;
if(filters.address && data.address.toLowerCase().indexOf(filters.address.toLowerCase())<=-1)
return false;
if(filters.sector && (data.sector != filters.sector))
return false;
if(filters.type && (filters.type != "Any" && data.type != filters.type))
return false;
if(!((filters.bachelors && data.bachelors=="1") || (filters.job && data.job=="1") || (filters.business && data.business=="1") || (filters.family && data.family=="1")))
return false;
if(filters.rooms && data.rooms!="1")
return false;
if(filters.kitchen && data.kitchen!="1")
return false;
if(filters.new && data.new!="1")
return false;
return true;
}
var rentdata = "{}";
var pagePosition = 0;
var d1 = $.Deferred();
function getdata() {
pagePosition = $(window).scrollTop();
$('.loader').fadeIn(100);
$.get('xhr.php?q=rent',function() {
})
.done(function(data) {
rentdata = data;
loadList("entries");
d1.resolve();
$('.loader').fadeOut(100);
})
.fail(function() {
$('.loader').fadeOut(100);
$('#entries').html("<center>Unable to retrieve data.</center>");
});
}
function loadList(div) {
var box="";
//console.log("server sent: "+rentdata);
try {
var data = JSON.parse(rentdata);
var displayed = 0;
for(var i=data.length-1;i>=0;i--) {
var row = data[i];
//console.log(row);
if(filter(row)){
displayed++;
var date = new Date(Number(row.added));
box += "<div class='col-sm-12 box' id='"+row.id+"''><p class='text-left'>"+ date.toLocaleString()+"</p>";
if(row.address!= "") box += "<div class='box-small' data-id='address'><img src='images/address.png'> "+row.address+", Sec-"+row.sector+" </div>";
if(row.name!= "") box += "<div class='box-small'><img src='images/name.png'> <span data-id='name'>"+row.name+"</span> </div>";
if(row.bhk!= "") box += "<div class='box-small' data-id='bhk'>"+row.bhk+" </div>";
if(row.type!= "") box += "<div class='box-small' data-id='type'>"+row.type+" </div>";
if(row.area!= "") box += "<div class='box-small' data-id='type'><img src='images/area.png'> "+currency(row.area)+" "+row.unit +" </div>";
if(row.phone!= "") box += "<div class='box-small' data-id='phone'><img src='images/phone.png'><a href='tel:"+row.phone+"'> "+row.phone+"</a></div>";
if(row.rent!= "") box += "<div class='box-small' data-id='rent'><img src='images/rupee.png'> "+currency(row.rent)+" </div>";
//console.log(row.bachelors+row.job+row.business);
if(row.bachelors !="0") box += "<div class='box-small' data-id='bachelors'> "+"bachelors"+" </div>";
if(row.job !="0") box +="<div class='box-small' data-id='job'> "+"job"+" </div>";
if(row.business !="0") box +="<div class='box-small' data-id='business'> "+"business"+" </div>";
if(row.family !="0") box +="<div class='box-small' data-id='family'> "+"Family"+" </div>";
if(row.new !="0") box +="<div class='box-small' data-id='new'> <img src='images/new.png' title='Newly Built'> </div>";
if(row.kitchen !="0" || row.rooms !="0") {
var woodwork = "<img src='images/wood.png'> ";
if(row.kitchen !="0") woodwork += "Kitchen";
if(row.kitchen !="0" && row.rooms !="0") woodwork +=", ";
if(row.rooms !="0") woodwork += "Rooms";
box += "<div class='box-small' data-id='woodwork'>"+woodwork+"</div>";
}
box += "<div class='btn btn-info box-small' onclick='edit("+row.id+")'>Edit</div>";
box += "<div class='btn btn-danger box-small delButton'>x</div></div>";
}
}
if(displayed == 0) box="<div class='box'><center>No Results</center></div>";
//console.log(box);
$('#'+div).html(box);
$('.delButton').on("click",del);
}
catch(e) {
$('#'+div).html(rentdata);
}
}
function reload() {
pagePosition = $(window).scrollTop();
d1 = $.Deferred();
getdata();
$.when(d1).then(function(){
$(window).scrollTop(pagePosition);
});
}
function edit(id) {
window.open('add.html?rentid='+id,'_blank');
}
function del(){
//console.log($(this).parent());
var id = $(this).parent().attr('id');
var name = $(this).parent().find('[data-id=name]').html();
var bhk = $(this).parent().find('[data-id=bhk]').html();
var type = $(this).parent().find('[data-id=type]').html();
if(confirm("Delete "+name+"'s "+bhk+" "+type+"?")) {
$("#"+id).addClass("redBorder");
$.get('xhr.php?q=rent-delete&id='+id,function(){
//console.log("delete query sent");
})
.done(function(data){
//console.log("delete done: "+data);
$("#"+id).hide(500,function(){
pagePosition = $(window).scrollTop();
d1 = $.Deferred();
getdata();
$.when(d1).then(function(){
$(window).scrollTop(pagePosition);
});
});
});
}
}
function filterclr() {
$("#filterForm").trigger('reset');
filters = {bachelors:true,job:true,business:true};
loadList("entries");
}
$(function(){
var filterTimer;
getdata();
$('.filter').on("keyup",function(){
var that = this;
clearTimeout(filterTimer);
filterTimer = setTimeout(function(){
if($(that).val()=="")
delete filters[$(that).attr('name')];
else
filters[$(that).attr('name')] = $(that).val();
loadList("entries");
},500);
});
$('.filter').on("change",function(){
var that = this;
clearTimeout(filterTimer);
filterTimer = setTimeout(function(){
if($(that).val()=="")
delete filters[$(that).attr('name')];
else
filters[$(that).attr('name')] = $(that).val();
loadList("entries");
},500);
});
$('.filter-check').on("click",function(){
filters[$(this).attr('name')] = $(this).prop('checked');
loadList("entries");
});
$("#filterForm").submit( function(event) {
event.preventDefault();
});
});