Intelligent Table - Setting a predefined row as default

Currently, I am utilizing smart-table and facing the need to preselect a specific row.

After loading my list, I iterate through it and set the isSelected attribute once I locate the item I intend to select:

// Preselecting a row
for (var i = 0, len = scope.displayCollection.length; i < len; i += 1) {
    var person = scope.displayCollection[i];
    if (person.firstName === 'Blandine') {
        person.isSelected = true;
        scope.selected = person;
        break;
    }
}

Although this process works well, I encounter an issue when attempting to select another row. The previously preselected row remains selected, requiring me to manually deselect it before selecting a new row.

To further illustrate the problem, here is a JSFiddle link: http://jsfiddle.net/6pykn5hu/3/

I have also attempted the solution suggested in Smart-Table - Select first row displayed (angularjs) with no success.

Any assistance on resolving this issue would be greatly appreciated. Thank you.

Answer №1

Upon reviewing their directive, it became apparent that a function in the parent directive stTable was being called. The row is connected to a click handler which triggers the ctrl.select() function from stTable, storing the last selected row. The issue arises when this event fails to fire, leaving the last clicked row unset and consequently not removing its class. To address this problem, I have rewritten the directive to better align with your intended functionality, though there is room for further enhancement.

app.directive('prSystem', function () {
  return {
    restrict: 'A',
      require: '^stTable',
      scope: {
        row: '=prSystem'
      },
      link: function (scope, element, attr, ctrl) {
          var mode = attr.stSelectMode || 'single';
          if(scope.row.isSelected) {
              scope.row.isSelected = undefined;
              ctrl.select(scope.row, mode);
          }

        element.bind('click', function () {
          scope.$apply(function () {
            ctrl.select(scope.row, mode);
          });
        });

        scope.$watch('row.isSelected', function (newValue) {
          if (newValue === true) {
            element.addClass('st-selected');
          } else {
            element.removeClass('st-selected');
          }
        });
      }
  }
})

Answer №2

After much searching, I've come up with a new approach:

Instead of using $watch, I decided to implement a ng-click on each row:

<tr st-select-row="row" ng-repeat="row in displayCollection" ng-click="selectRow(row)">

I also added a function to manually unselect all rows before selecting the clicked one:

scope.selectRow = function (row) {
    for (var i = 0, len = scope.displayCollection.length; i < len; i += 1) {
        scope.displayCollection[i].isSelected = false;
    }
    row.isSelected = true;
    scope.selected = row;
}

Here is the working JSFiddle link for reference: http://jsfiddle.net/6pykn5hu/6/

If anyone has a more efficient solution, feel free to share - I'm always open to new ideas :)

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

How can I rearrange divs using CSS? Can I add parent 1 in between the children of parent 2

Check out my current HTML code structure: <div class="container> <div class="parent1"></div> <div class="parent2"> <div class="child1"></div> <div class="child2"></div> </div ...

"ng-show does not respond to the completion of $http.success() or .error() function calls

Upon submitting a form to a web service, the following angular http post result is expected to trigger either the success or failure display of two HTML elements. Despite receiving a 200 OK response and a "Success" string in return, the success HTML elemen ...

How to adjust margins and padding for the <input type='radio' /> element using CSS?

Struggling to adjust the CSS for my radio buttons as their default settings are making my layout appear asymmetrical. I am using JavaScript to generate multiple forms and injecting a lot of inline styling like style='margin:0px padding:0px' to ma ...

Transmit a file using multipart with XMLHttpRequest

Is it possible to use XMLHttpRequest to send a multipart file to a servlet? I am currently working on a form that needs to be submitted as multipart, but I am not receiving a response after successfully uploading it. It is important that the process happe ...

How to extract the value of a key from JSON using JavaScript

Need help with an API call to retrieve a list of subcategories? Here's an example of the JSON format: { "description": "Flower", "name": "Flower", "parent_id": "1" }, { "description": "Moon", "n ...

Filling form fields with array data (AngularJS)

Although I'm new to AngularJS, I'm struggling to figure out how to handle a list of arrays returned from the searchAll function. console 0: {mobile: "12345678", lastname: "last01", firstname: "first01"} 1: {mobile: "87654321", lastname: ...

Side menu and grid display using HTML and CSS

Looking to revamp the layout of my angularJS website, specifically one page. Currently, information is displayed in a table format but I would like to switch it up by adding a left sidebar featuring a list of names (tiles). When a user clicks on a name, th ...

What is the best way to use Three.js to translate an object within a defined plane?

While exploring the example provided on the website (), I noticed that they are moving the selected cube along a plane relative to the camera using unprojection with raycaster. However, I am struggling to understand how to configure this setup so that the ...

Is there a compatibility problem between JQuery 'hold' button when using toggleClass and hasClass methods?

My current project involves a system that randomly generates text from a database, with the added feature of a 'hold' button for each line. The idea is to hold a specific line while others are refreshed using AJAX. I have successfully implemented ...

Export buttons in Jquery datatable are malfunctioning

I have been attempting to include export buttons for Excel and PDF in a jquery data table using its extension, but I am facing an issue where the buttons are not showing up above the table. Despite trying various other solutions from different sources, I ...

Blank white screen issue in Three JS while loading JSON object

I have recently downloaded threejs and am attempting to create a 3D environment where users can freely move around in an "ego perspective". To start, I used the pointlock-control example as a reference and now I'm trying to add my own elements, beginn ...

Can a form be selected and submitted using only its class as a selector?

When trying to select and submit a form using only the class as a selector, I encountered an issue: $("form.class").submit(...) doesn't seem to work... however: $("form#id").submit(...) works perfectly fine. Any idea what could be causing this di ...

Having trouble with a nested Angular 2 component not showing up on the page?

I'm currently developing a mobile app with Angular 2 and Nativescript. In my setup, I have the HomeComponent, which includes a DockComponent. The DockComponent is functioning correctly, but now I want to break down the four buttons in the dock into se ...

What is the best way to dynamically fill a dropdown list based on the chosen value of another dropdown list in MVC?

I'm currently working on a project where I need to populate the values for a car's model based on the selected make in a dropdown list. Being new to coding, I'm facing an issue where no values are displayed when I select the make. There are ...

Displaying and Concealing Messages with VueJS

Currently, I have set up a basic CLI structure environment and created a component that displays messages/alerts such as "Login Failed." Since this component is intended to be reused throughout the entire app, I decided to import it into the root App.vue f ...

Hold off on finishing numerous ajax requests that may be executed simultaneously

Is there a way to ensure that three ajax calls are allowed to run concurrently, but have the browser wait until all of them have completed before continuing? Setting async as false for each call can be time-consuming since they then cannot run at the sam ...

Error encountered while using Jest, React, Typescript, and React-Testing-Library

I have set up a project using React/NextJS with Typescript and now I am incorporating unit testing with Jest and React Testing Library. One of the unit tests for my component is as follows: import React from 'react'; import '@testing-libra ...

Dynamically determining the direction of a page with Vue Js

Is it possible to dynamically set the page direction (RTL or LTR) in a Vue js project? I have tried setting it dynamically using a variable called direction but it doesn't seem to work (it continues to use the standard LTR value): <html lang="e ...

Using jquery to toggle active nav links in Bootstrap

I'm currently working on integrating a jQuery function to automatically update the active status of the navigation links in my Bootstrap Navbar. The structure involves a base HTML file that extends into an app-specific base file, which is further exte ...

Removing information from a MongoDB database with the help of AngularJS and Node.js

Having trouble deleting data in MongoDB using AngularJS and Node.js, as I keep encountering the error "Cannot DELETE /api/manage-product" in the console. .html file <tbody> <tr ng-repeat="product in vm.result"> ...