discuz3.4:自定crud页面中,下拉框的实现

这是用cat批量生成的几个文件。

该页面实现了数据表格编辑,城市字段实现下拉选择。注意数据库中,相应表同时有csid字段和cs字段。记录一下,以防丢失。

不足之处是

运行结果:

数据库中下拉内容,这次只用 list_name_en='csid' 的内容:

source\class\table\table_cat_gys.php:

<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *      $Id: table_cat_gys.php
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_cat_gys extends discuz_table
{
	public function __construct() {

		$this->_table = 'cat_gys';
		$this->_pk    = 'id';

		parent::__construct();
	}

	public function fetch_all_by_displayorder() {
		return DB::fetch_all('SELECT * FROM %t ORDER BY id ASC', array($this->_table), $this->_pk);
	}

	public function fetch_all_by_sql($start, $limit) {
		return DB::fetch_all('SELECT * FROM %t ORDER BY id ASC'.DB::limit($start, $limit), array($this->_table), $this->_pk);
	}
	
	//返回json格式数据
	public function fetch_json_by_sql($start, $limit, $m_conds) {
		$query = DB::query('SELECT * FROM %t WHERE 1=1 '.$m_conds.' ORDER BY id ASC'.DB::limit($start, $limit), array($this->_table), $this->_pk);
		
		$result = array();
		
		// 记录数量
		$row = DB::fetch_first('SELECT count(*) as cnt FROM %t WHERE 1=1 '.$m_conds, array($this->_table), $this->_pk); //所有记录数量
		$result["total"] = intval($row['cnt']);

		while($data = DB::fetch($query)){
			$list[] = array(  
              'c_dh' => urlencode($data['c_dh']),
              'c_gj' => urlencode($data['c_gj']),
              'c_yzbm' => urlencode($data['c_yzbm']),
              'c_dq' => urlencode($data['c_dq']),
              'c_csid' => urlencode($data['c_csid']),
              'c_cs' => urlencode($data['c_cs']),
              'c_dz' => urlencode($data['c_dz']),
              'c_lxrzw' => urlencode($data['c_lxrzw']),
              'c_lxrxm' => urlencode($data['c_lxrxm']),
              'c_gsmc' => urlencode($data['c_gsmc']),
              'c_gysid' => urlencode($data['c_gysid']),
              'id' => urlencode($data['id']),
              'c_cz' => urlencode($data['c_cz']),
              'c_zy' => urlencode($data['c_zy'])

			);  
		}  
		$result["rows"] = $list;
  
		return urldecode(json_encode($result));  
	}

	//public function fetch_by_id_username($id, $username, $adminid = 1) {
		//return DB::fetch_first('SELECT * FROM %t WHERE id=%d', array($this->_table, $id, $adminid, $username));
	//}

	// 根据一个id删除一条记录
	public function delete_by_id($id) {

		$delcnt = DB::delete($this->_table, DB::field($this->_pk, $id)); // 返回删除记录数量,若为0表示删除失败

		if ($delcnt == 0) {
			$arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($arr));

	}

	// 根据多个id删除多条记录
	// 多个待删除id字符串的格式是: __6__78
	public function delete_by_mult_id($mult_del_id) {
		//从第2个字符开始取至最后
		$mult_del_id = substr($mult_del_id, 2);

		$id_arr = explode('__', $mult_del_id);
		foreach($id_arr as $value){
			// 返回删除记录数量,若为0表示删除失败
			$delcnt = DB::delete($this->_table, DB::field($this->_pk, $value)); 
		}

		if ($delcnt == 0) {
			$res_arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$res_arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($res_arr));
	}

	// 根据id 编辑一条数据
	// 是否可以换种方式?
	// 参考 https://www.cnblogs.com/yehuisir/p/12830858.html
	public function update_by_id($ID, $form_data) {

		$ret = DB::update($this->_table, $form_data,  "ID=$ID");

		$logfile = fopen("log.txt", "w") or die("Unable to open file!");  
		fwrite($logfile, 'ret:'.$ret);  
		fclose($logfile); 

		if ($ret == 0) {
			$arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($arr));

	}

	// 编辑多条记录
	// 此处传来的 $upd_data 是多行数据转为json格式字符串
	// 参考 https://www.cnblogs.com/yehuisir/p/12830858.html
	public function update_mult_rows($upd_data) {

		// json字符串转为对象数组
		$arr_rows = json_decode($upd_data);
		//$ret = DB::update($this->_table, $arr_rows[0],  "ID=$ID");

		foreach($arr_rows as $obj){
			 //$res_str =  "id:".$obj->ID." HZDQ:".$obj->C_HZDQ;
			 $id = $obj->id;
			 $ret = DB::update($this->_table, $obj,  "id=$id");
		}

		//$ret = DB::update($this->_table, $form_data,  "ID=$ID");

		//$logfile = fopen("log.txt", "w") or die("Unable to open file!");  
		//fwrite($logfile, 'ret:'.$ret);  
		//fclose($logfile); 

		if ($ret == 0) {
			$res_arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$res_arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($res_arr));
		//return urldecode('update_mult_rows ok:'.$res_str) ;//+ strlen($upd_data);

	}

	// 插入一条数据
	public function insert_data($form_data, $return_insert_id, $replace, $silent) {

		$ret = DB::insert($this->_table, $form_data, $return_insert_id, $replace, $silent);

		if ($ret == 0) {
			$arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($arr));

	}

	// 插入多条数据
	// 传入参数 ins_data 是json字符串
	public function insert_mult_rows($ins_data) {

		//$ret = DB::insert($this->_table, $form_data, $return_insert_id, $replace=False, $silent=True);

		// json字符串转为对象
		$arr_rows = json_decode($ins_data);
		foreach($arr_rows as $obj){
			//$res_str =  "C_HZDQ:".$obj->C_HZDQ;
			//$res_str = json_encode($obj);
			$ret = DB::insert($this->_table, $obj, true);
		}

		 json字符串转为数组
		//$arr_rows = json_decode($ins_data, true);
		//$i = 0;
		//foreach($arr_rows as $obj){
			//$i = $i + 1;
			 数组转字符串: implode('',$obj)
			//$res_str =  implode('',$obj);
			//$ret = DB::insert($this->_table, $obj, $return_insert_id=True, $replace=False, $silent=True);
		//}

		if ($ret == 0) {
			$arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($arr));

	}


}

?>

source\class\table\table_cat_sys_values.php,用于取出下拉列表内容:

<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *      $Id: table_cat_sys_values.php
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_cat_sys_values extends discuz_table
{
	public function __construct() {

		$this->_table = 'cat_sys_values';
		$this->_pk    = 'id';

		parent::__construct();
	}

	public function fetch_all_by_displayorder() {
		return DB::fetch_all('SELECT * FROM %t ORDER BY id ASC', array($this->_table), $this->_pk);
	}

	public function fetch_all_by_sql($start, $limit) {
		return DB::fetch_all('SELECT * FROM %t ORDER BY id ASC'.DB::limit($start, $limit), array($this->_table), $this->_pk);
	}
	
	//返回json格式数据
	public function fetch_json_by_sql($start, $limit, $m_conds) {
		$query = DB::query('SELECT * FROM %t WHERE 1=1 '.$m_conds.' ORDER BY id ASC'.DB::limit($start, $limit), array($this->_table), $this->_pk);
		
		$result = array();
		
		// 记录数量
		$row = DB::fetch_first('SELECT count(*) as cnt FROM %t WHERE 1=1 '.$m_conds, array($this->_table), $this->_pk); //所有记录数量
		$result["total"] = intval($row['cnt']);

		while($data = DB::fetch($query)){
			$list[] = array(  
              'list_name_en' => urlencode($data['list_name_en']),
              'list_name_cn' => urlencode($data['list_name_cn']),
              'list_value_en' => urlencode($data['list_value_en']),
              'parent_value_en' => urlencode($data['parent_value_en']),
              'value_order' => urlencode($data['value_order'])

			);  
		}  
		$result["rows"] = $list;
  
		return urldecode(json_encode($result));  
	}

	//返回用于下拉列表所用的json格式数据,不分页,
	public function fetch_json_for_combobox($list_name) {
		$query = DB::query('SELECT * FROM %t WHERE list_name_en=\''.$list_name.'\' ORDER BY value_order ASC', array($this->_table), $this->_pk);
		
		$result = array();
		
		// 记录数量
		$row = DB::fetch_first('SELECT count(*) as cnt FROM %t WHERE list_name_en=\''.$list_name.'\'', array($this->_table), $this->_pk); //所有记录数量
		$result["total"] = intval($row['cnt']);

		while($data = DB::fetch($query)){
			$list[] = array(  
              //'list_value_en' => urlencode($data['list_value_en']),
              //'list_value_cn' => urlencode($data['list_value_cn']),
              'c_csid' => urlencode($data['list_value_en']),
              'c_cs' => urlencode($data['list_value_cn']),
			);  
		}  
		$result["rows"] = $list;
  
		//return urldecode(json_encode($result)); 
		return urldecode(json_encode($list)); 
	}

	//public function fetch_by_id_username($id, $username, $adminid = 1) {
		//return DB::fetch_first('SELECT * FROM %t WHERE id=%d', array($this->_table, $id, $adminid, $username));
	//}

	// 根据一个id删除一条记录
	public function delete_by_id($id) {

		$delcnt = DB::delete($this->_table, DB::field($this->_pk, $id)); // 返回删除记录数量,若为0表示删除失败

		if ($delcnt == 0) {
			$arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($arr));

	}

	// 根据多个id删除多条记录
	// 多个待删除id字符串的格式是: __6__78
	public function delete_by_mult_id($mult_del_id) {
		//从第2个字符开始取至最后
		$mult_del_id = substr($mult_del_id, 2);

		$id_arr = explode('__', $mult_del_id);
		foreach($id_arr as $value){
			// 返回删除记录数量,若为0表示删除失败
			$delcnt = DB::delete($this->_table, DB::field($this->_pk, $value)); 
		}

		if ($delcnt == 0) {
			$res_arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$res_arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($res_arr));
	}

	// 根据id 编辑一条数据
	// 是否可以换种方式?
	// 参考 https://www.cnblogs.com/yehuisir/p/12830858.html
	public function update_by_id($ID, $form_data) {

		$ret = DB::update($this->_table, $form_data,  "ID=$ID");

		$logfile = fopen("log.txt", "w") or die("Unable to open file!");  
		fwrite($logfile, 'ret:'.$ret);  
		fclose($logfile); 

		if ($ret == 0) {
			$arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($arr));

	}

	// 编辑多条记录
	// 此处传来的 $upd_data 是多行数据转为json格式字符串
	// 参考 https://www.cnblogs.com/yehuisir/p/12830858.html
	public function update_mult_rows($upd_data) {

		// json字符串转为对象数组
		$arr_rows = json_decode($upd_data);
		//$ret = DB::update($this->_table, $arr_rows[0],  "ID=$ID");

		foreach($arr_rows as $obj){
			 //$res_str =  "id:".$obj->ID." HZDQ:".$obj->C_HZDQ;
			 $ID = $obj->ID;
			 $ret = DB::update($this->_table, $obj,  "ID=$ID");
		}

		//$ret = DB::update($this->_table, $form_data,  "ID=$ID");

		//$logfile = fopen("log.txt", "w") or die("Unable to open file!");  
		//fwrite($logfile, 'ret:'.$ret);  
		//fclose($logfile); 

		if ($ret == 0) {
			$res_arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$res_arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($res_arr));
		//return urldecode('update_mult_rows ok:'.$res_str) ;//+ strlen($upd_data);

	}

	// 插入一条数据
	public function insert_data($form_data, $return_insert_id, $replace, $silent) {

		$ret = DB::insert($this->_table, $form_data, $return_insert_id, $replace, $silent);

		if ($ret == 0) {
			$arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($arr));

	}

	// 插入多条数据
	// 传入参数 ins_data 是json字符串
	public function insert_mult_rows($ins_data) {

		//$ret = DB::insert($this->_table, $form_data, $return_insert_id, $replace=False, $silent=True);

		// json字符串转为对象
		$arr_rows = json_decode($ins_data);
		foreach($arr_rows as $obj){
			//$res_str =  "C_HZDQ:".$obj->C_HZDQ;
			//$res_str = json_encode($obj);
			$ret = DB::insert($this->_table, $obj, true);
		}

		 json字符串转为数组
		//$arr_rows = json_decode($ins_data, true);
		//$i = 0;
		//foreach($arr_rows as $obj){
			//$i = $i + 1;
			 数组转字符串: implode('',$obj)
			//$res_str =  implode('',$obj);
			//$ret = DB::insert($this->_table, $obj, $return_insert_id=True, $replace=False, $silent=True);
		//}

		if ($ret == 0) {
			$arr = array ('msg' => 'wrong!', 'success' => false);
		}
		else {
			$arr = array ('msg' => 'ok!', 'success' => true);
		}

		return urldecode(json_encode($arr));

	}


}

?>

source\module\cat\cat_gys_crud_list_edit.php:

<?php
// mod文件只能被入口文件引用,不能直接访问
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

//初始化当前页码
$page = empty($_GET['page'])?1:intval($_GET['page']);
if($page<1) $page=1;

//分页
$perpage = 20;
$start = ($page-1)*$perpage;

//获取当前页的留言数据
//$cat_gys_list = array();
//$query = DB::query("SELECT * FROM ".DB::table('cat_gys')." WHERE 1 ORDER BY ID DESC LIMIT $start, $perpage");


$gtmj = DB::fetch($query);


//while($cat_gys = DB::fetch($query)) {
	$gtmj['ID'] = dgmdate($mood['dateline'], 'u');
	//$cat_gys_list[] = $cat_gys;
//}

//print_r($gtmj_list);
//print(count($gtmj_list));
//die();


//获得一个简单的分页,只有上一页和下一页,这个不需要count()数据表中的所有记录
//$multi = simplepage(count($gtmj_list), $perpage, $page, 'gtmj.php?mod=gtmj_list');

//数据准备完毕,引入相应的模板,准备输出
//include_once template("cat/cat_gys_crud_list");
//include_once template("cat/list_test");
if ($_GET['page']){
	$page = empty($_GET['page']) ? 1 : intval($_GET['page']);
	if($page<1) $page=1;
	$perpage = empty($_GET['rows']) ? 20 : intval($_GET['rows']);
	$start = ($page-1)*$perpage;

	// 多个查询条件
	$m_conds = '';

		// 字符型字段
	if (trim($_GET['c_dh']) != ''){
		$a_cond0 = trim($_GET['c_dh']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_dh '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_dh)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_gj']) != ''){
		$a_cond0 = trim($_GET['c_gj']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_gj '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_gj)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 数字型字段
	if (trim($_GET['c_yzbm']) != ''){
		$a_cond0 = trim($_GET['c_yzbm']);
		if ((substr_count($a_cond0, '=') > 0)
			or (substr_count($a_cond0, '<') > 0) 
			or (substr_count($a_cond0, '>') > 0)){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_yzbm '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (c_yzbm='.$a_cond0.')';
			
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_dq']) != ''){
		$a_cond0 = trim($_GET['c_dq']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_dq '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_dq)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_csid']) != ''){
		$a_cond0 = trim($_GET['c_csid']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_csid '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_csid)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_cs']) != ''){
		$a_cond0 = trim($_GET['c_cs']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_cs '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_cs)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_dz']) != ''){
		$a_cond0 = trim($_GET['c_dz']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_dz '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_dz)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_lxrzw']) != ''){
		$a_cond0 = trim($_GET['c_lxrzw']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_lxrzw '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_lxrzw)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_lxrxm']) != ''){
		$a_cond0 = trim($_GET['c_lxrxm']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_lxrxm '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_lxrxm)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_gsmc']) != ''){
		$a_cond0 = trim($_GET['c_gsmc']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_gsmc '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_gsmc)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 数字型字段
	if (trim($_GET['c_gysid']) != ''){
		$a_cond0 = trim($_GET['c_gysid']);
		if ((substr_count($a_cond0, '=') > 0)
			or (substr_count($a_cond0, '<') > 0) 
			or (substr_count($a_cond0, '>') > 0)){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_gysid '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (c_gysid='.$a_cond0.')';
			
		}
		$m_conds .= $a_cond1;
	}
	

	// 数字型字段
	if (trim($_GET['id']) != ''){
		$a_cond0 = trim($_GET['id']);
		if ((substr_count($a_cond0, '=') > 0)
			or (substr_count($a_cond0, '<') > 0) 
			or (substr_count($a_cond0, '>') > 0)){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (id '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (id='.$a_cond0.')';
			
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_cz']) != ''){
		$a_cond0 = trim($_GET['c_cz']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_cz '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_cz)>0)';
		}
		$m_conds .= $a_cond1;
	}
	

	// 字符型字段
	if (trim($_GET['c_zy']) != ''){
		$a_cond0 = trim($_GET['c_zy']);
		if (
			//(substr_count($a_cond0, 'like') > 0)
			(substr_count($a_cond0, 'in') > 0) 
			or (substr_count($a_cond0, '=') > 0)
			){ 
			// 判断是否含逻辑运算符,如果含有则直接用,如果不含有,则加上=
			$a_cond1 = ' and (c_zy '.$a_cond0.')';
		}else{
			$a_cond1 = ' and (LOCATE(\''.$a_cond0.'\',c_zy)>0)';
		}
		$m_conds .= $a_cond1;
	}

	$get_json = C::t('cat_gys')->fetch_json_by_sql($start, $perpage, $m_conds);
	echo $get_json;
	exit;
}
elseif ($_GET['ac']=='del'){
	$json_del_id = $_GET['del_id'];
	$del_res = C::t('cat_gys')->delete_by_mult_id($json_del_id);
	echo $del_res;
	exit;	
}
elseif ($_GET['ac']=='upd'){
	$upd_data = $_GET['upd_data'];
	//$del_res = C::t('cat_gys')->delete_by_mult_id($json_del_id);
	$upd_res = C::t('cat_gys')->update_mult_rows($upd_data);
	echo $upd_res;
	exit;	
}
elseif ($_GET['ac']=='ins'){
	$ins_data = $_GET['ins_data'];
	//$del_res = C::t('cat_gys')->delete_by_mult_id($json_del_id);
	$ins_res = C::t('cat_gys')->insert_mult_rows($ins_data);
	echo $ins_res;
	exit;	
}
else{
	//print_r($gtmj_list);
	//print(count($gtmj_list));
	//die();


	//获得一个简单的分页,只有上一页和下一页,这个不需要count()数据表中的所有记录
	//$multi = simplepage(count($gtmj_list), $perpage, $page, 'gtmj.php?mod=gtmj_list');

	//数据准备完毕,引入相应的模板,准备输出
	//if ($_GET['ac']=='sh'){
	  //include_once template("cat/cat_gys_crud_list_show");
	//}elseif ($_GET['ac']=='ed'){
	  include_once template("cat/cat_gys_crud_list_edit");
	//}
	//include_once template("cat/list_test");
}
?>

template\default\cat\cat_gys_crud_list_edit.htm:

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Fluid Layout - jQuery EasyUI Demo</title>
	<link rel="stylesheet" type="text/css" href="../../../template/default/style/cat/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../../../template/default/style/cat/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../../../template/default/style/cat/themes/demo.css">
	<script type="text/javascript" src="../../../template/default/style/cat/jquery.min.js"></script>
	<script type="text/javascript" src="../../../template/default/style/cat/jquery.easyui.min.js"></script>
</head>
<body>
	<div style="margin:20px 0;"></div>
	<div class="easyui-layout" style="width:100%;height:550px;">
		<div id="p" data-options="region:'west'" title="菜单" style="width:10%;padding:10px">
			<p>width: 30%</p>
		</div>
		<div data-options="region:'center'" title="">

			<table id="dg" class="easyui-datagrid" title="供应商" style="width: 100%;height: 100%"
					data-options="singleSelect:true,collapsible:true,url:'cat_cat_gys_crud_entr_edit.php?page=1',
					toolbar: '#tb',
					rownumbers:'true',
					pagination:'true',
					idField:'id',
					method: 'get',
					onClickRow: onClickRow
				">
				<thead>
					<tr>
<th data-options="field:'id',width:80">编号</th>
<th data-options="field:'c_yzbm',width:80, editor:'textbox'">邮政编码</th>
<th data-options="field:'c_dq',width:80, editor:'textbox'">地区</th>
<th data-options="field:'c_csid',width:80, editor:'textbox', 
						formatter:function(value,row){
							return row.c_cs || value;
						},
						editor:{
							type:'combobox',
							options:{
								valueField:'c_csid',
								textField:'c_cs',
								method:'get',
								required:true,
								url: 'cat_get_values.php?list_name_en=csid'
							}
						}">城市id</th>

					</tr>
				</thead>
			</table>

<div id="tb" style="height:auto">
<span>电话:</span><input id="c_dh" style="line-height:26px;border:1px solid #ccc">
<span>国家:</span><input id="c_gj" style="line-height:26px;border:1px solid #ccc">
<span>邮政编码:</span><input id="c_yzbm" style="line-height:26px;border:1px solid #ccc">
<span>地区:</span><input id="c_dq" style="line-height:26px;border:1px solid #ccc">

				<a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">搜索</a><br>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">增加</a>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">删除</a>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">保存</a>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">取消</a>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="getChanges()">信息</a>
 </div>


	<script type="text/javascript">
		//编辑的行
		var editIndex = undefined;

		function getRowIndex(target){
			
			var tr = $(target).closest('tr.datagrid-row');
			return parseInt(tr.attr('datagrid-row-index'));
		}

		function endEditing() {
			//alert('a');
			if (editIndex == undefined){return true}
			if ($('#dg').datagrid('validateRow', editIndex)){
				
				var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'c_csid'});
				var c_cs = $(ed.target).combobox('getText');
				//alert(c_cs);
				//alert($('#dg').datagrid('getRows')[editIndex]['c_cs']);
				$('#dg').datagrid('getRows')[editIndex]['c_cs'] = c_cs;//c_cs;
				$('#dg').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}

	  //function onClickCell(index, field){
	   //if (editIndex != index) {
		//if (endEditing()) {
		 //$('#dg').datagrid('selectRow', index)
		   //.datagrid('beginEdit', index);
		 //var ed = $('#dg').datagrid('getEditor', {index: index, field: field});
		 //if (ed) {
		  //($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
		 //}
		 //editIndex = index;
		//} else {
		 //setTimeout(function () {
		  //$('#dg').datagrid('selectRow', editIndex);
		 //}, 0);
		//}
	   //}
	  //}

	  //function onEndEdit(index, row){
	   //var ed = $(this).datagrid('getEditor', {
		//index: index,
		//field: 'c_csid'
	   //});
	   //row.c_cs = $(ed.target).combobox('getText');
	  //}

		function onClickRow(index){
			//alert('b');
			if (editIndex != index){
				if (endEditing()){
					$('#dg').datagrid('selectRow', index)
							.datagrid('beginEdit', index);
					editIndex = index;
				} else {
					$('#dg').datagrid('selectRow', editIndex);
				}
			}
		}


  function append(){
   var index = $('#dg').datagrid('getRowIndex', $('#dg').datagrid('getSelected'));
   if (index == -1)
    index = 0;
   $("#dg").datagrid("insertRow", {
    index: index+1,
	row: {}
    //row: {oper: "<a href='javascript:append()'>+<a> <a href='javascript:removeit()'>-<a>",status:'P'}
    });
  }


  function removeit(){
   if (editIndex != undefined){
	   $('#dg').datagrid('selectRow', editIndex);
	 
		$('#dg').datagrid('cancelEdit', editIndex)
		 .datagrid('deleteRow', editIndex);
	   editIndex = undefined;
   }
  }


  function accept(){
   if (endEditing()){
    var dg = $('#dg');
    var rows = dg.datagrid('getChanges');
    if (rows.length) {
       var inserted = dg.datagrid('getChanges', "inserted");
       var deleted = dg.datagrid('getChanges', "deleted");
       var updated = dg.datagrid('getChanges', "updated");
       var effectRow = new Object();
       if (inserted.length) {
           effectRow["inserted"] = JSON.stringify(inserted);
       }
       if (deleted.length) {
           effectRow["deleted"] = JSON.stringify(deleted);
       }
       if (updated.length) {
           effectRow["updated"] = JSON.stringify(updated);
       }

//console.log(inserted);
//console.log(deleted);
//console.log(updated);

		// 删除多行 ----
		if (deleted.length>0){
			$.messager.confirm('请确认','您确定要删除记录?',function(r){
				if (r){

					// 取得待删除多行的ID字符串 ---
					//jsonDataStr = JSON.stringify(deleted);
					//len = jsonDataStr.length;
					len = deleted.length;
					str_id = '';
					//arr_id = []
					for (var i = 0; i < len; i++) {
						str_id = str_id + '__' + deleted[i]['id'];
						//arr_id[i] = deleted[i]['ID'];
					};
					//json_del_str = JSON.stringify(arr_id);
					//console.log(str_id)
					// 取得待删除多行数据的ID字符串 ===

					//$.post('/cat_cat_gys_crud_entrance.php?ac=de', {"data": str_id},function(result){
					$.post('/cat_cat_gys_crud_entr_edit.php?ac=del', {"del_id": str_id},function(result){
						if (result.success){
							$('#dg').datagrid('reload');
						} else {
							$.messager.show({
								title: '出错啦',
								msg: result.errorMsg
							});
						}
					},'json');
				}
			});
		}
		// 删除多行 ====

		// 编辑多行 ----
		if (updated.length>0){
			$.messager.confirm('请确认','您确定要保存编辑的记录?',function(r){
				if (r){

					// 取得待编辑的多行记录 ---
					jsonDataStr = JSON.stringify(updated);
					//len = jsonDataStr.length;
					//len = deleted.length;
					//str_id = '';
					//arr_id = []
					//for (var i = 0; i < len; i++) {
						//str_id = str_id + '__' + deleted[i]['ID'];
						//arr_id[i] = deleted[i]['ID'];
					//};
					//json_del_str = JSON.stringify(arr_id);
					//console.log('aaa:' + jsonDataStr)
					// 取得待编辑的多行记录 ===

					//$.post('/cat_cat_gys_crud_entrance.php?ac=de', {"data": str_id},function(result){
					$.post('/cat_cat_gys_crud_entr_edit.php?ac=upd', {"upd_data": jsonDataStr},function(result){
						if (result.success){
							$('#dg').datagrid('reload');
						} else {
							$.messager.show({
								title: '出错啦',
								msg: result.errorMsg
							});
						}
					},'json');
				}
			});
		}
		// 编辑多行 ====

		// 插入多行 ----
		if (inserted.length>0){ // inserted updated
			$.messager.confirm('请确认','您确定要保存新增的记录?',function(r){
				if (r){

					// 取得待新增的多行记录 ---
					jsonDataStr = JSON.stringify(inserted);
					//len = jsonDataStr.length;
					//len = deleted.length;
					//str_id = '';
					//arr_id = []
					//for (var i = 0; i < len; i++) {
						//str_id = str_id + '__' + deleted[i]['ID'];
						//arr_id[i] = deleted[i]['ID'];
					//};
					//json_del_str = JSON.stringify(arr_id);
					//console.log('ins:' + jsonDataStr)
					// 取得待编辑的多行记录 ===

					//$.post('/cat_cat_gys_crud_entrance.php?ac=de', {"data": str_id},function(result){
					$.post('/cat_cat_gys_crud_entr_edit.php?ac=ins', {"ins_data": jsonDataStr},function(result){
						if (result.success){
							$('#dg').datagrid('reload');
						} else {
							$.messager.show({
								title: '出错啦',
								msg: result.errorMsg
							});
						}
					},'json');
				}
			});
		}
		// 插入多行 ====

    }
   }
  }


  function reject(){
   $('#dg').datagrid('rejectChanges');
   editIndex = undefined;
  }

  function getChanges(){
   var rows = $('#dg').datagrid('getChanges');
   alert(rows.length+' 行数据待保存!');
  }

   function contains(arr, obj) {
   var i = arr.length;
   while (i--) {
    if (arr[i] === obj) {
     return true;
    }
   }
   return false;
  }

		//function updateActions(index){
			//$('#dg').datagrid('updateRow',{
				//index: index,
				//row:{}
			//});
		//}
	
	function doSearch(){
		$('#dg').datagrid('load',{
			c_dh: $('#c_dh').val(),

c_gj: $('#c_gj').val(),

c_yzbm: $('#c_yzbm').val(),

c_dq: $('#c_dq').val(),

c_csid: $('#c_csid').val(),

c_cs: $('#c_cs').val(),

c_dz: $('#c_dz').val(),

c_lxrzw: $('#c_lxrzw').val(),

c_lxrxm: $('#c_lxrxm').val(),

c_gsmc: $('#c_gsmc').val(),

c_gysid: $('#c_gysid').val(),

id: $('#id').val(),

c_cz: $('#c_cz').val(),

c_zy: $('#c_zy').val(),


		});
	}

	</script>

		</div>
	</div>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值