获取文本而不是选择元素

时间:2016-01-26 02:05:56

标签: javascript jquery codeigniter

有没有办法获取文本而不是选择元素上的值并创建存储以将其传递到$ this-> input-> post('cols')?

这是我的控制器

public function index() {
    $data['title'] = 'Reports';
    $data['content'] = 'reports';
    $this->load->view('layout/admin_layout', $data);
}

public function gen_custom_rep() {
    $col_num = 1;
    $c_dt_st = explode("/", $this->input->post('c_dt_st'));
    $c_dt_end = explode("/", $this->input->post('c_dt_end'));
    $dt_st = $c_dt_st[2] . '-' . $c_dt_st[0] . '-' . $c_dt_st[1];
    $dt_end = $c_dt_end[2] . '-' . $c_dt_end[0] . '-' . $c_dt_end[1];
    $ref = $this->input->post('list_of');
    $oprtn = $this->input->post('oprtn');
    $val = $this->input->post('val');
    $columns = array("$ref");
    $qry = "SELECT DISTINCT($ref) as ref_col";
    foreach ($this->input->post('cols') as $col_nm) {
        $col_res = $this->m_reports->exec_custom("SELECT DISTINCT($col_nm) as col_key FROM tbl_proj_details");
        foreach ($col_res as $c_res) {
            array_push($columns, $c_res->col_key);
            $qry = $qry . ",(SELECT $oprtn($val) FROM tbl_proj_details WHERE $ref = ref_col AND $col_nm='$c_res->col_key' AND (dt_approved BETWEEN '$dt_st' AND '$dt_end')) as col_$col_num";
            $col_num = $col_num + 1;
        }
    }
    $qry = $qry . " FROM tbl_proj_details";
    $data['res'] = $this->m_reports->exec_custom($qry);
    $this->load->library('excel');
    $this->load->view('reports/report_excel', $data);
}

public function generate_imported() {
    $frm = $this->input->post('yr');
    $qry = "SELECT masterlist_id";

    foreach ($this->input->post('cols')as $col_nm) {
        $qry = $qry . ", " . $col_nm;
    }
    $qry = $qry . " FROM master_list WHERE date_approved LIKE '%$frm%'";
    $this->m_reports->exec_custom_array($qry);

    $this->load->library('excel');
    //Set Active Sheet
    $this->excel->setActiveSheetIndex(0);
    $this->excel->getActiveSheet()->setTitle('Core and Non Core');
    // Set Orientation, size and scaling
    $this->excel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
    $this->excel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
    $this->excel->getActiveSheet()->getPageSetup()->setFitToPage(true);
    $this->excel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
    $this->excel->getActiveSheet()->getPageSetup()->setFitToHeight(0);
    //Set Page Margins
    $this->excel->getActiveSheet()->getPageMargins()->setTop(0.75);
    $this->excel->getActiveSheet()->getPageMargins()->setRight(1.2);
    $this->excel->getActiveSheet()->getPageMargins()->setLeft(1.2);
    $this->excel->getActiveSheet()->getPageMargins()->setBottom(0.75);
    //Set Repeat Row
    $this->excel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(5, 6);

    $cls = $this->input->post('cols');
    array_unshift($cls, "ID");
    $this->make_row('A', 1, $cls);
    if (is_array($res)) {
        $r_nm = 2;
        foreach ($res as $r) {
            $cl_nm = 'A';
            foreach ($r as $flds) {
                $this->make_cell($flds, $cl_nm, $r_nm);
                $cl_nm++;
            }
            $r_nm++;
        }
    }

    $filename = 'SETUP_master_list.xlsx'; //save our workbook as this file name
    header('Content-Type: application/vnd.ms-excel'); //mime type
    header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
    header('Cache-Control: max-age=0'); //no cache
    //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
    //if you want to save it as .XLSX Excel 2007 format
    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
    //force user to download the Excel file without writing it to server's HD
    $objWriter->save('php://output');
}

public function make_cell($cellvalue = 0, $hloc = 0, $vloc = 0, $colspan = 1, $rowspan = 1, $width = false, $bld = false, $centered = false, $perce = FALSE, $colr = FALSE, $fnt_size = 11) {
    $this->excel->getActiveSheet()->setCellValue($hloc . $vloc, $cellvalue);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getFont()->setSize($fnt_size);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getFont()->setBold($bld);
    $hloc2 = $hloc;
    $vloc2 = $vloc;
    $styleArray = array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN)),);
    if ($colspan > 1 || $rowspan > 1) {
        while ($colspan > 1) {
            $hloc2++;
            $colspan--;
        }
        while ($rowspan > 1) {
            $vloc2++;
            $rowspan--;
        }
        $this->excel->getActiveSheet()->mergeCells($hloc . $vloc . ':' . $hloc2 . $vloc2);
        $this->excel->getActiveSheet()->getStyle($hloc . $vloc . ':' . $hloc2 . $vloc2)->applyFromArray($styleArray);
        $this->excel->getActiveSheet()->getStyle($hloc . $vloc . ':' . $hloc2 . $vloc2)->getAlignment()->setWrapText(true);
    }
    if ($centered == true) {
        $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    }
    if ($width != false) {
        $this->excel->getActiveSheet()->getColumnDimension($hloc)->setWidth($width);
    }
    if ($perce != false) {
        $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getNumberFormat()->applyFromArray(
                array(
                    'code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00
                )
        );
    }
    if ($colr != FALSE) {
        $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getFill()->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $colr)));
    }
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->applyFromArray($styleArray);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getAlignment()->setWrapText(true);
}

public function makeheader_noborder($cellvalue = 0, $hloc = 0, $vloc = 0, $colspan = 1, $rowspan = 1, $bld = false, $fnt_size = 11) {
    $this->excel->getActiveSheet()->setCellValue($hloc . $vloc, $cellvalue);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getFont()->setSize($fnt_size);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getFont()->setBold($bld);
    $hloc2 = $hloc;
    $vloc2 = $vloc;
    if ($colspan > 1 || $rowspan > 1) {
        while ($colspan > 1) {
            $hloc2++;
            $colspan--;
        }
        while ($rowspan > 1) {
            $vloc2++;
            $rowspan--;
        }
        $this->excel->getActiveSheet()->mergeCells($hloc . $vloc . ':' . $hloc2 . $vloc2);
    }
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}

public function makecell_vertical($cellvalue = 0, $hloc = 0, $vloc = 0, $colspan = 1, $rowspan = 1, $width = false, $height = false, $bld = false, $fnt_size = 11) {
    $this->excel->getActiveSheet()->setCellValue($hloc . $vloc, $cellvalue);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getFont()->setSize($fnt_size);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getFont()->setBold($bld);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getAlignment()->setTextRotation(90);
    $hloc2 = $hloc;
    $vloc2 = $vloc;
    $styleArray = array(
        'borders' => array(
            'allborders' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
            )
        ),
    );
    if ($colspan > 1 || $rowspan > 1) {
        while ($colspan > 1) {
            $hloc2++;
            $colspan--;
        }
        while ($rowspan > 1) {
            $vloc2++;
            $rowspan--;
        }
        $this->excel->getActiveSheet()->mergeCells($hloc . $vloc . ':' . $hloc2 . $vloc2);
        $this->excel->getActiveSheet()->getStyle($hloc . $vloc . ':' . $hloc2 . $vloc2)->applyFromArray($styleArray);
        $this->excel->getActiveSheet()->getStyle($hloc . $vloc . ':' . $hloc2 . $vloc2)->getAlignment()->setWrapText(true);
    }
    if ($width != false) {
        $this->excel->getActiveSheet()->getColumnDimension($hloc)->setWidth($width);
    }
    if ($height != false) {
        $this->excel->getActiveSheet()->getRowDimension($vloc)->setRowHeight($height);
    } else if ($height == false) {
        $this->excel->getActiveSheet()->getRowDimension($vloc)->setRowHeight(-1);
    }
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->applyFromArray($styleArray);
    $this->excel->getActiveSheet()->getStyle($hloc . $vloc)->getAlignment()->setWrapText(true);
}

public function make_row($int_pos, $r_no, $arr, $width = FALSE) {
    if (is_array($arr)) {
        foreach ($arr as $cv) {
            $this->make_cell($cv, $int_pos, $r_no, 1, 1, $width, FALSE, FALSE);
            $int_pos++;
        }
    }
}

}

在我的视图页面中。

    <div class="form-group col-md-6 col-sm-12">
                    <label>Columns:(Ctrl+Click to Select Multiple)</label>
                    <select class="form-control" name="cols[]" multiple="multiple" id="cols[]" required="required">
                        <option value="latest_upd">Latest Update</option>
                        <option value="status">Status</option>
                        <option value="spin">SPIN</option>
                        <option value="file_location">File location</option>
                        <option value="upl_webgis">uploaded to webgis</option>
                        <option value="rem_upl">Remarks on upload</option>
                        <option value="level_approval">Level of Approval</option>
                        <option value="date_approved">Date Approved</option>
                        <option value="year_appr">Year Approved</option>
    </select>

下面。我刚编辑了我的帖子。 :D有没有人知道如何从选定的选项中获取文本并将其放在变量中以将其传递到输入帖子上,以便我可以将其用作生成的Excel文件的列名

1 个答案:

答案 0 :(得分:1)

您可以使用

选择所选选项的文本

$('#cols').change(function(){ var selectedOptions = []; $('#cols option:selected').each(function(){ selectedOptions.push($(this).text()); }); alert(selectedOptions); });

以下是示例

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="cols" id="cols" multiple>
           <option value"latest_update"> Lates Update</option>
           <option value"spin"> SPIN</option>
           <option value"status"> Status</option>
    </select>
&#13;
get '/' do
  template_output = erb :template
  "Here is the output from template: #{template_output}"
end
&#13;
&#13;
&#13;