简体   繁体   中英

How can I use “paginate ()” and checklist at the same time with Laravel?

I have a table in which data must be entered, but when using paginate() when I try to store the data, only the current page is stored and not the one previously edited. It only happens with checkboxes.

public function update(Request $request)
{
    $pensums = sizeof($request->input('course_id'));
    $id = $request->input('grade_id');
    $course_id = $request->input('course_id');

    $data = Pensum::where('grade_id', $id)
        ->update([
            'status' => 'INACTIVE',
        ]);

    for ($i = 0; $i < $pensums; $i++) {
        $data = Pensum::where('grade_id', $id)
            ->where('course_id', $course_id[$i])
            ->update([
                'status' => 'ACTIVE',
            ]);
    }

    $grade_id = $request->input('grade_id');

    return redirect()->action('PensumController@detail', ['grade_id' => $grade_id])
        ->with(['status' => 'Successful upgrade']);
}

When you use paginate Laravel refresh the page, so, Your checked checkboxes no more remain checked.

Your input tag for checkbox should be as below

<input value="" type="checkbox" name="" class="mobile_hide" 
 onchange="loadingIdChange(this,'You have the pass id here')" '+(isChecked ? 'checked' : '')+'/>

You need to check the array selectedCheckboxes.

var isChecked = isIdAlreadyAdded(data[0],selectedCheckboxes); 

You should use jQuery to store your checked checkboxes to an array.

var selectedCheckboxes = []; //Array where you store checked checkbox IDs.

This function will call whenever checkbox checked or unchecked.

function checkboxChange(checkbox, id){
     if(checkbox.checked == true){
            if(!isIdAlreadyAdded(id,selectedCheckboxes)){
                selectedCheckboxes.push(id);
            }
        }else{
            removeIfExits(id,selectedCheckboxes);
       }
}

This function will check is ID in array selectedCheckboxes.

function isIdAlreadyAdded(id,arr){
        for(var i=0;i<arr.length;i++){
            if(arr[i] == id){
                return true;
            }
        }
        return false;
    }

This function for removing ID if checkbox status is unchecked.

function removeIfExits(id,arr){
        for(var i=0;i<arr.length;i++){
            if(arr[i] == id){
                delete arr[i];
                return;
            }
        }
    }

Hope this will be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM