php 实现excel作为字典 实现h5手机端下拉联动

2020-04-11  本文已影响0人  怪力程序员

jq插件 jquery-weui select

php phpExcel

php代码:

function get_xw_xk_zd(type_code,code){//硕士
path_excel='my_info_ss.xlsx';path_excel=WWW.'upload/'.path_excel;PHPReader = new PHPExcel_Reader_Excel2007();;
if(!PHPReader->canRead(path_excel)){
PHPReader = new PHPExcel_Reader_Excel5(); if(!PHPReader->canRead(path_excel)){ background_output(['state'=>'false','msg'=>'格式不正确']); die; } }PHPExcel = PHPReader->load(path_excel);
/读取excel文件中的第一个工作表/
currentSheet =PHPExcel->getSheet(0);
/
取得最大的列号/
allColumn =currentSheet->getHighestColumn();
/*取得一共有多少行/
allRow =currentSheet->getHighestRow();
bzzd=array( 'A'=>'门类代码', 'B'=>'门类名称', 'C'=>'学科代码', 'D'=>'学科名称', 'E'=>'学科编号', 'F'=>'上级学科编号', ); for(colIndex='A';colIndex<=allColumn;colIndex++){addr = colIndex.'1';cell = currentSheet->getCell(addr)->getValue();
if(cell instanceof PHPExcel_RichText) { //富文本转换字符串cell = cell->__toString(); } if(bzzd[colIndex]!=cell){
error='表字段不对应!'; } } if(error){
background_output(['state'=>'false','msg'=>'解析失败']);
}else{
doctorate=[];doctorate_one=[];
doctorate_one_all=[];doctorate_two_relavite__all=[];
xk_type_all=[]; //循环读取每个单元格的内容。注意行从2开始,列从A开始 for(rowIndex=2;rowIndex<=allRow;rowIndex++){ for(colIndex='A';colIndex<=allColumn;colIndex++){addr = colIndex.rowIndex;
cell =currentSheet->getCell(addr)->getValue(); if(cell instanceof PHPExcel_RichText) {
//富文本转换字符串
cell =cell->__toString();
}
//拼接sql
if(colIndex=='A'){a=cell; }elseif(colIndex=='B'){
b=cell;
}elseif(colIndex=='C'){c=cell; }elseif(colIndex=='D'){
d=cell;
}elseif(colIndex=='E'){e=cell; }elseif(colIndex=='F'){
f=cell;
}

       }
       $doctorate[$e]=[
           'name'=>$d,
           'code'=>$e,
           'type'=>$b,
           'parent_number'=>$f,
       ];
       $xk_type=[
           'title'=>$b,
           'value'=>$a,
       ];
       if(!in_array($xk_type,$xk_type_all)){
           array_push($xk_type_all,$xk_type);
       }
       if($f==0&&$a==$type_code){
           $doctorate_one=[
               'title'=>$d,
               'value'=>$e,
           ];
           array_push($doctorate_one_all,$doctorate_one);
       }
       if($f>0&&$f<103){
           $doctorate_two[$c]=$d;
           if($code&&$f==$code){
               $doctorate_two_relavite=[
                   'title'=>$d,
                   'value'=>$e,
               ];
               array_push($doctorate_two_relavite__all,$doctorate_two_relavite);
           }
       }
   }

}
//array_unique(xk_type_all, SORT_REGULAR);result=[
'data1'=>json_encode(doctorate_one_all,true),//一级学科 'data2'=>doctorate_two,//二级学科字典
'data3'=>doctorate, 'data4'=>json_encode(doctorate_two_relavite__all,true),//二级学科
'data5'=>json_encode($xk_type_all,true),//学科门类
];

return $result;
}

前端代码:

<div class="my_cell">
<div class="my_cell_hd">
<label class="my-label ">硕博学科门类:</label>
</div>
<div class="my_cell_bd">
<input type="text" class="my-input select" name="postdb[BH_PSUBC]" :value="tab_one_data.BH_PSUBC" id="select1">
</div>
</div>
<div class="my_cell">
<div class="my_cell_hd">
<label class="my-label ">一级学科:</label>
</div>
<div class="my_cell_bd">
<input type="text" class="my-input select" name="postdb[BH_PSUB]" :value="tab_one_data.BH_PSUB" id="select2">
</div>
</div>
<div class="my_cell">
<div class="my_cell_hd">
<label class="my-label "> 二级学科:</label>
</div>
<div class="my_cell_bd">
<input type="text" class="my-input select" name="postdb[BH_PSUB_2]" :value="tab_one_data.BH_PSUB_2" id="select3">
</div>
</div>

js主要代码:

function change_one_tab() {
var url='<{webdb['www']}>widgets/Encyclopedia/?h=my&job=My_info_edit';.ajax({
type: "post",
url:url,
dataType:"json",
data:{getJsonOne:3,subject_xk:1},
success:function(json){
if(json.state=='ok'){
my_detail.change_tab='one';
my_detail.tab_one_data=json.data;
var xkzd=json.bs_xk_zd;
$("#select1").select({
title: "选择学科门类",
items: JSON.parse(xkzd),
onChange: function(d){
if(d.values){
get_one_xk(d.values);
}
},
onClose: function () {

               },
           });
       }
   },
   error: function(json) {
       //alert('加载有误!')
   },

})
}

function get_one_xk(code) {
var url='<{webdb['www']}>widgets/Encyclopedia/?h=my&job=My_info_edit';.ajax({
type: "post",
url:url,
dataType:"json",
data:{getJsonOne:4,type_code:code,subject_xk:1},
success:function(json){
if(json.state=='ok'){
var xkzd1=json.bs_xk_zd;
console.log(xkzd1);
$("#select2").select(
"update",
{
title: "选择一级学科",
items: JSON.parse(xkzd1),
onChange: function(d){
if(d.values){
get_two_xk(d.values);
}
},
}
);

       }
   },
   error: function(json) {
       //alert('加载有误!')
   },

})
}

上一篇下一篇

猜你喜欢

热点阅读