【发布时间】:2015-07-10 09:42:07
【问题描述】:
我正在开发一个就业网站。在搜索结果页面上,我将 url 变量传递给 Coldfusion 组件,该组件以 JSON 格式返回结果,然后使用把手模板输出(感谢 Raymond Camden 的脚本,可以找到 here)。
我想使用基于我数据库中的各种类别的复选框来过滤结果,网上有一个 PHP 教程,它完全符合我希望我的搜索页面做的事情,可以在 here 找到>
这是我的脚本和车把模板:
车把模板:
<script id="results-template" type="text/x-handlebars-template">
{{#each records}}
<div class="search-results">
<h3 class="text-left">{{job_title}}</h3>
<ul class="list-group">
<li class="list-group-item">DATE POSTED: {{job_date_post}}</li>
<li class="list-group-item">JOB REF NO: {{job_ref_no}}</li>
<li class="list-group-item">INDUSTRY: {{job_industry}}</li>
<li class="list-group-item">KEYWORDS: {{job_keywords}}</li>
<li class="list-group-item">JOB TYPE: {{job_type_id}}</li>
</ul>
</div>
{{/each}}
</script>
这里是ajax调用:
<script>
function cfQueryNormalize(d) {
var result = [];
for(var i=0, len=d.DATA.length; i<len;i++) {
var item = {};
for(var k=0,innerlen=d.COLUMNS.length; k<innerlen; k++ ) {
item[d.COLUMNS[k].toLowerCase()] = d.DATA[i][k];
}
result.push(item);
}
return result;
}
$(document).ready(function() {
//Get the contents from the script block
var source = document.querySelector("#results-template").innerHTML;
//Compile that baby into a template
template = Handlebars.compile(source);
$.get("cfc/search-results.cfc?method=getresults&returnformat=json", {city:"<cfoutput>#url.city#</cfoutput>", Keywords:"<cfoutput>#url.keywords#</cfoutput>"}, function(res,code) {
var d = cfQueryNormalize(res);
var html = template({records:d});
$("#results").html(html);
}, "json");
});
</script>
这是 Coldfusion 组件:
<cffunction access="remote" name="getresults" output="false" >
<cfargument name="city" displayName="city" type="string" hint="Displays the Search Results" />
<cfargument name="keywords" displayName="keywords" type="string" hint="Displays the Search Results" />
<cfargument name="salary_id" displayName="salary_id" type="string" hint="Displays the Salary Results" />
<cfargument name="job_type_id" displayname="job_type_id" type="string" required="no">
<cfargument name="job_industry" displayname="job_industry" type="string" required="no">
<cfquery name="getresults" datasource="#datasource#" username="#username#" password="#password#">
SELECT jobs.job_id,
jobs.job_title,
jobs.job_type_id,
jobs.job_salary_id,
jobs.job_salary,
jobs.loc_country,
jobs.loc_region,
jobs.loc_city,
jobs.job_date_post,
jobs.job_ref_no,
jobs.job_detail_organization,
jobs.job_detail_requirements,
jobs.job_detail_description,
jobs.recruiter_id,
jobs.job_industry,
jobs.job_sub_industry,
jobs.job_keywords,
jobs.job_active,
jobs.job_applications,
jobs.job_views
FROM jobs
WHERE <cfif #Arguments.city# GT''>jobs.loc_city = #Arguments.city# AND</cfif> jobs.job_keywords LIKE '%#Arguments.keywords#%' <cfif Isdefined ('Arguments.salary_id')>AND jobs.job_salary_id = #Arguments.salary_id#</cfif>
</cfquery>
<cfreturn getresults>
</cffunction>
我的复选框将基于:
1) 薪水,他们将有一系列年薪金额
2) 工作类型 - 永久、兼职、临时等
3) 工作行业。
结果在数据库中都有对应的复选框字段。
我如何能够单击一个或多个复选框并根据我所做的选择优化 Coldfusion 组件中的结果?
任何帮助将不胜感激。
托马拉克:
这是我拥有的一组复选框的代码:
<cfloop query="getstypes">
<li class="list-group-item">
<label><input class="job_salary#getstypes.job_salary_id#" type="checkbox" name="salary_id" id="salary_id" value="#getstypes.job_salary_id#"> #getcur.currency_symbol##numberformat(getstypes.job_salary_from, ",")#to #getcur.currency_symbol##numberformat(getstypes.job_salary_to, ",")#<span>(#getscount.recordcount#)</span></label>
</li>
</cfloop>
请问这是否正确?
【问题讨论】:
-
看起来您正在从数据库中获取与传入的 city/keywords/salary_id 匹配的所有记录。然后您是否希望根据单击复选框进一步过滤该结果集?如果是这样,那么我会考虑使用 JS 过滤浏览器中已有的记录集,而不是重复从数据库中查找记录。顺便说一句,非常值得一读 SQL 注入攻击和 cfqueryparam :)
-
有没有办法让用户在这个过程的早期输入他的过滤器?
-
嗨,约翰,感谢您回复我。主要搜索关键字,然后用户可以选择国家和城市,如果它们是空白的,那么它将加载该国家的所有工作。我想使用 JS 过滤从主搜索返回的记录集。
-
嗨,丹,感谢您回复我。我的客户希望使用关键字进行主要搜索,然后在有意义的情况下使用 Salary Job Type 和 Industry 过滤结果。
-
这没有意义。搜索关键字和 url 变量是一个奇怪的组合。你有一个带有method =“get”的表单吗?
标签: jquery ajax checkbox filter coldfusion