Retrieve data from a row in a kogrid when a checkbox is selected

I am working with a kogrid that contains checkboxes in one column. There are two issues that I am facing:

  1. How can I retrieve row data when a checkbox is checked or unchecked?
  2. I have a save button located outside the kogrid. When this button is clicked, I want to submit the row values as a JSON object.

Here is the HTML code:

<div id="reconciliationGrid" data-bind="koGrid: reconciliationGridOptions"></div>

And here is the JavaScript code:

this.currentRowValues = ko.observableArray(); 
self.reconciliationGridOptions = { 
data: self.reconciliationGrid, 
canSelectRows: true, 
afterSelectionChange: function () { return true; }, 
selecteditems: this.currentRowValues 
};

Answer №1

It seems like there may be an error in your code at selecteditems, but the correct syntax is actually selectedItems. Take a look at the example below which logs the currently selected row, displays all selected items in a div, and triggers an alert with all selected data upon clicking the save button.

function viewModel(){
  this.currentRowValues = ko.observableArray([]); 
   this.gridData = ko.observableArray([{name: "Moroni", age: 50},
                                      {name: "Tiancum", age: 43},
                                      {name: "Jacob", age: 27},
                                      {name: "Nephi", age: 29},
                                      {name: "Enos", age: 34}]);
  
  this.reconciliationGridOptions = { 
                      data: this.gridData, 
                      selectedItems: this.currentRowValues ,
                      afterSelectionChange: function (selectedRow) { 
                        console.log(selectedRow);
                        return true;
                      }, 
                };  
};
viewModel.prototype.save = function(){
   alert(ko.toJSON(this.currentRowValues()));
};

ko.applyBindings(new viewModel());
.gridStyle {
    border: 1px solid rgb(212,212,212);
    width: 400px; 
    height: 300px;
 
}

button{
  padding: 10px;
  margin: 10px 0 10px 0;
}

.selectedItems{
    border: solid black 1px;
    padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<link href="https://cdn.lukej.me/kogrid/2.0.6/KoGrid.css" rel="stylesheet"/>
<script src="https://cdn.lukej.me/kogrid/2.0.6/koGrid.debug.js"></script>



<div class="gridStyle" id="reconciliationGrid" data-bind="koGrid: reconciliationGridOptions"></div> 

<button data-bind="click: save">Save Example</button>
<br/>
<div class="selectedItems" data-bind="foreach: currentRowValues">
       <span data-bind="text:ko.toJSON($data)"></span>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps for utilizing JQuery to send JSON data?

I need to send Json data to a web service located on the same server, but I am unsure of how to achieve this using JQuery. Here is the code I have attempted: $.ajax({ type: 'POST', url: '/form/', data: {"name":"jonas"}, ...

Can you explain the meaning of <!-- in javascript?

Have you ever noticed the <!-- and --> characters being used around JavaScript code like this: <script type="text/javascript"> <!-- function validateForm() { if(document.pressed == 'Submit') { ...

The incorrect order of CSS in NextJS production build

When working on my project, I make sure to import CSS files both from local sources and node modules: //> Global Styling // Local import "../styles/globals.scss"; // Icons import "@fortawesome/fontawesome-free/css/all.min.css"; // Bootstrap import "boot ...

What causes fs to produce an error when routing to a new page, yet refreshing the page resolves the issue?

Concern: I have developed a NextJs application with 4 input fields, each connected to a predefined options list read in as a json file within the project. The user can select two fields and then proceed to a search page by clicking a button. From the sear ...

Having trouble getting event modifiers to work in Vue when added programmatically

Trying to dynamically add events using v-on through passing an object with event names as keys and handlers as values. It appears that this method does not support or recognize event modifiers such as: v-on=“{‘keydown.enter.prevent’: handler}” T ...

Elements with v-for directive failing to display

I am attempting to incorporate a component using v-for. The data source infoTexts is structured as an Object where the locale code serves as the key and the corresponding message is the value. For example: { nl: 'Text in Dutch', fr: &apo ...

What distinguishes v-text from v-load in Vue.js when concealing {{ Mustache }}?

When experiencing the "flash of uncompiled content" in Vue, the brief moment when the page is loading and you see the {{ Mustache }} syntax, developers often use either v-text or v-cloak. According to the documentation, v-text: Updates the element’s t ...

Unexpected Error: The axiosCookieJarSupport function is throwing a TypeError, functioning properly in Node.JS but not in .vue pages. What could

Struggling with a function that authenticates on a website, it runs smoothly in a basic node.js script but fails when executed from a .vue page within the NuxtJS framework. The error message received when running in the .vue page is TypeError: axiosCookie ...

Why is Reactjs axios returning a promise instead of the expected value?

I have been attempting to retrieve data from an API using axios, but all I am getting back is a Promise. The Node.js code: router.get('/isAdmin/:userId/:groupeId', async (req, res) => { let userId = req.params.userId let groupeId = ...

How can data be transmitted to the client using node.js?

I'm curious about how to transfer data from a node.js server to a client. Here is an example of some node.js code - var http = require('http'); var data = "data to send to client"; var server = http.createServer(function (request, respon ...

What is the best way to asynchronously load an external javascript file in a popup.html file?

I have successfully implemented all the necessary functionalities, but I noticed a delay in loading the popup.html after adding an external javascript file. This file is only a few lines long, so the delay is quite frustrating. To eliminate this lag, I be ...

The onsubmit function is not successfully executing the JavaScript function

This code contains a form for creating a new user login. Upon pressing the 'continue' button, it is supposed to call the 'validateForm()' function which checks the values entered in the form. However, even when there are errors such as ...

AngularJS: Showcase HTML code within ng-view along with its corresponding output displayed in a navigation format such as Html, Css, JS, and Result

Currently, I am working on a web application. One of the screens, screen-1, consists of a series of buttons labeled 'Code'. When a user clicks on any of these buttons, it loads a different HTML page, screen-2, in the ng-view using $location.path( ...

JavaScript's "for of" loop is not supported in IE 11, causing it to fail

Here is a piece of code I'm using to select and remove a d3.js node: if (d.children) { for (var child of d.children) { if (child == node) { d.children = _.without(d.children, child); update(root); ...

What is the process for indicating the cache directory in npm5 when using the install command?

When using yarn, you can specify the cache folder with yarn --cache-folder [CACHE_FOLDER]. Is there an npm5 alternative to this? One option is to set the cache folder with a separate command: npm config set cache [CACHE_FOLDER]. However, I'm wonderin ...

When the component is reloaded, props will become defined

I am trying to iterate through an object's array to create a table structure like the one below: import React from "react"; export default function Scoreboard(props) { return ( <div className="scoreboard"> <ta ...

Encountering error ORA-12514 when utilizing the node oracle-db npm package

At the moment, I am immersed in a project that relies on utilizing oracle for its backend. Following the instructions provided in this link, I successfully installed node-oracledb on my Mac using npm. The contents of my file are as follows: var oracledb = ...

Transforming control of execution seamlessly between two interactive functions in nodejs

Two functions are used to take input from the CLI using process.stdin. The issue arises when one function is done taking input, as a similar function is called. However, while the control shifts to the second function, the first function continues executin ...

Module is absent in JavaScript but present in TypeScript

As I delve into coding a vscode extension by following a tutorial, I encountered an issue with importing in my server.ts file. The directory structure looks like this: ...

The loading process in CSS can be customized using unique IDs such as loading1 and loading300

My current CSS code looks like this: #loading{ display:none; } The issue I am facing is that I need to hide multiple loading divs, each with an id that includes the word "loading". For example: <div id=loading1></div> <div id=loading42 ...