因为项目用到DataTable表格加载后台数据,要连表查询虚拟机选中的策略状态,所以想到先把策略表内容取出来,组成一个‘<select><option value="1"></option>[n个option]</select>‘字符串,在遍历虚拟机列表时把他的策略值拼成 ‘value="1"‘ 这样的字符串,再利用explode()和implode() 函数,组成新的字符串返回给前台,就实现了选中状态。
1 $option = ‘<select class="sla_list">‘; 2 3 // 取出所有策略 4 $sla_query = $this->db->select(‘sla_id, name‘)->get(‘sla‘); 5 $sla_res = $sla_query->result_array(); 6 if (!empty($sla_res)) { 7 $option .= ‘<option value="0">未保护</option>‘; 8 foreach ($sla_res as $k1 => $v1) { 9 $option .= ‘<option value="‘ . $v1[‘sla_id‘] . ‘">‘ . $v1[‘name‘] . ‘</option>‘; 10 } 11 $option .= ‘</select>‘; 12 13 } else { 14 $option .= ‘<option value="0">未保护</option></select>‘; 15 } 16 17 18 // 选中默认的保护策略 19 $vm_query = $this->db->select(‘sla_id‘)->where(‘vm_id‘, $vm_id)->get(‘task_vm‘, 1); 20 $vm_res = $vm_query->row_array(); 21 if (is_null($vm_res[‘sla_id‘])) $res[‘sla_id‘] = 0; 22 $selected = ‘value="‘ . $vm_res[‘sla_id‘] . ‘"‘; 23 $new_str_arr = explode($selected, $option); 24 $new_option = implode(" {$selected} selected", $new_str_arr);
时间: 2024-12-29 11:46:17