In part 1, we created a workspace plugin that would get a list of forms, folders, and records. The plugin would display the forms as tabs, the folders as columns, and the records in a list, by column.
At this point you probably only have 1 column and you want to do more than just see a list of records. In this guide we will work on adding new folders/columns and moving records from one column to another.
Adding Folders
Let’s start by adding a column to the board that prompts for a folder name. You may recall some of this code from part 1, as a reference where to put the add folder HTML.
This represents patterns for forms and buttons that will match the app. The ng-show for formId will make sure the column only appears when a form has been selected, since a form is required to create a form folder. The ng-model makes the new folder name accessible from the $scope as the property addFolderName. We will write the addFolder function below in the plugin JavaScript to make it work.
The following function will post data to the FormFolders endpoint to create a new folder using the name from above. After successfully creating a new folder, it will update the list of folders and initialize an empty record list for the folder. With AngularJS 2-way data binding, updating the folders property will automatically make a new column appear in the interface.
A new service is also introduced, called znMessage, so be sure to add that to the dependencies in a similar way to $routeParams and znData. The znMessage service is used here to indicate success or failure to the user.
Moving Records
Users can now add folders, but without a way to change the record folder from this screen, the new columns are probably empty. Let’s add the ability to move the records between lists using the ui-sortable directive.
In the plugin JavaScript, we need to add some sortable options to the $scope. This will connect the record lists and allow you to drag records from one folder to another.
Next, in the plugin HTML, add the directive ui-sortable to the record list as seen below. As you can see, it should reference the sortableOptions from above. We also need to add ng-model referencing the list of records for ui-sortable to work.
One more small, but important, addition is to update the CSS to add some height to empty lists. This is necessary to be able to drag items onto empty lists. Add the following to the plugin CSS.
Saving Record Folders
Now that users can move records into different folders, let’s add a way to save the changes. Starting with the plugin HTML, we will need to add a way to identify the record being moved. We can do this by adding a data-id attribute to the record item.
Next, we need to update the sortable options to trigger a save when a record is moved. Sortable provides several callbacks when lists are updated. Here we can take advantage of the stop callback with the function you see below.
When the sorting has stopped, we traverse the known folders and records to find the where the record was moved. When the record is found and the new folder is different from the current folder, it uses the znData service to save the new folder ID. Once the save is complete, it updates the folder record list with the response.
Wrapping Up
Your plugin should now be able to display folders as columns, create folders, drag records from one folder to another, and save the results.
The code for the entire record board plugin can be found below and also on Github. In this case, the plugin namespace is ‘namespaced’, so to make it work as your own, you will need to replace all instances of the word ‘namespaced’ with your namespace.
If you have improvements to the plugin, feel free to make pull requests to the code repository and update the documentation for it here.