data-checkbox data-click-to-select not working reactjs - react-bootstrap

This code performs a render of a bootstrap table. In this table there are 2 columns with data and a total of 4 rows. I wish to have a checkbox at the side before the first column of data to select the entire row of data. I have tried using "data-click-to-select" and "data-checkbox", but no check box appears. What should I do to enable the selection of data by row?
Not sure if its because I'm not importing any react-bootstrap modules.
import axios from 'axios';
import React, { Component } from 'react';
import { table } from 'react-bootstrap'; // this is reflected as an unused variable
This is the example I tried to follow to get the checkbox: https://live.bootstrap-table.com/example/methods/get-selections.html
render() {
const obj = (this.state.message);
const datamapping = Object.entries(this.state.message);
return (
<div>
<div className="viewall">
<table class="table table-hover" data-click-to-select="true">
<thead>
<th data-checkbox="true"></th>
<th scope="col">key1</th>
<th scope="col">key2</th>
</thead>
<tbody>
{obj.Items?.map((data, key) => {
return (
<tr key={key}>
<td></td>
<td>{data.key1}</td>
<td>{data.key2}</td>
</tr>
)
})}
</tbody>
</table>
</div>
</div>
);
}

Found a workaround solution that does not hinge on data-checkbox or data-click-to-select - by clicking on a button that is present on each row and sending the key (array number) together with the data over.
handleClick(obj, i) {
//this.props.history.push("/signIn.js");
console.log(obj.Items[i]);
}
render() {
const obj = (this.state.message);
const datamapping = Object.entries(this.state.message);
return (
<div>
<div className="viewall">
<table class="table table-hover">
<thead>
<th data-checkbox="true"></th>
<th scope="col">key1</th>
<th scope="col">key2</th>
</thead>
<tbody>
{obj.Items?.map((data, key) => {
return (
<tr key={key}>
<td>
<button type="button" class="btn btn-primary" onClick={ () => this.handleClick(obj, key)}>
Sign In
</button>
</td>
<td>{data.key1}</td>
<td>{data.key2}</td>
</tr>
)
})}
</tbody>
</table>
</div>
</div>
);
}

Related

when click checkbox update one column as 1 and other as 0 in laravel

$addresses = Address::findorFail($addressesId);
if( $request->has('default_address')) {
Address::where('id', $addressesId)->update([
'default_address' => '1',
// 'default_address' => '0',
]);
}
this is my controller,
Route::post('/change/{addressesId}', 'DemoUserController#changeAddress');
this is my route
what i want is, i inserted various types of addresses and in the index page i display addresses of various users and checked the default address . suppose a user want to change address the user can check the next checkbox then it will be updated as 1 and the previous should be updated as 0 .I did when click the checkbox it updated as 1 its ok but the previous wont updated .how can I solve this please help.
<table class="table table-bordered table-striped" id="address_datatable" width="100%">
<thead>
<tr>
<th>Address</th>
<th>Address Type</th>
<th>City</th>
<th>State</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($demouser->addresses as $address)
<tr>
<td>{{$address->address}}</td>
<td>{{$address->address_type}}</td>
<td>{{$address->city}}</td>
<td>{{$address->state}}</td>
#if($address->default_address == '1')
<td><input type="checkbox" checked></td>
#else
<form method="POST" action="/change/{{$address->id}}">
#csrf
<td><input type="checkbox" name="default_address"></td>
<button class="btn btn-primary" style="position:
absolute;top:160px;right:30px">Save</button>
</form>
#endif
</tr>
#endforeach
</tbody>
</table>
my view
your question is not very clear but you can update the address without selecting it twice
just select it and update the record like this
$address = Address::findorFail($addressesId);
if( $request->has('default_address')) {
if($request->default_address == 'on'){
$address->default_address = 1;
$address->save();
}
}

How to show checkbox selected values in the same laravel view before submitting the form?

I have create.blade.php view. And that view showing a table with checkbox in each row. Now I want to show the checked row on the same view next to this table. Till now what I am getting the all the checked values but how can show it the same view before sending any post request to the server.
Here is some code-
#if(count($movies) > 0)
<div class="table-responsive">
<table class="table table-striped table-hover" data-toggle="dataTable"
data-form="deleteForm">
<thead>
<tr>
<th></th>
<th scope="col">Name</th>
</tr>
</thead>
<tbody>
<label>Select movies</label>
#foreach($movies as $movie)
<tr>
<td>
<input class="form-check" type="checkbox" name="movie_cb" id=" .
{{$movie->id}}" value="{{$movie->name}}">
</td>
<td>{{$infeedVideo->name}}</td>
</tr>
#endforeach
</tbody>
</table>
#else
<div class="text-center">
<hr>
<p class="text-center">No movies found</p>
</div>
#endif
app.blade.php
<script type="text/javascript">
$(document).ready(function() {
$("#showCBs").click(function(){
var favorite = [];
$.each($("input[name='movie_cb']:checked"), function(){
favorite.push($(this).val());
});
alert("My favourite movies are: " + favorite.join(", "));
});
});
</script>
Now how can I pass the favorite array from app.blade.php to my laravel view create.blade.php. Thank you

Cannot use v-for on stateful component root element because it renders multiple elements?

app.js
var Users = {
template: `
<tr v-for="list in UsersData">
<th>{{ list.idx }}</th>
<td>{{ list.id }}</td>
</tr>
`,
data: function () {
return {
UsersData //get data from query
}
}
}
var mainview = new Vue({
el: "#mainview",
components: {
'users': Users
},
method: {}
})
layout.blade.php
<body>
<div class="container">
<aside>...</aside>
<main id="mainview">
#section('content')
#show
</mainview>
</div>
</body>
index.blade.php
#extends('layout')
#section('content')
<table>
<thead>
<tr>
<th>IDX</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<users></users>
</tbody>
</table>
#endsection
ERROR LOG from chrome console
[Vue warn]: Cannot use v-for on stateful component root element because it renders multiple elements:
<tr v-for="list in UsersData">
<th>{{ list.idx }}</th>
<td>{{ list.id }}</td>
</tr>
vue.js:525 [Vue warn]: Multiple root nodes returned from render function. Render function should return a single root node.
(found in component )
How should I fix code?
Your template has to have one root element. It's just one rule you cannot break. Since you're making a table, it would make sense to make tbody the root element.
var Users = {
template: `
<tbody>
<tr v-for="list in UsersData">
<th>{{ list.idx }}</th>
<td>{{ list.id }}</td>
</tr>
</tbody>
`,
data: function () {
return {
UsersData //get data from query
}
}
}
index.blade.php
#extends('layout')
#section('content')
<table>
<thead>
<tr>
<th>IDX</th>
<th>ID</th>
</tr>
</thead>
<users></users>
</table>
#endsection
For the lazy: Vue interprets the root element having a v-for loop on it as producing multiple root level elements (a nono in modern JS frameworks).
Just wrap your template in a superfluous blank <div>. 👌
I struggled with this, until I realized you can just add a section tag over everything in template and then this error goes away.
Example
//gives error
<template>
<div classname="allFutures" v-for="(items, index) in arr">
<Future title="Shee" />
</div>
</template>
// do this instead
<template>
<section id="main" class="main-alt">
<div classname="allFutures" v-for="(items, index) in arr">
<Future title="Shee" />
</div>
</section>
</template>

Kendo Window won't load data from model

#model Example.Model1ViewModel
#if (Model != null && Model.TableSix != null)
{
<table>
<thead>
<tr>
<td style="width:60%"></td>
<td style="width:20%">Outstanding Amount</td>
</tr>
</thead>
<tbody>
<tr>
<td>Total</td>
<td class="Total cursor">#Html.DisplayFor(x => x.TableSix.Total)</td>
</tr>
<tr>
<td>One</td>
<td>#Html.DisplayFor(x => x.TableSix.One)</td>
</tr>
<tr>
<td>Two</td>
<td>#Html.DisplayFor(x => x.TableSix.Two)</td>
</tr>
</tbody>
</table>
}
<div style="display: none">
#(Html.Kendo().Window()
.Name("RemainingAssetsDrillDown")
.Modal(true)
.Title("Item Drill Down")
.Actions(actions => actions.Close())
.Content(#<text>
<div>
<table>
<tbody>
<tr>
<td>One</td>
<td>#Html.DisplayFor(x => x.TableSix.One)</td>
</tr>
</tbody>
</table>
</div>
</text>)
</div>
)
$(".Total").click(function () {
$("#MVC").load('#Url.Action("PartList", "Report")');
var win = $("#DrillDown").data("kendoWindow");
win.center().open();
});
<style type="text/css">
.cursor {
cursor: pointer;
}
</style>
So, the problem is that #Html.DisplayFor(x => x.TableSix.One) fetches the correct value from the view model in the first table, but in the table under kendo window I get the value as 0.
Any idea how I can get #Html.DisplayFor(x => x.TableSix.One) to display the value from the view model into the table that is under kendo window?
Thanks in advance. :)

Use all the elements of the table in Laravel

I have a table in the page "map.blade.index.php" as:
<table id="table">
<thead>
<tr><th>ID</th>
<th>Name</th>
<th>Latitude</th>
<th>Longitude</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td id="0">Name 174</td>
<td id="lat0">41.1230199</td>
<td id="lng0">14.73767010000006</td>
</tr>
</tbody>
</table>
and I try to use the table elements in my function Controller called "MapController#saved" , but I think that I can't use the table by her id.
there a way to use all the elements of the table ? Now I must use one item at a time.
You can parse your table with any HTML parser. I'm using this one.
$html->find('td[id=lat0]')->innertext; // Returns 41.1230199
If I understood then what you need is a multi input like:
<form action="map/saved">
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Latitude</th>
<th>Longitude</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td id="0">Name 174</td>
<td id="lat0">41.1230199</td>
<td id="lng0">14.73767010000006</td>
<input type="hidden" name="entry[0][name]" value="Name 174">
<input type="hidden" name="entry[0][lat]" value="41.1230199">
<input type="hidden" name="entry[0][lng]" value="14.73767010000006">
</tr>
</tbody>
</table>
<button type="submit">
</form>
Your controller method will looks like this:
public function map(Request $request) {
foreach ($request->input('entry') as $i => $entry) {
// here you get an array with:
// array (3) [
// 'name' => 'Name 174'
// 'lat' => '41.1230199'
// 'lng' => '14.73767010000006'
// ]
}
}

Resources