标签:foreach length public count ras code class cti item
使用select2插件,如果后台查找没有记录,不能返回null,要返回一个空数组,不然会报错:data is null
空数组形式为(firefox调试输出):{"items":[],"total_count":0}
完整使用记录如下:
前台html标记:
<div class="form-group col-md-6"> <label for="supplier">供应商</label> <select name="supplier" id="supplier" class="form-control" > </select> </div>
前台JS
$(‘#supplier‘).select2({ theme: "bootstrap", placeholder: "选择供应商", allowClear: true, ajax:{ url:"{:U(‘Supplier/getvalidSupplier‘)}", dataType:"json", delay:200, data:function(params){ return { q:params.term, page:params.page } }, processResults:function(data,params){ params.page = params.page || 1; return { results: data.items, pagination: { more: (params.page * 10) < data.total_count } }; }, cache: false }, //minimumInputLength: 1, });
后台PHP TP3.22
public function getvalidSupplier(){ $q=I("q"); $page=intval(I("page"))<1?1:I("page"); $map[‘supplier|contractor|co_addr‘] = array(‘like‘,‘%‘.$q.‘%‘); $map[‘status‘]=1; $pageParas[‘pageLength‘]=10; $pageParas[‘pageStart‘]=($page-1)*10; $pageParas[‘orderField‘]=‘supplier‘; $arr = $this->getPageData($map,null,true,null,$pageParas); $i=0; $arr2=array();//重点在这里 foreach ($arr as $key => $row) { $arr2[]=array("id"=>$row[id],"text"=>$row[‘supplier‘]); $i++; } $this->ajaxReturn(array(‘items‘=>$arr2,"total_count"=>$i)); }
标签:foreach length public count ras code class cti item
原文地址:http://www.cnblogs.com/huangju6/p/6399684.html