how to apply bootstrap table style to grid component

2 Answers 80 Views
Grid
David
Top achievements
Rank 1
Iron
David asked on 04 Oct 2024, 02:07 AM

hi,

can you please tell me how to apply bootstrap table style,i.e table-primary,table-secondary...,please see the below:

<table class="table-primary">...</table>

<table class="table-secondary">...</table> <table class="table-success">...</table> <table class="table-danger">...</table> <table class="table-warning">...</table> <table class="table-info">...</table> <table class="table-light">...</table> <table class="table-dark">...</table>

 

the grid i am using is descripted as below web page:

https://www.telerik.com/kendo-angular-ui/components/grid

 

thank you.

david

2 Answers, 1 is accepted

Sort by
0
Zornitsa
Telerik team
answered on 08 Oct 2024, 08:35 AM

Hi David,

The developer can directly add the mentioned bootstrap contextual classes to the Grid rows by implementing the approach from the following article that suggests using the rowClass function:

Here is also a StackBlitz example demonstrating the results from the above suggestion:

In this line of thought, I want to mention that since the built-in styling of the Grid comes from our Kendo Themes, we provide a Bootstrap theme that matches the Bootstrap 4 styling. In addition, all Kendo Themes can be customized per the developer's preference, including the colors.

So, instead of manually setting classes that apply certain colors like the mentioned ones (which in turn will override the built-in colors), the developer can customize the colors of the theme globally by modifying the corresponding color variables. More information about customizing the Kendo UI Bootstrap theme can be found in the below article from our Design System documentation:

I hope the provided information is helpful. 

Regards,
Zornitsa
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

David
Top achievements
Rank 1
Iron
commented on 17 Oct 2024, 01:58 PM

hi

the below web page seems does not work:

David
Top achievements
Rank 1
Iron
commented on 21 Oct 2024, 07:35 AM

hi,

any updates on this issue?

Zornitsa
Telerik team
commented on 22 Oct 2024, 07:54 AM

Hi David,

When I open the linked example, the suggested styling for the Grid rows is applied as expected on my side. Here is a screenshot for reference:

With the above being said, please ensure that the Bootstrap CDN is contained in the index.html file and is correctly loaded in the example since it is needed to recognize the applied contextual classes:

<head>
    <title>Kendo UI for Angular Example Application</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/themes/8.2.1/default/default-main.css"></link>
</head>
<body>
    <my-app>
        <span class="k-icon k-i-loading" style="color: #ff6358"></span>
    </my-app>
</body>

You can also verify if the Bootstrap CSS file was successfully loaded in the Network tab of the browser's Developer Tools and inspect the network requests:

Please check out the above suggestions and let me know how it goes. 

Regards,
Zornitsa
Progress Telerik

 

David
Top achievements
Rank 1
Iron
commented on 23 Oct 2024, 03:11 PM | edited

hi Zornitsa,

following your suggestion, changed my index.html here:

 <link
 rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css"
    />

 

then rowClass works fine.

i changed the bootstrap version to 5.3.3:

https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css

 

the issue came out again.

i think it should be bootstrop version.

i am using kendo ui 16.11.0 vs bootstrap 5.3.3.

 

can you please tell me which bootstrap version should i use?

 

thank you very much.

David
Top achievements
Rank 1
Iron
commented on 23 Oct 2024, 03:11 PM | edited

let me upload my package.json,angular.json and style.scss

{
  "name": "autocal_app_web",
  "version": "1.0.0",
  "scripts": {
    "pre": "npm install --legacy-peer-deps",
    "ng": "ng",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "live": "ng build --configuration=production --aot --output-hashing=all",
    "myinstall": "npm install --legacy-peer-deps"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^18.2.6",
    "@angular/common": "^18.2.6",
    "@angular/compiler": "^18.2.6",
    "@angular/core": "^18.2.6",
    "@angular/forms": "^18.2.6",
    "@angular/localize": "^18.2.6",
    "@angular/platform-browser": "^18.2.6",
    "@angular/platform-browser-dynamic": "^18.2.6",
    "@angular/router": "^18.2.6",
    "@fortawesome/angular-fontawesome": "^0.15.0",
    "@fortawesome/free-solid-svg-icons": "^6.6.0",
    "@ionic/angular": "^7.0.2",
    "@microsoft/signalr": "^5.0.17",
    "@microsoft/signalr-protocol-msgpack": "^6.0.6",
    "@progress/kendo-angular-buttons": "16.11.0",
    "@progress/kendo-angular-common": "16.11.0",
    "@progress/kendo-angular-dateinputs": "16.11.0",
    "@progress/kendo-angular-dialog": "16.11.0",
    "@progress/kendo-angular-dropdowns": "16.11.0",
    "@progress/kendo-angular-excel-export": "16.11.0",
    "@progress/kendo-angular-grid": "^16.11.0",
    "@progress/kendo-angular-icons": "16.11.0",
    "@progress/kendo-angular-inputs": "16.11.0",
    "@progress/kendo-angular-intl": "16.11.0",
    "@progress/kendo-angular-l10n": "16.11.0",
    "@progress/kendo-angular-label": "16.11.0",
    "@progress/kendo-angular-layout": "16.11.0",
    "@progress/kendo-angular-navigation": "16.11.0",
    "@progress/kendo-angular-pdf-export": "16.11.0",
    "@progress/kendo-angular-popup": "16.11.0",
    "@progress/kendo-angular-progressbar": "16.11.0",
    "@progress/kendo-angular-sortable": "^16.11.0",
    "@progress/kendo-angular-treelist": "^16.11.0",
    "@progress/kendo-angular-treeview": "16.11.0",
    "@progress/kendo-angular-utils": "16.11.0",
    "@progress/kendo-data-query": "^1.0.0",
    "@progress/kendo-drawing": "^1.17.2",
    "@progress/kendo-licensing": "^1.0.2",
    "@progress/kendo-svg-icons": "^3.0.0",
    "@progress/kendo-theme-bootstrap": "^9.0.0",
    "@progress/kendo-theme-default": "^8.2.1",
    "@types/jquery": "^3.5.6",
    "angular-split": "^14.1.0",
    "angular2-uuid": "^1.1.1",
    "bootstrap": "^5.3.3",
    "date-fns": "^2.10.0",
    "echarts": "^5.4.0",
    "install": "^0.13.0",
    "jquery": "^3.6.0",
    "linq-collections": "^1.0.254",
    "monaco-editor": "^0.50.0",
    "ng-multiselect-dropdown": "^1.0.0",
    "ng2-file-upload": "^7.0.1",
    "ngx-bootstrap": "^18.1.1",
    "ngx-echarts": "^18.0.0",
    "ngx-toastr": "^19.0.0",
    "npm": "^8.19.2",
    "pinyin": "^3.1.0",
    "rxjs": "~7.5.0",
    "segmentit": "^2.0.3",
    "tslib": "^2.1.0",
    "zone.js": "~0.14.10"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^18.2.6",
    "@angular/cli": "^18.2.6",
    "@angular/compiler-cli": "^18.2.6",
    "@angular/language-service": "^18.2.6",
    "@types/jasmine": "~4.0.0",
    "@types/node": "^12.20.55",
    "@types/pinyin": "^2.10.2",
    "@types/signalr": "^2.2.37",
    "jasmine-core": "~4.2.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.0.0",
    "sass": "^1.51.0",
    "typescript": "~5.5.2"
  }
}

 

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
 
  "projects": {
    "lab_cal_app_debug": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss",
          "skipTests": true
        },
        "@schematics/angular:class": {
          "skipTests": true
        },
        "@schematics/angular:directive": {
          "skipTests": true
        },
        "@schematics/angular:guard": {
          "skipTests": true
        },
        "@schematics/angular:interceptor": {
          "skipTests": true
        },
        "@schematics/angular:pipe": {
          "skipTests": true
        },
        "@schematics/angular:resolver": {
          "skipTests": true
        },
        "@schematics/angular:service": {
          "skipTests": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": {
              "base": "dist"
            },
            "loader": {
              ".ttf":"binary"
            },
            "index": "src/index.html",
            "polyfills": [
              "zone.js",
              "@angular/localize/init"
            ],
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              { "glob": "**/*", "input": "node_modules/monaco-editor/min/vs", "output": "vs"  }
            ],
            "styles": [
             
              "src/styles.scss"
            ],
            "scripts": [],
            "allowedCommonJsDependencies": [
              "angular2-uuid",
              "linq-collections",
              "segmentit"
            ],
            "browser": "src/main.ts"
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2500kb",
                  "maximumError": "20mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "1mb",
                  "maximumError": "10mb"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "buildTarget": "lab_cal_app_debug:build:production"
            },
            "development": {
              "buildTarget": "lab_cal_app_debug:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "buildTarget": "lab_cal_app_debug:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": [
              "zone.js",
              "zone.js/testing",
              "@angular/localize/init"
            ],
            "tsConfig": "tsconfig.spec.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              {
                "input": "node_modules/@progress/kendo-theme-default/dist/all.css"
              },
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "cli": {
    "analytics": false
  }
}

 

 
// @import "./../node_modules/bootstrap/dist/css/bootstrap.css";
// @import "./../node_modules/@progress/kendo-theme-bootstrap/dist/all.css";
@import "./../node_modules/bootstrap/scss/bootstrap.scss";
//@import "./../node_modules/@progress/kendo-theme-bootstrap/dist/all.css";
@import "@progress/kendo-theme-default/dist/all.scss";
@import "./../node_modules/ngx-bootstrap/datepicker/bs-datepicker.scss";
@import "./../node_modules/ngx-toastr/toastr.css";
@import "./styles.css";

 

thank you .

David
Top achievements
Rank 1
Iron
commented on 25 Oct 2024, 02:06 PM

hi

please any update on this issue?

Zornitsa
Telerik team
commented on 28 Oct 2024, 08:04 AM

Hi David,

Thank you for the additional clarifications provided.

Indeed, when adding the mentioned classes with Bootstrap v5.3.3, the respective colors are not applied to the Grid rows as in the scenario using the previous version.

On that note, it seems that the issue is caused due to some significant changes in how the classes are being handled in Bootstrap 5. What I mean is that according to the documentation of Bootstrap v5.3.x, the contextual classes will correctly apply the associated colors to the table rows only when the <tr> elements are contained in a <table> parent element that has the `.table` class set:

With the above being said, in order for the `.table-primary` and `.table-success` Bootstrap contextual classes to actually have an effect on the Grid rows in the current scenario, the developer can manually add the `.table` class to the internal <table> element of the Grid:

public ngAfterViewInit(): void {
    const gridTable = document.querySelector(".k-grid-content table"); 
  
    if (gridTable) {
      gridTable.classList.add("table");
    }
}

For demonstration, here is a StackBlitz example using Bootstrap v5.3.3, in which the above suggestion is implemented and the respective colors are applied to the Grid rows as expected:

Alternatively, a more straightforward approach that I would recommend for this particular case is for the developer to add custom classes to the Grid rows and apply the desired background colors with some custom CSS declarations, instead of relying on the Bootstrap contextual classes:

public rowCallback = (context: RowClassArgs) => {
    if (context.dataItem.UnitPrice <= 0) {
      return 'my-table-primary'
    } else {
      return 'my-table-success'
    }
};
#my-grid .my-table-primary{
    background-color: #cfe2ff;
}

#my-grid .my-table-success{
    background-color: #d1e7dd;
}

Below is a StackBlitz example, illustrating the results from the second approach as well:

I hope the provided information sheds some light on the matter.

Regards,
Zornitsa
Progress Telerik

David
Top achievements
Rank 1
Iron
commented on 03 Nov 2024, 01:32 PM | edited

hi Zornitsa

the below codes  worked very well:

public ngAfterViewInit(): void {
    const gridTable = document.querySelector(".k-grid-content table"); 
  
    if (gridTable) {
      gridTable.classList.add("table");
    }
}

 

but i have another qurestion,how to set the table border?

i used the below codes .it does not work.

 

gridTable.classList.add("table");

gridTable.classList.add('table-hover');
gridTable.classList.add('table-bordered');
gridTable.classList.add('border-primary');

please have a look.

btw,i feel it is hard to control genrated html table class (including table ,thead,tbody,tr td...).

it would be better to provide some apis to let me control the css name s(or css styles )directly?

 

thank you.

Zornitsa
Telerik team
commented on 06 Nov 2024, 03:10 PM

Hi David,

You are correct, it is indeed a bit complicated to style the Grid rows by manually applying Bootstrap contextual classes since they are provided by an external framework and are not native to the Grid component.  

In this line of thought, what we recommend for styling the rows of the Grid is utilizing the built-in rowClass functional callback of the component, which allows the developer to add custom CSS classes for the rows as preferred. The defined custom classes can then be used in suitable selectors for applying the desired CSS declarations to the Grid rows, e.g. setting specific color, background color, border, padding, and others.

For more details about the recommended approach for styling the rows of the Grid and how to use the rowClass function, please refer to the following article from our documentation:

Here is also a StackBlitz example that demonstrates how to apply some CSS customizations to the Grid rows by using the custom classes defined through the rowClass function:

For further reference, the Grid component also provides various other built-in approaches for styling individual cells and columns that can be utilized appropriately as per the requirements of the application:

I hope the provided information is helpful. 

Regards,
Zornitsa
Progress Telerik
0
David
Top achievements
Rank 1
Iron
answered on 13 Nov 2024, 01:36 AM

hi Zronitsa,

the issue was reslvoed, thank you very much.

 

br

David

Tags
Grid
Asked by
David
Top achievements
Rank 1
Iron
Answers by
Zornitsa
Telerik team
David
Top achievements
Rank 1
Iron
Share this question
or