JavaScript--全選、全不選、反選、無重新整理刪除、批量刪除、即點即改入庫(在yii框架中操作)
<?php header("content-type:text/html;charset=utf-8"); use yii\helpers\Html; use yii\widgets\LinkPager; //print_r($countries);die; ?> <h1>顯示出資料</h1> <input type="checkbox" value="全選" onclick="check(this);">全選 <input type="checkbox" value="全不選" onclick="check_bx(this);">全不選 <input type="checkbox" value="反選" onclick="check_fx();">反選 <input type="checkbox" value="批量刪除" onclick="check_del();">批量刪除 <script src="public/jq.js"></script> <style media="screen"> tr{ background-color: red; font-family: 宋體; width: 100px; height: 30px; line-height: 30px; text-align: center; } td{ background-color: pink; border: 2px solid purple; } .aa{ border: 2px solid green; background-color: yellow; font-family: 隸書; } h1{ font-family: 華文行楷; box-shadow: 10px 10px 5px #888888; border:2px solid; border-radius:25px; width: 200px; background-color: white; } th{ font-family: 隸書; border: 2px solid green; background-color: yellow; } .checkbox{ width: 25px; height: 30px; } </style> <table border="1"> <th></th> <th>序列號</th> <th>父級ID</th> <th>地區名稱</th> <th>操作</th> <?php foreach ($countries as $k => $v) { ?> <tr> <td><input type="checkbox" id="<?php echo $v['r_id'] ?>" class="checkbox" name="check[]" value="<?php echo $v['r_id'] ?>"></td> <td><?php echo $v['r_id'] ?></td> <td><?php echo $v['pid'] ?></td> <td><span class="num" id="<?php echo $v['r_id'] ?>"><?php echo $v['r_name']?></span></td> <td><a href="javascript:void(0)" id="<?php echo $v['r_id'] ?>" class="aa" onclick="del(this)">刪除 </a></td> </tr> <?php } ?> </table> <?= LinkPager::widget(['pagination' => $pagination]) ?> <script type="text/javascript"> //即點即改入庫 $(function(){ $(document).on('click','.num',function(){ var id=$(this).attr('id'); var _this=$(this); var new_val=$(this).html(); _this.parent().html("<input type='text'class='asdf' value="+new_val+" id="+id+">"); var inp=$('.asdf'); inp.focus(); inp.blur(function(){ var old_id=$(this).attr('id'); var old_val=$(this).val(); //inp.parent().html("<span class=\"num\" id="+old_id+">"+old_val+"</span>"); $.get("index.php?r=upload/updates",{measure_unit:old_val,id:old_id},function(e){ if(e==1){ inp.parent().html("<span class=\"num\" id="+old_id+">"+old_val+"</span>"); }else{ inp.parent().html("<span class=\"num\" id="+old_id+">"+new_val+"</span>"); } }) }) }) }) </script> <script> //無重新整理刪除 function del(obj) { var ids=obj.id; ajax=new XMLHttpRequest(); ajax.onreadystatechange=function() { if(ajax.readyState==4) { //alert(ajax.responseText); if(ajax.responseText==1) { obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode); } else{ alert("刪除失敗"); } } } ajax.open("get","index.php?r=upload/del&ids="+ids); ajax.send(null); } /*全選*/ function check(obj) { var ids=document.getElementsByName("check[]"); if(obj.checked) { for(var i=0;i<ids.length;i++) { ids[i].checked=true; } } } /*全不選*/ function check_bx(obj) { var ids=document.getElementsByName("check[]"); if(obj.checked) { for(var i=0;i<ids.length;i++) { ids[i].checked=false; } } } //反選 function check_fx() { var ids=document.getElementsByName("check[]"); for(var i=0;i<ids.length;i++) { ids[i].checked=!ids[i].checked; } } /*批量刪除*/ function check_del() { var ids=document.getElementsByName("check[]"); var str=''; for(var i=0;i<ids.length;i++) { if(ids[i].checked) { str=str+','+ids[i].value; } } new_str=str.substr(1); ajax=new XMLHttpRequest(); ajax.onreadystatechange=function() { if (ajax.readyState == 4) { //alert(ajax.responseText); if(ajax.responseText==1) { for(var j=ids.length-1;j>=0;j--) { if(ids[j].checked) { ids[j].parentNode.parentNode.parentNode.removeChild(ids[j].parentNode.parentNode); } } } else { alert("刪除失敗"); } } } ajax.open("get","index.php?r=upload/del_all&new_str="+new_str); ajax.send(null); }
<?php namespace app\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\LoginForm; use app\models\ContactForm; use yii\web\UploadedFile; use app\models\Upload; use yii\data\Pagination; use app\models\Country; use app\models\Region; class UploadController extends Controller { //public $layout=false; //禁用yii自帶樣式 public function actionIndex(){ $model = new Upload(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { // 驗證 $model 收到的資料 // 做些有意義的事 ... return $this->render('entry-confirm', ['model' => $model]); } else { // 無論是初始化顯示還是資料驗證錯誤 return $this->render('entry', ['model' => $model]); } } public function actionAdds() { $model = new Upload(); $request = Yii::$app->request; $post=$request->post('Upload'); $u_name = $post['u_name']; $u_pwd = $post['u_pwd']; //在瀏覽器輸出的值是 yii\web\UploadedFile Object ( [name] => 2.jpg [tempName] => C:\Windows\php3986.tmp // [type] => image/jpeg [size] => 216848 [error] => 0 ) $arr = $model->u_img = UploadedFile::getInstance($model,'u_img'); //print_r($arr); if ($model->upload()){ $u_img = $arr->name; //var_dump($u_img); $connection = \Yii::$app->db; $result=$connection->createCommand()->insert('upload', [ 'u_name' => $u_name, 'u_pwd' => $u_pwd, 'u_img' =>$u_img, ])->execute(); if($result) { echo "新增成功"; } else { echo "新增失敗"; } } } /*分頁*/ public function actionLists() { $query = Country::find(); $pagination = new Pagination([ 'defaultPageSize' => 1, 'totalCount' => $query->count(), ]); $countries = $query->orderBy('name') ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('lists', [ 'countries' => $countries, 'pagination' => $pagination, ]); } /*地區表進行分頁*/ public function actionShow(){ $query = Region::find(); $pagination = new Pagination([ 'defaultPageSize' => 6, 'totalCount' => $query->count(), ]); $countries = $query->orderBy('r_id') ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('show', [ 'countries' => $countries, 'pagination' => $pagination, ]); } /*修改*/ public function actionUpdates(){ $name = $_GET['measure_unit']; $id = $_GET['id']; $connection = \Yii::$app->db; $command = $connection->createCommand("UPDATE region SET r_name='$name' WHERE r_id='$id'"); $command->execute(); if(!empty($command)){ echo 1; }else{ echo 0; } } // 無重新整理delete public function actionDel(){ $id=$_GET['ids']; $connection=\Yii::$app->db; $arr=$connection->createCommand("delete from region where r_id='$id'")->execute(); if($arr){ echo 1; }else{ echo 0; } } /* 批量刪除 @new_str GET */ public function actionDel_all(){ $new_str=$_GET['new_str']; $connection=\Yii::$app->db; $arr=$connection->createCommand("delete from region where r_id in($new_str)")->execute(); if($arr){ echo 1; }else{ echo 0; } } } ?>
另外一種方法:
相關文章
- jQuery中點選刪除,顯示是否要刪除jQuery
- vue.js單選全選刪除Vue.js
- ListView全選刪除CheckBoxView
- javascript實現 checkbox全選和批量刪除功能JavaScript
- vue 工作專案中 實現訊息列表的 全選,反選,刪除功能Vue
- 點選刪除彈出提示是否刪除程式碼
- collectionView--編輯全選刪除View
- iOS 多選刪除(附tableViewTips及單選刪除)iOSView
- 商品訂單(增刪改查):新增訂單;批量刪除,發貨;模糊查詢,下拉選單內容過濾(含時間);全選反選,列名排序排序
- Android列表實現單選、多選、全選、取消、刪除Android
- 簡單的js反選,全選,全不選JS
- 點選刪除按鈕彈出是否刪除提示框
- 點選刪除按鈕刪除當前行程式碼例項行程
- JavaScript點選新增行或者刪除行JavaScript
- JavaScript點選按鈕刪除div元素JavaScript
- jQuery點選按鈕刪除div元素jQuery
- Jquery實現頁面的新增、刪除、全選、取消全選、漸變色jQuery
- Android 中listview的全選、全不選、反選、獲得選中條目資料AndroidView
- 安卓開發:listview長按進入多選刪除操作安卓View
- 點選新增或者刪除表格行詳解
- 點選刪除表格行程式碼例項行程
- 點選退格鍵刪除整個單詞而不是逐個字元刪除字元
- dbca刪除資料庫時選項灰色資料庫
- 原生js多選框選中排序及刪除JS排序
- Win10系統刪除卷選項灰色無法刪除如何解決Win10
- jquery實現的全選和刪除功能外掛jQuery
- JavaScript點選按鈕刪除一個div元素JavaScript
- 28-Beego優選刪除商品Go
- c# listview 刪除選中子項C#View
- vue+element-ui操作刪除(單行和批量刪除)VueUI
- win10滑鼠右鍵選單欄怎麼改 刪除右鍵選單欄無用選項的方法Win10
- 刪除按鈕點選後的虛線輪廓
- 點選文字框刪除提示文字效果程式碼
- php(js)批量刪除/單個刪除PHPJS
- 資料庫的選定、建立、刪除和變更資料庫
- Yii 1.0資料庫操作 查詢、增加、更新、刪除資料庫
- 恢復被刪除的Word選單
- win10右鍵skydrive pro選項怎麼刪除_win10右鍵選單中skydrive pro選項的刪除方法Win10