-1

I'm working on Admin panel for my Portfolio page.

body {
  margin: 0;
  background-color: #E0E0E0;
}

.navbar {
  width: 100%;
  height: 40px;
  background-color: #21262a;
  position: fixed;
  top: 0;
  display: block;
}
.navbar .adminText {
  color: #d9f3fe;
  position: absolute;
  font-family: sans-serif;
  top: 50%;
  margin-left: 10px;
  transform: translateY(-50%);
  text-transform: uppercase;
  font-weight: 600;
  cursor: default;
  user-select: none;
}
.navbar .logout {
  float: right;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  border-style: none;
  height: 100%;
  width: 100px;
  background-color: #21262a;
  color: #d9f3fe;
  transition: .2s;
}

.logout:hover {
  background-color: #181b20;
  cursor: pointer;
}

.logout:focus {
  outline: none;
}

.control {
  height: calc(100% - 40px);
  width: 20%;
  background-color: #21262a;
  position: fixed;
  left: 0;
  bottom: 0;
}
.control .dashboard {
  position: relative;
  width: 100%;
  height: 50px;
  border: none;
  background-color: #21262a;
  color: #d9f3fe;
  font-size: 1.1em;
  transition: .3s;
  text-align: left;
}
.control .dashboard:hover {
  background-color: #181b20;
  cursor: pointer;
}
.control .dashboard:focus {
  outline: none;
}
.control .dashboard i {
  margin-right: 10px;
  float: left;
  margin-left: 10px;
}

.workStation {
  position: absolute;
  height: calc(100%-40px);
  width: 80%;
  right: 0;
  top: 40px;
  z-index: 100;
}

#active {
  background-color: #006da0;
}
#active:after {
  content: "";
  height: 20px;
  width: 20px;
  position: absolute;
  background-color: #E0E0E0;
  transform: rotate(45deg);
  right: -10px;
}
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
</head>

<div class="navbar">
<h class="adminText">Zenzzex Portfolio Admin Panel</h>
  <button class="logout">LOGOUT</button>
</div>

<aside class="control">
  <button class="dashboard" id="active"> <i class="fas fa-tachometer-alt"></i> Dashboard</button>
  
    <button class="dashboard"> <i class="fas fa-images"></i> Posts</button>
  
      <button class="dashboard"> <i class="fas fa-plus-square"></i> Add post</button>
</aside>

<aside class="workStation">
  <h1>THis is DASHBOARD</h1>
</aside>

That's my page.

Now, I will have it linked to my database, I'm doing it in PHP, but for now I'm working on frontend. So When I click on Posts I want to display content for that page in this gray area that is empty.

I think You know What I mean by now. So what's the best way to do it? Keep in mind, I'm doing my backend system in PHP.

MareGraphics MaRe
  • 439
  • 1
  • 5
  • 14

4 Answers4

1

An easy way to do this is by using the state pattern. This will let you load different pages by changing the state of your application. Let me show you an example of doing this.

<div id="content"></div>

const content = document.getElementById('content');

const PageState = function() {
  let currentState = new homeState(this);
  this.change = state => currentState = state;
}

const homeState = function() {
content.innerHTML = `This is my home page`;
}

const aboutState = function() {
content.innerHTML = `This is my about page`;
}

const page = new PageState();

page.change(new homeState);

You have the content element as the main tag of the page, then you simply call page.change() with the new state you want to have. This lets you avoid changing the page & only changing the content of it.

This will let you achieve a single page application without a javascript framework like react.js or vue.js. It's a neat & simple way to get started.

0

Please try this

    $(document).ready(() => {

        let return_text = true;
        let $content = $('.workStation');
        let $button = $('button.dashboard');

        if($button.attr('id') === 'active'){
            let data_uri = $button.data('uri');
            return_data($content,data_uri, return_text)
        }

        $button.click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            $this = $(this);
            $this.attr('id', 'active').siblings().removeAttr('id', 'active');
            let data_uri = $this.data('uri');
            return_data($content,data_uri, return_text)
        });

        function return_data($content,data_uri, return_text) {

            if (return_text) {
                $content.html(`<h1>${data_uri}</h1>`);
            } else {
                //Ajax
                $.get(data_uri).done(function (response, status){
                    if(status === 'success'){
                        $content.html(response);
                    }else{
                        alert('error! '+status);
                    }
                }).fail(function(jqXHR, textStatus){
                    alert("Request failed: " + textStatus);
                })
            }
        }
    });
 body {
        margin: 0;
        background-color: #E0E0E0;
    }

    .navbar {
        width: 100%;
        height: 40px;
        background-color: #21262a;
        position: fixed;
        top: 0;
        display: block;
    }

    .navbar .adminText {
        color: #d9f3fe;
        position: absolute;
        font-family: sans-serif;
        top: 50%;
        margin-left: 10px;
        transform: translateY(-50%);
        text-transform: uppercase;
        font-weight: 600;
        cursor: default;
        user-select: none;
    }

    .navbar .logout {
        float: right;
        position: relative;
        top: 50%;
        transform: translateY(-50%);
        border-style: none;
        height: 100%;
        width: 100px;
        background-color: #21262a;
        color: #d9f3fe;
        transition: .2s;
    }

    .logout:hover {
        background-color: #181b20;
        cursor: pointer;
    }

    .logout:focus {
        outline: none;
    }

    .control {
        height: calc(100% - 40px);
        width: 20%;
        background-color: #21262a;
        position: fixed;
        left: 0;
        bottom: 0;
    }

    .control .dashboard {
        position: relative;
        width: 100%;
        height: 50px;
        border: none;
        background-color: #21262a;
        color: #d9f3fe;
        font-size: 1.1em;
        transition: .3s;
        text-align: left;
    }

    .control .dashboard:hover {
        background-color: #181b20;
        cursor: pointer;
    }

    .control .dashboard:focus {
        outline: none;
    }

    .control .dashboard i {
        margin-right: 10px;
        float: left;
        margin-left: 10px;
    }

    .workStation {
        position: absolute;
        height: calc(100% - 40px);
        width: 80%;
        right: 0;
        top: 40px;
        z-index: 100;
    }

    #active {
        background-color: #006da0;
    }

    #active:after {
        content: "";
        height: 20px;
        width: 20px;
        position: absolute;
        background-color: #E0E0E0;
        transform: rotate(45deg);
        right: -10px;
    }
<head>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="navbar">
    <h class="adminText">Zenzzex Portfolio Admin Panel</h>
    <button class="logout">LOGOUT</button>
</div>

<aside class="control">
    <button class="dashboard" id="active" data-uri="This is Dashboard"><i class="fas fa-tachometer-alt"></i> Dashboard
    </button>
    <button class="dashboard" data-uri="This is Post"><i class="fas fa-images"></i> Posts</button>
    <button class="dashboard" data-uri="This is Add post"><i class="fas fa-plus-square"></i> Add post</button>
</aside>

<aside class="workStation"></aside>
GOSTRAFX
  • 37
  • 4
0

This code is simple in each button there is an attribute data-page="" put the name of the page in data-page="exemple.php or path/exemple.php"

    $(document).ready(() => {

        let $content = $('.workStation');
        let $button = $('button.dashboard');

        if ($button.attr('id') === 'active') {
            let $data_uri = $button.data('page');
            return_page($content, $data_uri)
        }

        $button.click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            $this = $(this);
            $this.attr('id', 'active').siblings().removeAttr('id');
            let $data_uri = $this.data('page');

            if ($data_uri !== '') {
                return_page($content, $data_uri);
            }else{
                $content.html('<h1>Empty data-uri attribute<h1/>');
            }

        });
        
        function return_page($content, $data_uri) {

            $($content).load( $data_uri,function(responseText,textStatus){
               if(textStatus === 'error') $content.html(`<h1>${$data_uri} Page not found<h1/>`);
            });

        }
    });
 body {
            margin: 0;
            background-color: #E0E0E0;
        }

        .navbar {
            width: 100%;
            height: 40px;
            background-color: #21262a;
            position: fixed;
            top: 0;
            display: block;
        }

        .navbar .adminText {
            color: #d9f3fe;
            position: absolute;
            font-family: sans-serif;
            top: 50%;
            margin-left: 10px;
            transform: translateY(-50%);
            text-transform: uppercase;
            font-weight: 600;
            cursor: default;
            user-select: none;
        }

        .navbar .logout {
            float: right;
            position: relative;
            top: 50%;
            transform: translateY(-50%);
            border-style: none;
            height: 100%;
            width: 100px;
            background-color: #21262a;
            color: #d9f3fe;
            transition: .2s;
        }

        .logout:hover {
            background-color: #181b20;
            cursor: pointer;
        }

        .logout:focus {
            outline: none;
        }

        .control {
            height: calc(100% - 40px);
            width: 20%;
            background-color: #21262a;
            position: fixed;
            left: 0;
            bottom: 0;
        }

        .control .dashboard {
            position: relative;
            width: 100%;
            height: 50px;
            border: none;
            background-color: #21262a;
            color: #d9f3fe;
            font-size: 1.1em;
            transition: .3s;
            text-align: left;
        }

        .control .dashboard:hover {
            background-color: #181b20;
            cursor: pointer;
        }

        .control .dashboard:focus {
            outline: none;
        }

        .control .dashboard i {
            margin-right: 10px;
            float: left;
            margin-left: 10px;
        }

        .workStation {
            position: absolute;
            height: calc(100% - 40px);
            width: 80%;
            right: 0;
            top: 40px;
            z-index: 100;
        }

        #active {
            background-color: #006da0;
        }

        #active:after {
            content: "";
            height: 20px;
            width: 20px;
            position: absolute;
            background-color: #E0E0E0;
            transform: rotate(45deg);
            right: -10px;
        }
<head>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="navbar">
    <h class="adminText">Zenzzex Portfolio Admin Panel</h>
    <button class="logout">LOGOUT</button>
</div>

<aside class="control">
    <button class="dashboard" id="active" data-page="dashboard.html"><i class="fas fa-tachometer-alt"></i> Dashboard
    </button>
    <button class="dashboard" data-page="post.html"><i class="fas fa-images"></i> Posts</button>
    <button class="dashboard" data-page="add_post.html"><i class="fas fa-plus-square"></i> Add post</button>
</aside>

<aside class="workStation"></aside>
</body>
GOSTRAFX
  • 37
  • 4
-1

Please try this

$('#post').click(function(){
  $('.title-dashboard').hide();
  $('.title-post').show();
  
  $('#dasboard').removeClass('active');
  $('#post').addClass('active');
});

$('#dasboard').click(function(){
  $('.title-dashboard').show();
  $('.title-post').hide();
  
  $('#post').removeClass('active');
  $('#dasboard').addClass('active');
});
body {
  margin: 0;
  background-color: #E0E0E0;
}

.navbar {
  width: 100%;
  height: 40px;
  background-color: #21262a;
  position: fixed;
  top: 0;
  display: block;
}
.navbar .adminText {
  color: #d9f3fe;
  position: absolute;
  font-family: sans-serif;
  top: 50%;
  margin-left: 10px;
  transform: translateY(-50%);
  text-transform: uppercase;
  font-weight: 600;
  cursor: default;
  user-select: none;
}
.navbar .logout {
  float: right;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  border-style: none;
  height: 100%;
  width: 100px;
  background-color: #21262a;
  color: #d9f3fe;
  transition: .2s;
}

.logout:hover {
  background-color: #181b20;
  cursor: pointer;
}

.logout:focus {
  outline: none;
}

.control {
  height: calc(100% - 40px);
  width: 20%;
  background-color: #21262a;
  position: fixed;
  left: 0;
  bottom: 0;
}
.control .dashboard {
  position: relative;
  width: 100%;
  height: 50px;
  border: none;
  background-color: #21262a;
  color: #d9f3fe;
  font-size: 1.1em;
  transition: .3s;
  text-align: left;
}
.control .dashboard:hover {
  background-color: #181b20;
  cursor: pointer;
}
.control .dashboard:focus {
  outline: none;
}
.control .dashboard i {
  margin-right: 10px;
  float: left;
  margin-left: 10px;
}

.workStation {
  position: absolute;
  height: calc(100%-40px);
  width: 80%;
  right: 0;
  top: 40px;
  z-index: 100;
}

.active {
  background-color: #006da0;
}
.active:after {
  content: "";
  height: 20px;
  width: 20px;
  position: absolute;
  background-color: #E0E0E0;
  transform: rotate(45deg);
  right: -10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
</head>

<div class="navbar">
<h class="adminText">Zenzzex Portfolio Admin Panel</h>
  <button class="logout">LOGOUT</button>
</div>

<aside class="control">
  <button class="dashboard" class="active" id="dasboard"> <i class="fas fa-tachometer-alt"></i> Dashboard</button>
  
    <button class="dashboard" id="post"> <i class="fas fa-images"></i> Posts</button>
  
      <button class="dashboard"> <i class="fas fa-plus-square"></i> Add post</button>
</aside>

<aside class="workStation">
  <h1 class="title-dashboard">THis is DASHBOARD</h1>
  <h1 style="display:none;" class="title-post">THis is POST</h1>
</aside>
Abed Putra
  • 1,153
  • 2
  • 17
  • 37