React + Ag-grid + Webpack - reactjs

I am using ag-grid with react. And i am unable to display grid properly,
please find below my code,
import React, {Component} from 'react'
import {render} from 'react-dom'
import {AgGridReact} from 'ag-grid-react'
import Logo from './components/Logo/'
import Link from './components/Link/'
import Child from './child.jsx'
export default class App extends Component {
constructor(props){
super(props);
this.state = {
columnDefs: [
{headerName: "Name", field: "Name"},
{headerName: "Age", field: "Age"}
],
rowData: [
{"Name": "Man", "Age":"25"},
{"Name": "Pal", "Age":"27"}
]
}
}
onGridReady(params) {
this.gridApi = params.api;
this.columnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
}
render() {
return (
<div>
<p className="f5p"><img className="f5logo" src="./src/assets/index.png" alt="F5 Logo"></img></p>
<h2 className="f5h1">CTU Log Viewer</h2>
<ul className="f5ul">
<li className="f5li">Logs</li>
<li className="f5li">Command Logs</li>
<li className="f5li">Installed Packages</li>
<li className="f5liabout">About</li>
</ul>
<div className="ag-fresh">
<AgGridReact
columnDefs = {this.state.columnDefs}
rowData = {this.state.rowData}
onGridReady = {this.onGridReady}
/>
</div>
</div>
)
}
}
I am also using webpack, and not sure if webpack causing the issue.
I am using babel, css-loader modules in webpack.
The grid rows are not coming properly, data is displayed in vertical manner.
I am unable to understand what is the issue.
Any help is much appreciated.
Thanks in advance.

Related

How to read json file from my src folder in my react app using webpack-dev-server?

machine.json:
[
{
"id": "1",
"name": "Test",
"imageUrl": "../images/panda.jpg"
}
]
I have to read imageUrl from above json object and add it to src of img tag to see the image on UI.
React File:
import React, { useEffect, useState } from 'react'
import machineList from '../../resources/data/machine.json';
const Home = props => {
return (
<div className='home-component'>
{machineList.map(machine => {
return (<div className="card-wrapper" key={machine.id}>
<img src={machine.imageUrl} className="image" />
<div className="name">{machine.name}</div>
</div>)
})}
</div>
)
}
export default Home;
After rendering this component i am getting broken image.
Missing an explanation in the question but in general:
You have to import your json file:
import images from './json-file-path'
and:
<img src={images[0].imageUrl}></img>
Hope it helps.

React Bootstrap table not formatting

I am very new to react and am attempting to follow a basic table tutorial.
I have installed react-bootstrap-table
npm install react-bootstrap-table --save
I have imported bootstrap-table
import {BootstrapTable, TableHeaderColumn} from 'react-bootstrap-table';
I have imported the css file:
import '../../node_modules/react-bootstrap-table/dist/react-bootstrap-table-all.min.css';
With the above I am expecting a table to display as below:
However what I am seeing with my code is:
Why isn't the table appearing as in the example?
import React, { Component } from "react";
import './MenuButton.css';
import './MenuContainer.css';
import MenuButton from './MenuButton';
import Menu from './Menu';
import serverCall from "../api/ServerCall";
import {BootstrapTable,TableHeaderColumn} from 'react-bootstrap-table';
import '../../node_modules/react-bootstrap-table/css/react-bootstrap-table.css'
class MenuContainer extends Component {
constructor(props, context) {
super(props, context);
this.state = {
visible: false
};
this.handleMouseDown = this.handleMouseDown.bind(this);
this.toggleMenu = this.toggleMenu.bind(this);
this.getCustomers();
}
handleMouseDown(e) {
this.toggleMenu();
e.stopPropagation();
}
toggleMenu() {
this.setState({
visible: !this.state.visible
});
}
getCustomers(){
console.log("Getting Customers");
serverCall(function(error, response) {
if (error) {
alert("Authorization Failed.")
} else {
console.log(response);
}
},"","customers.php");
}
render() {
let products = [{
id: 1,
name: "Item name 1",
price: 100
},{
id: 2,
name: "Item name 2",
price: 100
}];
return (
<div>
<div className="menu-container">
<div className="grid-item-hamburger-menu">
<MenuButton handleMouseDown={this.handleMouseDown}/>
<Menu handleMouseDown={this.handleMouseDown} menuVisibility={this.state.visible}/>
</div>
<div className="grid-item-dashboard-title">
Dashboard | Customers
</div>
<div className="grid-item-search">
</div>
<div className="grid-item-table">
<BootstrapTable data={ products }>
<TableHeaderColumn dataField='id' isKey>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
</BootstrapTable>
</div>
</div>
</div>
);
}
}
export default MenuContainer;
I was able to solve this problem by including the following code in the index.js file. Not only did my table not have any formatting, my initial project did not as well.
import 'bootstrap/dist/css/bootstrap.min.css';
try this line
import '../../node_modules/react-bootstrap-table/css/react-bootstrap-table.css'
in main entry components
For future reference, you should only need to import from the base of the app and not the node_modules path. I use the following and it works fine:
import 'react-bootstrap-table/dist/react-bootstrap-table-all.min.css'
The snippets provided in the component examples fails to mention the need to install both the react-bootstrap module as well as bootstrap itself. Mentioned here: reactbootstrap getting started docs
TLDR: run npm install react-bootstrap bootstrap

How to make owl-carousel responsive in React?

I am using react-owl-carousel package.
https://www.npmjs.com/package/react-owl-carousel
I have successfully implemented the code as instructed and the carousel is running smoothly.
Problem : Currently I am displaying 4 items simultaneously. And in every screen , these 4 items are coming . Instead of 4 , I want to show 3 items for devices between 768px to 1200px , 2 items between 500px to 767px and 1 item for the devices below 499px.
The option of including "responsive" is there in owl carousel doc. But I am wondering How to include it to achieve the same.
Here is what I have done so far.
import React, { Component } from 'react';
import {Grid, Row, Col , ProgressBar } from 'react-bootstrap';
import UserAvtar from '../common/UserAvtar.js';
import SectionHeaderOfCards from '../common/SectionHeaderOfCards.js';
import OwlCarousel from 'react-owl-carousel';
const options = {
items: 4,
};
class DashboardPage extends Component {
render() {
return (
<div>
<section className="has-small__padding has-grey__bg">
<UserAvtar />
</section>
<section className="has-small__padding">
<Grid>
<SectionHeaderOfCards title="Recommended Matches" />
<OwlCarousel margin={10} >
<div class="item"><h4>1</h4></div>
<div class="item"><h4>2</h4></div>
<div class="item"><h4>3</h4></div>
<div class="item"><h4>4</h4></div>
<div class="item"><h4>5</h4></div>
<div class="item"><h4>6</h4></div>
</OwlCarousel>
</Grid>
</section>
</div>
);
}
}
export default DashboardPage;
You have to use OwlCarousel Options responsive.
Please check official documentation of owlcarousel2 API options to here.
For example use following options for your items state.
options:{
loop: true,
margin:10,
nav:true,
responsive:{
0:{
items:1
},
600:{
items:3
},
1000:{
items:5
}
}
},
Please check demo example to here.
Hope this will help you.
You can follow -
import OwlCarousel from 'react-owl-carousel';
import 'owl.carousel/dist/assets/owl.carousel.css';
const options = {
margin: 30,
responsiveClass: true,
nav: true,
dots: false,
autoplay: false,
navText: ["Prev", "Next"],
smartSpeed: 1000,
responsive: {
0: {
items: 1,
},
400: {
items: 1,
},
600: {
items: 2,
},
700: {
items: 3,
},
1000: {
items: 5,
}
},
};
class Slider extends Component {
render() {
return (
<div>
<OwlCarousel className="slider-items owl-carousel" {...options}>
...
</OwlCarousel>
</div>
);
}
}
export default Slider;
You can make owl-carousel responsive in React like this explained bellow:
Step 1: you need to create state in same component where you want owl-carousel....
Like you have slider.js component so you have to create state in same file ..Like this;
Step 2: And the state you created initialize in responsive property in owl-carousel
import OwlCarousel from 'react-owl-carousel';
import $ from 'jquery';
import 'owl.carousel/dist/assets/owl.carousel.css';
import 'owl.carousel/dist/assets/owl.theme.default.css';
class Slider extends Component {
state= {
responsive:{
0: {
items: 1,
},
450: {
items: 2,
},
600: {
items: 3,
},
1000: {
items: 4,
},
},
}
render() {
return (<OwlCarousel className={'owl-theme'}
loop={true}
margin={10}
nav={true}
dots={false}
autoplay={true}
autoplayTimeout={2000}
items={4}
responsive={this.state.responsive} >
<div className={'item'}>
Item 1
</div>
<div className={'item'}>
Item 2
</div>
<div className={'item'}>
Item 3
</div>
<div className={'item'}>
Item 4
</div>
<div className={'item'}>
Item 5
</div>
</OwlCarousel>
I was getting a type error in typescript, here is the version without type error :
<OwlCarousel
mouseDrag= {false} touchDrag={true}
stagePadding={0} margin={0} autoplay ={true} merge={true} nav dots={true} slideBy={2} dotsEach={1} loop={true}
responsive= {
{
'1':{
items: 1
},
'1025': {
items: 2
}
}
}
>
{reviews}
</OwlCarousel>
hope it helps : )

material-ui card with openlayers map inside?

I'm trying to place an openlayers map inside a material-ui card component. I've tried placing a div containing the map inside the card text and the card media sections.
Can anyone help me figure out the correct way to put a map inside the card?
import React, {PropTypes} from 'react'
import {connect} from 'react-redux'
import 'openlayers/dist/ol.css';
import ol from 'openlayers';
import {Card, CardActions, CardHeader, CardMedia} from 'material-ui/Card';
import FlatButton from 'material-ui/FlatButton';
import Paper from 'material-ui/Paper'
import baseTheme from 'material-ui/styles/baseThemes/lightBaseTheme'
import getMuiTheme from 'material-ui/styles/getMuiTheme'
import '../components/tap_events'
import styles from '../styles/ExportInfo.css'
import {updateExportInfo} from '../actions/exportsActions.js';
class ExportInfo extends React.Component {
constructor(props) {
super(props)
getChildContext() {
return {muiTheme: getMuiTheme(baseTheme)};
}
componentDidMount() {
this._initializeOpenLayers()
}
_initializeOpenLayers() {
const scaleStyle = {
background: 'white',
};
this._map = new ol.Map({
controls: [
new ol.control.ScaleLine(),
new ol.control.Attribution({
collapsible: false,
collapsed: false,
}),
new ol.control.Zoom({
className: styles.olZoom
})
],
interactions: ol.interaction.defaults({
keyboard: false,
altShiftDragRotate: false,
pinchRotate: false
}),
layers: [
// Order matters here
new ol.layer.Tile({
source: new ol.source.OSM()
}),
],
target: 'infoMap',
view: new ol.View({
projection: "EPSG:3857",
center: [110, 0],
zoom: 2.5,
minZoom: 2.5,
maxZoom: 22,
})
});
}
render() {
const providers = this.props.providers;
console.log("this is it"+providers[0])
return (
<div className={styles.wholeDiv}>
<div className={styles.root}>
<Paper className={styles.paper} zDepth={2} rounded>
<div className={styles.mapCard}>
<Card >
<CardHeader
title="Selected Area of Interest"
actAsExpander={true}
showExpandableButton={true}
/>
<CardMedia expandable={true}>
<div id="infoMap" className={styles.map} ref="olmap">
</div>
</CardMedia>
</Card>
</div>
</Paper>
</div>
</div>
)
}
}
ExportInfo.childContextTypes = {
muiTheme: React.PropTypes.object.isRequired,
}
export default (ExportInfo)
The id of your div (summaryMap) doesn't match target field in olmap configuration ('infoMap') both of them should be the same.
Additionally instead of calling _initializeOpenLayers in componentDidMount. I would recommend using ref callback and initialize target with actual control instead of string.
<div id="summaryMap" className={styles.map} ref={olmapDiv => this. _initializeOpenLayers(olmapDiv)}>
</div>

ag-grid react table not displaying?

New to react and ag-grid. I'm trying to implement a trivial example of ag-grid in my react project. When I run the project, the grid doesn't display. No console warnings, just the grid doesn't display.
import React from 'react';
import {AgGridReact} from 'ag-grid-react';
import '../../node_modules/ag-grid/dist/styles/ag-grid.css';
import '../../node_modules/ag-grid/dist/styles/theme-material.css';
var headers = [{headerName: 'Name', field: 'name', width: 150, filter: 'text'},
{headerName: 'Last', field: 'last', width: 150, filter: 'text'}]
var data = [{name: 'bob', last:'dude'},
{name: 'will', last:'willdude'}]
export default class Accounts extends React.Component {
constructor() {
super();
this.state = {
showGrid: true,
columnDefs: headers,
rowData: data,
};
}
onGridReady(params) {
this.api = params.api;
this.columnApi = params.columnApi;
}
render() {
var gridTemplate;
gridTemplate = (
<div className="ag-material">
<AgGridReact
columnDefs={this.state.columnDefs}
rowData={this.state.rowData}
onGridReady={this.onGridReady.bind(this)}
showGrid={this.state.showGrid}
/>
</div>
);
return (
<div style={{width: '800px'}}>
<p>I see this</p>
{gridTemplate}
</div>
)
}
}
Okay got it, we need height on the parent div.
e.g.
<div className="ag-material" style={{height: '500px'}}>
<AgGridReact
columnDefs={this.state.columnDefs}
rowData={this.state.rowData}
onGridReady={this.onGridReady.bind(this)}
showGrid={this.state.showGrid}
/>
</div>
its always better to give height and width otherwise ag-grid table will render by adjusting it to fit min content.
<div className="ag-material" style={{height: 500, width: 500}}>
<AgGridReact
columnDefs={this.state.columnDefs}
rowData={this.state.rowData}
onGridReady={this.onGridReady.bind(this)}
showGrid={this.state.showGrid}
/>
</div>
I was seeing a similar issue when the grid was not rendering. Adding the grid styles fixed the problem for me.
import '../../node_modules/ag-grid/dist/styles/ag-grid.css';
import '../../node_modules/ag-grid/dist/styles/theme-material.css';

Resources